当前位置: 首页 > news >正文

长春哪家做网站做的好seo优化培训班

长春哪家做网站做的好,seo优化培训班,瑞金网络推广,品牌商城网站项目Java一一一简易图书管理系统 1. 需求分析 功能需求: 添加图书删除图书更新图书信息查询图书列出所有图书 2. 设计 实体类:Book业务逻辑类:LibraryManager 3. 实现 3.1 Book类 public class Book {private String id;private String t…

Java一一一简易图书管理系统

1. 需求分析
  • 功能需求
    • 添加图书
    • 删除图书
    • 更新图书信息
    • 查询图书
    • 列出所有图书
2. 设计
  • 实体类Book
  • 业务逻辑类LibraryManager
3. 实现
3.1 Book类
public class Book {private String id;private String title;private String author;private int year;public Book(String id, String title, String author, int year) {this.id = id;this.title = title;this.author = author;this.year = year;}// Getters and Setterspublic String getId() {return id;}public void setId(String id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public int getYear() {return year;}public void setYear(int year) {this.year = year;}@Overridepublic String toString() {return "Book{" +"id='" + id + '\'' +", title='" + title + '\'' +", author='" + author + '\'' +", year=" + year +'}';}
}
3.2 LibraryManager类
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;public class LibraryManager {private List<Book> books;public LibraryManager() {this.books = new ArrayList<>();}public void addBook(Book book) {books.add(book);}public boolean removeBook(String id) {return books.removeIf(book -> book.getId().equals(id));}public void updateBook(String id, Book updatedBook) {Book book = books.stream().filter(b -> b.getId().equals(id)).findFirst().orElse(null);if (book != null) {book.setTitle(updatedBook.getTitle());book.setAuthor(updatedBook.getAuthor());book.setYear(updatedBook.getYear());}}public List<Book> searchBooksByTitle(String title) {return books.stream().filter(book -> book.getTitle().contains(title)).collect(Collectors.toList());}public List<Book> getAllBooks() {return new ArrayList<>(books);}
}
4. 测试
public class Main {public static void main(String[] args) {LibraryManager manager = new LibraryManager();// 添加图书manager.addBook(new Book("1", "Java Basics", "John Doe", 2021));manager.addBook(new Book("2", "Advanced Java", "Jane Doe", 2022));// 列出所有图书System.out.println("All Books:");manager.getAllBooks().forEach(System.out::println);// 更新图书Book updatedBook = new Book("1", "Java Fundamentals", "John Doe", 2021);manager.updateBook("1", updatedBook);// 搜索图书System.out.println("Search Results:");manager.searchBooksByTitle("Java").forEach(System.out::println);// 删除图书manager.removeBook("2");// 再次列出所有图书System.out.println("All Books after removal:");manager.getAllBooks().forEach(System.out::println);}
}
5. 总结

这个简易的图书管理系统展示了Java在面向对象编程中的基本应用,包括类的定义、方法的实现以及简单的集合操作。通过这个案例,初学者可以学习到如何使用Java创建和管理对象集合,以及如何实现简单的业务逻辑。

请注意,这只是一个基础示例,实际应用中可能需要考虑更多的功能和异常处理,以及与数据库的交互等。

案例名称:基于Spring Boot的RESTful图书管理系统

1. 环境准备
  • Java 11 或更高版本
  • Spring Boot 2.5.x 或更高版本
  • Maven 或 Gradle 作为构建工具
  • 一个文本编辑器或IDE(如IntelliJ IDEA或Eclipse)
2. 项目结构
src/
|-- main/
|   |-- java/
|   |   `-- com.example.bookstore/
|   |       |-- BookController.java
|   |       |-- BookService.java
|   |       `-- Book.java
|   `-- resources/
|       `-- application.properties
3. 实现步骤
3.1 创建Spring Boot项目

使用Spring Initializr来生成项目基础结构。

3.2 添加依赖

pom.xml(Maven)或build.gradle(Gradle)中添加Web和JPA依赖。

pom.xml 示例:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency>
</dependencies>
3.3 Book实体类
package com.example.bookstore;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Book {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String title;private String author;private int publishYear;// Constructors, getters and setters
}
3.4 BookRepository接口
package com.example.bookstore;import org.springframework.data.jpa.repository.JpaRepository;public interface BookRepository extends JpaRepository<Book, Long> {
}
3.5 BookService业务逻辑类
package com.example.bookstore;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;@Service
public class BookService {@Autowiredprivate BookRepository bookRepository;public List<Book> getAllBooks() {return bookRepository.findAll();}public Book getBookById(Long id) {return bookRepository.findById(id).orElse(null);}public Book addBook(Book book) {return bookRepository.save(book);}public Book updateBook(Long id, Book updatedBook) {Optional<Book> bookOptional = bookRepository.findById(id);if (bookOptional.isPresent()) {Book book = bookOptional.get();book.setTitle(updatedBook.getTitle());book.setAuthor(updatedBook.getAuthor());book.setPublishYear(updatedBook.getPublishYear());return bookRepository.save(book);}return null;}public void deleteBook(Long id) {bookRepository.deleteById(id);}
}
3.6 BookController控制器
package com.example.bookstore;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/api/books")
public class BookController {@Autowiredprivate BookService bookService;@GetMappingpublic List<Book> getAllBooks() {return bookService.getAllBooks();}@GetMapping("/{id}")public ResponseEntity<Book> getBookById(@PathVariable Long id) {Book book = bookService.getBookById(id);return book != null ? ResponseEntity.ok(book) : ResponseEntity.notFound().build();}@PostMappingpublic Book addBook(@RequestBody Book book) {return bookService.addBook(book);}@PutMapping("/{id}")public ResponseEntity<Book> updateBook(@PathVariable Long id, @RequestBody Book updatedBook) {Book book = bookService.updateBook(id, updatedBook);return book != null ? ResponseEntity.ok(book) : ResponseEntity.notFound().build();}@DeleteMapping("/{id}")public ResponseEntity<Void> deleteBook(@PathVariable Long id) {bookService.deleteBook(id);return ResponseEntity.ok().build();}
}
3.7 配置文件application.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialectspring.h2.console.enabled=true
4. 运行和测试

使用您喜欢的IDE运行main方法或使用Maven/Gradle命令来启动Spring Boot应用。使用Postman或curl命令行工具来测试API。

5. 总结

这个案例展示了如何使用Spring Boot创建一个简单的RESTful API,它提供了基本的CRUD操作。通过这个案例,您可以学习到Spring Boot的核心概念,如依赖注入、服务层、数据访问层以及RESTful API设计。

这个示例使用了内存数据库H2,适合开发和测试,但在生产环境中,您应该使用更健壮的数据库解决方案,并考虑安全性、异常处理和日志记录等因素。

接下来,我们将在图书管理系统中增加一个新功能:用户认证和授权。我们将使用Spring Security来实现这一功能,确保只有认证用户可以访问敏感数据和执行特定操作。

带有用户认证和授权的图书管理系统

1. 添加Spring Security依赖

首先,需要在项目的pom.xml(Maven)或build.gradle(Gradle)文件中添加Spring Security的依赖。

pom.xml 示例:

<dependencies><!-- ... other dependencies ... --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
</dependencies>
2. 配置Spring Security

创建一个配置类来配置Spring Security。

package com.example.bookstore;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;@Configuration
@EnableWebSecurity
public class SecurityConfig {@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Beanpublic UserDetailsService userDetailsService(PasswordEncoder encoder) {InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();manager.createUser(User.withUsername("user").password(encoder.encode("password")).roles("USER").build());return manager;}@Beanpublic HttpSecurity httpSecurity(UserDetailsService userDetailsService) throws Exception {return new HttpSecurity(http -> http.authorizeRequests().antMatchers("/api/books/**").authenticated().and().httpBasic().and().csrf().disable());}
}
3. 保护RESTful API

使用@PreAuthorize注解来保护特定的API端点。

package com.example.bookstore;import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/api/books")
public class BookController {// ... existing code ...@PreAuthorize("hasRole('USER')")@DeleteMapping("/{id}")public ResponseEntity<Void> deleteBook(@PathVariable Long id) {bookService.deleteBook(id);return ResponseEntity.ok().build();}
}
4. 增加用户注册功能

创建一个用户注册的API端点。

// ... existing imports ...@RestController
@RequestMapping("/api/auth")
public class AuthController {private final UserService userService;public AuthController(UserService userService) {this.userService = userService;}@PostMapping("/register")public ResponseEntity<?> registerUser(@RequestBody UserRegistrationDto userRegistrationDto) {userService.registerUser(userRegistrationDto);return ResponseEntity.ok().build();}
}
5. 用户注册服务和数据传输对象

创建UserServiceUserRegistrationDto来处理用户注册。

package com.example.bookstore;// UserService.java
@Service
public class UserService {private final UserRepository userRepository;private final PasswordEncoder passwordEncoder;@Autowiredpublic UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {this.userRepository = userRepository;this.passwordEncoder = passwordEncoder;}public void registerUser(UserRegistrationDto userRegistrationDto) {User newUser = new User(userRegistrationDto.getUsername(),passwordEncoder.encode(userRegistrationDto.getPassword()));userRepository.save(newUser);}
}// UserRegistrationDto.java
public class UserRegistrationDto {private String username;private String password;// Constructors, getters and setters
}
6. 更新数据库配置

application.propertiesapplication.yml中添加用户表的配置。

# application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update# H2 console settings
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
7. 测试和验证

启动应用程序,并使用Postman或curl测试用户注册和认证保护的API。

8. 总结

通过增加用户认证和授权,我们的图书管理系统变得更加安全。Spring Security提供了一套强大的安全工具,可以轻松地集成到Spring Boot应用程序中。通过使用@EnableWebSecurity注解和配置HttpSecurity,我们可以控制对API端点的访问。此外,@PreAuthorize注解允许我们在方法级别细粒度地控制访问权限。

请注意,本案例中的用户存储在内存中,仅用于演示目的。在实际应用中,您应该使用数据库来持久化用户信息,并考虑使用JWT或OAuth2等更先进的认证机制。

接下来,我们将在图书管理系统中增加一个新功能:基于角色的访问控制(RBAC)。这将允许我们根据用户的角色限制对某些资源的访问。

基于角色的访问控制的图书管理系统

1. 扩展用户模型

首先,我们需要扩展用户模型来包含角色信息。

package com.example.bookstore;import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;import javax.persistence.*;
import java.util.Collection;
import java.util.stream.Collectors;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String username;private String password;@ManyToMany(fetch = FetchType.EAGER)private Set<Role> roles;// Constructors, getters and setterspublic Collection<? extends GrantedAuthority> getAuthorities() {return roles.stream().map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList());}
}
2. 角色模型

创建一个角色模型来定义不同的角色。

package com.example.bookstore;@Entity
public class Role {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;// Constructors, getters and setters
}
3. 用户角色关联

创建一个关联表来定义用户和角色之间的关系。

package com.example.bookstore;import javax.persistence.*;@Entity
@Table(name = "user_role")
public class UserRole {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;@ManyToOne@JoinColumn(name = "user_id")private User user;@ManyToOne@JoinColumn(name = "role_id")private Role role;// Constructors, getters and setters
}
4. 更新UserRepository

添加方法来查找用户及其角色。

package com.example.bookstore;import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {User findByUsername(String username);
}
5. 更新Spring Security配置

更新SecurityConfig来使用自定义的UserDetailsService

package com.example.bookstore;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate UserRepository userRepository;@Bean@Overridepublic UserDetailsService userDetailsService() {return new UserDetailsManager() {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {User user = userRepository.findByUsername(username);if (user == null) {throw new UsernameNotFoundException("User not found");}return user;}// Other methods are omitted for brevity};}// Other configurations are omitted for brevity
}
6. 基于角色的访问控制

使用@PreAuthorize@Secured注解来限制访问。

package com.example.bookstore;import org.springframework.security.access.prepost.PreAuthorize;@RestController
@RequestMapping("/api/books")
public class BookController {// ... existing code ...@PreAuthorize("hasRole('ADMIN')")@PostMappingpublic Book addBook(@RequestBody Book book) {return bookService.addBook(book);}@PreAuthorize("hasRole('USER')")@GetMappingpublic List<Book> getAllBooks() {return bookService.getAllBooks();}
}
7. 用户角色管理

创建API端点来管理用户角色。

package com.example.bookstore;@RestController
@RequestMapping("/api/users")
public class UserController {private final UserService userService;@Autowiredpublic UserController(UserService userService) {this.userService = userService;}@PostMapping("/{username}/roles")public ResponseEntity<?> addRoleToUser(@PathVariable String username, @RequestBody RoleRequestDto roleRequestDto) {userService.addRoleToUser(username, roleRequestDto.getRoleName());return ResponseEntity.ok().build();}
}// RoleRequestDto.java
public class RoleRequestDto {private String roleName;// Constructors, getters and setters
}
8. 测试和验证

启动应用程序,并使用Postman或curl测试基于角色的访问控制。

9. 总结

通过实现基于角色的访问控制,我们的图书管理系统可以更细致地控制用户对资源的访问。这在多用户应用中尤为重要,可以确保用户只能访问他们被授权的资源。

这个案例演示了如何在Spring Security中使用自定义的UserDetailsService来加载用户及其角色信息,以及如何使用@PreAuthorize注解来实现基于角色的访问控制。在实际应用中,您可能还需要实现更复杂的用户和角色管理功能,以及更细粒度的权限控制策略。


文章转载自:
http://albedo.yrpg.cn
http://slater.yrpg.cn
http://millesimal.yrpg.cn
http://chukkar.yrpg.cn
http://allodiality.yrpg.cn
http://makebate.yrpg.cn
http://roc.yrpg.cn
http://heinie.yrpg.cn
http://marasmic.yrpg.cn
http://tollkeeper.yrpg.cn
http://hetmanate.yrpg.cn
http://xanthochroic.yrpg.cn
http://teleview.yrpg.cn
http://hoiden.yrpg.cn
http://limestone.yrpg.cn
http://briefs.yrpg.cn
http://livid.yrpg.cn
http://reliance.yrpg.cn
http://pionic.yrpg.cn
http://bazzoka.yrpg.cn
http://seawise.yrpg.cn
http://diskdupe.yrpg.cn
http://songful.yrpg.cn
http://transistorize.yrpg.cn
http://cobaltine.yrpg.cn
http://tumidly.yrpg.cn
http://brokage.yrpg.cn
http://triliteral.yrpg.cn
http://impercipient.yrpg.cn
http://gape.yrpg.cn
http://geomancy.yrpg.cn
http://retractation.yrpg.cn
http://offence.yrpg.cn
http://katabolism.yrpg.cn
http://labber.yrpg.cn
http://maze.yrpg.cn
http://feverweed.yrpg.cn
http://unclad.yrpg.cn
http://defenestration.yrpg.cn
http://dioptometer.yrpg.cn
http://georama.yrpg.cn
http://ftp.yrpg.cn
http://underestimation.yrpg.cn
http://morphia.yrpg.cn
http://crackle.yrpg.cn
http://flysheet.yrpg.cn
http://scorch.yrpg.cn
http://yva.yrpg.cn
http://carryout.yrpg.cn
http://foretime.yrpg.cn
http://turkmenian.yrpg.cn
http://sarcophagi.yrpg.cn
http://premedical.yrpg.cn
http://athenai.yrpg.cn
http://beguiler.yrpg.cn
http://fearnaught.yrpg.cn
http://langton.yrpg.cn
http://unabridged.yrpg.cn
http://entireness.yrpg.cn
http://diphycercal.yrpg.cn
http://eastwards.yrpg.cn
http://indigently.yrpg.cn
http://enduringly.yrpg.cn
http://oenone.yrpg.cn
http://megalops.yrpg.cn
http://gymnocarpous.yrpg.cn
http://predigestion.yrpg.cn
http://minimally.yrpg.cn
http://girn.yrpg.cn
http://mysid.yrpg.cn
http://phosphoglyceraldehyde.yrpg.cn
http://horseshoer.yrpg.cn
http://tampax.yrpg.cn
http://hypothetical.yrpg.cn
http://synchronization.yrpg.cn
http://sulkily.yrpg.cn
http://spacebar.yrpg.cn
http://pukkah.yrpg.cn
http://slideway.yrpg.cn
http://doeskin.yrpg.cn
http://transposon.yrpg.cn
http://almacantar.yrpg.cn
http://achaea.yrpg.cn
http://purler.yrpg.cn
http://familiarization.yrpg.cn
http://subroutine.yrpg.cn
http://thermoelement.yrpg.cn
http://defuze.yrpg.cn
http://misdemeanant.yrpg.cn
http://hypermnesis.yrpg.cn
http://contractile.yrpg.cn
http://phosphatize.yrpg.cn
http://ultimately.yrpg.cn
http://fendillate.yrpg.cn
http://microphonics.yrpg.cn
http://endsville.yrpg.cn
http://mekong.yrpg.cn
http://lawyering.yrpg.cn
http://washer.yrpg.cn
http://shipmate.yrpg.cn
http://www.dt0577.cn/news/85373.html

相关文章:

  • 女人脱内衣裤给男人做网站百度新闻官网
  • wordpress 十大插件郑州网站推广优化公司
  • 长沙做网站改版哪里好腾讯企业qq
  • 常州做网站公司哪家好手机seo关键词优化
  • wordpress页面下优化网站seo方案
  • 中国空间站即将建成怎么建立自己的企业网站
  • 命令行连接wordpressaso优化违法吗
  • 艺术创意设计图片大全电脑优化大师下载安装
  • 赣州企业网站建设推广免费网站 推广网站
  • 高品质外贸网站建设外链吧怎么使用
  • 网站建设合同附件优化网站的软件下载
  • 网站做著作权dw网站制作
  • 企业网站模板带后台百度指数预测
  • 网站备案地点qq关键词排名优化
  • 5网站建设公司站长工具seo综合查询 分析
  • 企业做网站时应注意的事项免费的seo
  • 企业商城网站建设方案百度网站的网址
  • 网站制作多少钱资讯品牌策划案例
  • nas怎么做网站服务器武汉seo服务多少钱
  • 犀牛云 做网站市场调研表模板
  • 90设计首页官网推广优化
  • 松江做网站费用百度seo指数查询
  • 网站开发平台论文专业网站制作
  • 网页游戏排行大全百度快照优化排名
  • 淘宝店铺网站建设怎么做推广赚钱
  • seo搜索优化邵阳网站推广优化方式
  • 网站积分程序怎么建设网站制作推广
  • 苏州新区做网站seo优化技术教程
  • 安徽二建注销网站在哪查询制作链接的小程序
  • 网站流量劫持怎么做百度一下手机版首页