当前位置:   article > 正文

Spring-security在SpringMvc中的使用_securitywebfiltersorder.authentication的值

securitywebfiltersorder.authentication的值
Spring-security是spring中的校验流程,有SpringMVC配置和SpringFlux配置两种模式,关于使用方式,我们在这里说下

1、SpirngMVC中的Security配置

在SpirngMVC中的Security配置,我们需要有一个类继承WebSecurityConfigurerAdapter类,在里面可以配置自己需要的bean和拦截属性,更多详细介绍请看官方文档,这里只是简单做下介绍

  1. @Configuration
  2. @EnableWebSecurity
  3. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  4. @Bean
  5. public UsernamePasswordAuthFilter usernamePasswordAuthFilter() {
  6. return new UsernamePasswordAuthFilter(this.getApplicationContext());
  7. }
  8. @Bean
  9. public Oauth2LoginAuthenticationFilter Oauth2LoginAuthenticationFilter() {
  10. return new Oauth2LoginAuthenticationFilter(this.getApplicationContext());
  11. }
  12. @Override
  13. public void configure(HttpSecurity http) throws Exception {
  14. http
  15. .authorizeRequests()
  16. // .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
  17. // 对于获取token的rest api要允许匿名访问
  18. .antMatchers("/auth_center/auth/**").permitAll()
  19. .antMatchers("/auth_center/oauth2/**").permitAll()
  20. .antMatchers("/auth_center/druid/**").permitAll()
  21. .antMatchers(HttpMethod.GET, "/").permitAll()
  22. .antMatchers(HttpMethod.HEAD).permitAll()
  23. // 除上面外的所有请求全部需要鉴权认证
  24. .anyRequest().authenticated().and().formLogin().disable()
  25. .httpBasic().disable()
  26. .openidLogin().disable()
  27. .logout().disable()
  28. .rememberMe().disable()
  29. // 由于使用的是JWT,我们这里不需要csrf
  30. .csrf().disable()
  31. // 基于token,所以不需要session
  32. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  33. //http.addFilterBefore(userCenterFilterSecurityInterceptor, FilterSecurityInterceptor.class);
  34. // 添加JWT filter
  35. http.addFilterAt(usernamePasswordAuthFilter(), UsernamePasswordAuthenticationFilter.class);
  36. http.addFilterAt(Oauth2LoginAuthenticationFilter(), OAuth2LoginAuthenticationFilter.class);
  37. // 禁用缓存
  38. http.headers().cacheControl();
  39. }
  40. }

2、Spring-security关于在WebFlux项目中的配置

Spring-security关于在WebFlux项目中的配置,与在SpringMVC中的注解是不同的,为@EnableWebFluxSecurity,使用方式如下,可以自己配置Filter和权限属性:

  1. @EnableWebFluxSecurity
  2. public class WebfluxSecurityConfig {
  3. /** **/
  4. @Autowired
  5. private AuthReactiveAuthenticationManager reactiveAuthenticationManager;
  6. @Autowired
  7. private ServerHttpAuthenticationConverter serverHttpAuthenticationConverter;
  8. @Autowired
  9. public RequiresServerWebExchangeMatcher serverWebExchangeMatcher;
  10. @Resource(name="delegatingAuthorizationManager")
  11. public DelegatingReactiveAuthorizationManager delegatingAuthorizationManager;
  12. @Bean
  13. public ServerAuthenticationFailureHandler serverAuthenticationFailureHandler(){
  14. return new ServerAuthenticationEntryPointFailureHandler(serverAuthenticationEntryPoint());
  15. }
  16. @Bean
  17. public ServerAuthenticationEntryPoint serverAuthenticationEntryPoint(){
  18. return new RestServerAuthenticationEntryPoint();
  19. }
  20. /**
  21. * 身份认证
  22. * @return
  23. */
  24. public AuthenticationWebFilter authenticationWebFilter(){
  25. AuthenticationWebFilter authenticationWebFilter= new AuthenticationWebFilter(reactiveAuthenticationManager);
  26. authenticationWebFilter.setRequiresAuthenticationMatcher(serverWebExchangeMatcher);
  27. authenticationWebFilter.setAuthenticationConverter(serverHttpAuthenticationConverter);
  28. authenticationWebFilter.setAuthenticationFailureHandler(serverAuthenticationFailureHandler());
  29. return authenticationWebFilter;
  30. }
  31. /**
  32. * 访问授权
  33. * @return
  34. */
  35. public AuthorizationWebFilter authorizationWebFilter(){
  36. return new AuthorizationWebFilter(delegatingAuthorizationManager);
  37. }
  38. @Bean
  39. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  40. http.authorizeExchange()
  41. .anyExchange().authenticated()
  42. .and().csrf().disable()
  43. .httpBasic().disable()
  44. .formLogin().disable()
  45. .logout().disable()
  46. .requestCache().disable();
  47. http.addFilterAt(authenticationWebFilter(), SecurityWebFiltersOrder.FORM_LOGIN);
  48. http.addFilterAt(authorizationWebFilter(),SecurityWebFiltersOrder.AUTHENTICATION);
  49. return http.build();
  50. }
  51. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/68915
推荐阅读
相关标签
  

闽ICP备14008679号