当前位置:   article > 正文

Spring Security 5.7.x及更高版本中配置多个AuthenticationProvider_高版本security 配置

高版本security 配置

SpringBoot2.7.x版本中SpringSecurity账号和手机验证码登录多重认证实践

SecurityConfig配置

  1. @EnableWebSecurity
  2. @EnableGlobalMethodSecurity(prePostEnabled = true)
  3. @Configuration
  4. public class SecurityConfig {
  5. @Autowired
  6. private AuthenticationEntryPointImpl authenticationEntryPoint;
  7. @Autowired
  8. private RestfulAccessDeniedHandler accessDeniedHandler;
  9. @Autowired
  10. private UserDetailsService userDetailsService;
  11. @Autowired
  12. private JwtAuthenticationFilter jwtAuthenticationFilter;
  13. @Autowired
  14. private SmsAuthenticationProvider smsAuthenticationProvider;
  15. /**
  16. * 访问路径白名单
  17. */
  18. private static final String[] WHITE_PATH = new String[] { "/login/xx", "/login", "/register/**",
  19. "/send/sms/**", };
  20. /**
  21. * get方式访问路径白名单
  22. */
  23. private static final String[] GET_WHITE_PATH = new String[] { "/user/list" };
  24. private DaoAuthenticationProvider daoAuthenticationProvider() {
  25. DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
  26. daoAuthenticationProvider.setPasswordEncoder(new BCryptPasswordEncoder());
  27. daoAuthenticationProvider.setUserDetailsService(userDetailsService);
  28. return daoAuthenticationProvider;
  29. }
  30. @Bean
  31. public AuthenticationManager authenticationManager() throws Exception {
  32. ProviderManager authenticationManager = new ProviderManager(
  33. Arrays.asList(smsAuthenticationProvider, daoAuthenticationProvider()));
  34. // 不擦除认证密码
  35. authenticationManager.setEraseCredentialsAfterAuthentication(false);
  36. return authenticationManager;
  37. }
  38. @Bean
  39. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  40. return http
  41. .csrf().disable().cors().and()
  42. .logout().disable().sessionManagement()
  43. .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  44. .and()
  45. .exceptionHandling()
  46. .authenticationEntryPoint(authenticationEntryPoint)
  47. .accessDeniedHandler(accessDeniedHandler)
  48. .and()
  49. .authorizeRequests(authorize -> authorize
  50. .antMatchers(WHITE_PATH).permitAll()
  51. .antMatchers(HttpMethod.GET, GET_WHITE_PATH).permitAll()
  52. .anyRequest().authenticated())
  53. .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
  54. .build();
  55. }
  56. @Bean
  57. public BCryptPasswordEncoder passwordEncoder() {
  58. return new BCryptPasswordEncoder();
  59. }
  60. }

JWT认证过滤器

  1. /**
  2. * JWT认证过滤器
  3. *
  4. * @author admin
  5. *
  6. */
  7. @Component
  8. public class JwtAuthenticationFilter extends OncePerRequestFilter {
  9. @Autowired
  10. private UserDetailsService userDetailsService;
  11. @Override
  12. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
  13. throws IOException, ServletException {
  14. String token = request.getHeader(Constants.TOKEN_HEADER);
  15. if (StrUtil.isEmpty(token) || !JwtTokenUtil.validateToken(token)) {
  16. chain.doFilter(request, response);
  17. return;
  18. }
  19. String username = JwtTokenUtil.getUsername(token);
  20. if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
  21. UserDetails userDetails = userDetailsService.loadUserByUsername(username);
  22. if (userDetails == null) {
  23. chain.doFilter(request, response);
  24. return;
  25. }
  26. UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
  27. userDetails, null, userDetails.getAuthorities());
  28. usernamePasswordAuthenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
  29. SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
  30. }
  31. chain.doFilter(request, response);
  32. }
  33. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号