当前位置:   article > 正文

springboot2 webflux集成spring security做权限登陆校验

spring boot webflux oauth2login配置

一、主要pom依赖包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-security</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-data-redis</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.commons</groupId>
  15. <artifactId>commons-pool2</artifactId>
  16. <version>2.5.0</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-webflux</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.session</groupId>
  24. <artifactId>spring-session-core</artifactId>
  25. </dependency>
  26. <!-- 使用redis存储session -->
  27. <dependency>
  28. <groupId>org.springframework.session</groupId>
  29. <artifactId>spring-session-data-redis</artifactId>
  30. </dependency>

二、application root配置

  1. @SpringBootApplication
  2. //开启webflux
  3. @EnableWebFlux
  4. //使用redis存储session信息
  5. @EnableRedisWebSession
  6. public class AuthorityParentApplication {
  7. public static void main(String[] args) {
  8. Hooks.onOperatorDebug();
  9. SpringApplication.run(AuthorityParentApplication.class, args);
  10. }
  11. }

三、权限校验配置

先把主要的配置类信息分别说一下,后面会有组合后的configuration类,类名为:SecurityConfig

1、过滤器配置

webflux security配置调用链信息的主类为:ServerHttpSecurity,里面有OAuth2,formlogin等登陆方式的默认配置

管理系统的话我们启用FormLoginSpec即刻,就是传统的cookie配session的登陆验证机制

  1. /**
  2. * 此处的代码会放在SecurityConfig类中,此处只是摘要下,里面的handler在下文会详细介绍
  3. * @param http
  4. * @return
  5. */
  6. @Bean
  7. SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
  8. ServerHttpSecurity.FormLoginSpec formLoginSpec = http.formLogin();
  9. formLoginSpec.authenticationSuccessHandler(createAuthenticationSuccessHandler())
  10. .loginPage("/login")
  11. .authenticationFailureHandler(createAuthenticationFailureHandler());
  12. return formLoginSpec.and()
  13. .csrf().disable()
  14. .httpBasic().disable()
  15. .authorizeExchange()
  16. .pathMatchers(AUTH_WHITELIST).permitAll()
  17. .anyExchange().authenticated()
  18. .and().build();
  19. }

最后调用ServerHttpSecurity的build方法,从源码中可以查看构造的filter调用链,及配置信息等

  1. public SecurityWebFilterChain build() {
  2. if (this.built != null) {
  3. throw new IllegalStateException("This has already been built with the following stacktrace. " + buildToString());
  4. }
  5. this.built = new RuntimeException("First Build Invocation").fillInStackTrace();
  6. if (this.headers != null) {
  7. this.headers.configure(this);
  8. }
  9. WebFilter securityContextRepositoryWebFilter = securityContextRepositoryWebFilter();
  10. if (securityContextRepositoryWebFilter != null) {
  11. this.webFilters.add(securityContextRepositoryWebFilter);
  12. }
  13. if (this.httpsRedirectSpec != null) {
  14. this.httpsRedirectSpec.configure(this);
  15. }
  16. if (this.csrf != null) {
  17. this.csrf.configure(this);
  18. }
  19. if (this.cors != null) {
  20. this.cors.configure(this);
  21. }
  22. if (this.httpBasic != null) {
  23. this.httpBasic.authenticationManager(this.authenticationManager);
  24. this.httpBasic.configure(this);
  25. }
  26. if (this.formLogin != null) {
  27. this.formLogin.authenticationManager(this.authenticationManager);
  28. if (this.securityContextRepository != null) {
  29. this.formLogin.securityContextRepository(this.securityContextRepository);
  30. }
  31. this.formLogin.configure(this);
  32. }
  33. if (this.oauth2Login != null) {
  34. this.oauth2Login.configure(this);
  35. }
  36. if (this.resourceServer != null) {
  37. this.resourceServer.configure(this);
  38. }
  39. if (this.client != null) {
  40. this.client.configure(this);
  41. }
  42. this.loginPage.configure(this);
  43. if (this.logout != null) {
  44. this.logout.configure(this);
  45. }
  46. this.requestCache.configure(this);
  47. this.addFilterAt(new SecurityContextServerWebExchangeWebFilter(), SecurityWebFiltersOrder.SECURITY_CONTEXT_SERVER_WEB_EXCHANGE);
  48. if (this.authorizeExchange != null) {
  49. ServerAuthenticationEntryPoint authenticationEntryPoint = getAuthenticationEntryPoint();
  50. ExceptionTranslationWebFilter exceptionTranslationWebFilter = new ExceptionTranslationWebFilter();
  51. if (authenticationEntryPoint != null) {
  52. exceptionTranslationWebFilter.setAuthenticationEntryPoint(
  53. authenticationEntryPoint);
  54. }
  55. ServerAccessDeniedHandler accessDeniedHandler = getAccessDeniedHandler();
  56. if (accessDeniedHandler != null) {
  57. exceptionTranslationWebFilter.setAccessDeniedHandler(
  58. accessDeniedHandler);
  59. }
  60. this.addFilterAt(exceptionTranslationWebFilter, SecurityWebFiltersOrder.EXCEPTION_TRANSLATION);
  61. this.authorizeExchange.configure(this);
  62. }
  63. AnnotationAwareOrderComparator.sort(this.webFilters);
  64. List<WebFilter> sortedWebFilters = new ArrayList<>();
  65. this.webFilters.forEach( f -> {
  66. if (f instanceof OrderedWebFilter) {
  67. f = ((OrderedWebFilter) f).webFilter;
  68. }
  69. sortedWebFilters.add(f);
  70. });
  71. //sortedWebFilters中保存了登陆权限校验的所有filter信息
  72. sortedWebFilters.add(0, new ServerWebExchangeReactorContextWebFilter());
  73. return new MatcherSecurityWebFilterChain(getSecurityMatcher(), sortedWebFilters);
  74. }

2、自定义用户信息获取方式

获取用户信息的接口定义是:ReactiveUserDetailsService,官网的实现是:MapReactiveUserDetailsService,给出demo中是代码写死用户名密码、角色,并用此类存储。但实际中更多的从数据库中获取配置的用户角色信息,所以我们实现ReactiveUserDetailsService接口,自定义从mysql中获取用户信息的类,代码如下:

  1. import com.sisheng.authority.common.enums.ErrorEnum;
  2. import com.sisheng.authority.common.exception.BusinessException;
  3. import com.sisheng.authority.repository.user.entity.SystemUser;
  4. import com.sisheng.authority.servi
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/68912
推荐阅读
相关标签
  

闽ICP备14008679号