1. Thư Văn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> |
2. Quảng chí
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailServiceImpl userDetailsService; @Autowired private PasswordEncoder passwordEncoder; @Override protected void configure(HttpSecurity http) throws Exception { http //HTTP Basic authentication .httpBasic() // .and() // .sessionManagement() // .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() .anyRequest() .authenticated() .and() .csrf().disable() .formLogin().disable(); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Autowired public void globalUserDetails(final AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Service public class UserDetailServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) { UserEntity userEntity = userRepository.findByName(username) .orElseThrow(() -> new UsernameNotFoundException("username not found")); return new UserPrincipal(userEntity.getId(), userEntity.getName(), userEntity.getPassword(), new ArrayList<>()); } } |
1 2 3 4 5 6 7 8 9 10 | @Data @Entity @Table(name = "users") public class UserEntity { @Id private Integer id; private String name; private String password; } |
1 2 3 4 5 6 7 8 9 10 11 12 | @Data public class UserPrincipal extends User { private Integer id; private String username; public UserPrincipal(Integer id, String username, String password, Collection<? extends GrantedAuthority> authorities) { super(username, password, authorities); this.id = id; this.username = username; } } |
1 2 3 4 5 6 7 8 9 10 | @ControllerAdvice @Slf4j public class AuthenSecurityControllerAdvice { @ModelAttribute public UserPrincipal customPrincipal(Authentication a) { return (UserPrincipal)(a == null ? null : a.getPrincipal()); } } |
1 2 3 4 5 6 7 8 9 10 11 | @RestController @RequestMapping @Log4j2 public class TestController { @GetMapping("/test") public String test(@ModelAttribute UserPrincipal principal) { log.info("User login information" + principal); return "oke"; } } |
File application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # SPRING spring: # MYSQL CONFIG datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/auth_server?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true username: root password: root # JPA CONFIG jpa: properties: hibernate: dialect: org.hibernate.dialect.MySQLDialect format_sql: true |
** tập lệnh databse auth_server **
1 2 3 4 5 6 7 8 9 10 11 12 13 | CREATE DATABASE `auth_server` DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `password` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; LOCK TABLES `users` WRITE; INSERT INTO `users` VALUES (1,'noname','$2y$12$ivHcnAeTgcLA/fRmVnv.OuuqTDEqIOc9NcppprLPVVXvGRdJJz8bu'),(2,'noname2','$2y$12$ivHcnAeTgcLA/fRmVnv.OuuqTDEqIOc9NcppprLPVVXvGRdJJz8bu'); UNLOCK TABLES; |
3. Kiểm tra
Chạy yêu cầu lên và yêu cầu
1 2 3 4 5 6 | curl -X GET http://localhost:8080/test -H 'Cache-Control: no-cache' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Postman-Token: 994a23e3-38e6-42b2-ad1b-32a331450560' |
Kết cấu cơ bản không thể yêu cầu và phản ứng Xác thực cơ bản, xác thực cơ bản Tên người dùng, mật khẩu ( trong người dùng )
Các bạn có ý tưởng trong tiêu đề tab
Kết qua like các bạn have nhìn thấy our Đà yêu cầu been api / kiểm tra
Sau khi bạn yêu cầu bạn yêu cầu bạn đăng nhập phiên họp
Phần còn lại của bạn