【问题标题】:UserDetailsService ignored by security configurationUserDetailsS​​ervice 被安全配置忽略
【发布时间】:2019-09-11 11:42:14
【问题描述】:

我已经尝试了 spring webFlux 的安全配置,如文档中所述:https://docs.spring.io/spring-security/site/docs/current/reference/html/jc-webflux.html#explicit-webflux-security-configuration

安全配置忽略 userDetailsS​​erviceBean - 我无法使用 user:pass 登录,但可以使用来自自动配置的凭据登录

UserDetailsServiceAutoConfiguration:

2019-04-22 11:29:24.571  INFO 12343 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: ff00a80a-d810-43d6-9c89-e861eb1bed96

我的 pom.xml(片段):

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

我的安全配置:

@EnableWebFluxSecurity
@EnableWebSecurity
@Configuration
public class WebfluxSecurityConfig {

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        http
        .authorizeExchange()
        .anyExchange().authenticated()
        .and()
        .httpBasic().and()
        .formLogin();
        return http.build();
    }


    @Bean
    public MapReactiveUserDetailsService userDetailsService() {
         UserDetails user = User.withDefaultPasswordEncoder()
                 .username("user")
                 .password("pass")
                 .roles("USER")
                 .build();
        return new MapReactiveUserDetailsService(user);
    }

}
  1. 为什么安全配置会忽略 userDetailsS​​ervice?
  2. spring-security 文档中是否有错误?

【问题讨论】:

    标签: spring-boot spring-security spring-webflux


    【解决方案1】:
    1. 删除@EnableWebSecurity 注释。它用于 Servlet 应用程序,不适用于 WebFlux。

    2. 您也可以考虑在您的 WebFlux 配置中定义一个 UserDetailsRepositoryReactiveAuthenticationManager 类型的 bean;例如:

      @Bean
      public ReactiveAuthenticationManager authenticationManager(ReactiveUserDetailsService detailsService) {
           return new UserDetailsRepositoryReactiveAuthenticationManager(detailsService);
      }
      

    在您的情况下,@EnableWebSecurity 注释很可能配置了 InMemoryUserDetailsManager 类型的 bean,它是 ReactiveUserDetailsManager 的非反应变体。

    注意:如果您打算仅使用 WebFlux,则可以从 POM 中删除以下内容:spring-boot-starter-tomcatspring-boot-starter-web

    【讨论】:

    • 1. “删除@EnableWebSecurity 注释” - 无效
    • 2. “你也可以考虑定义一个 UserDetailsRepositoryReactiveAuthenticationManager 类型的 bean”——没有效果
    • 3.排除依赖 org.springframework.bootspring-boot-starter-web - 工作。问题更新:如果我想在一个应用程序中使用 spring web mvc 和 spring webflux 如何配置安全性(稍后我将尝试使用两种安全配置 - 一个用于 web mvc,另一个用于 webFlux)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2015-08-07
    • 2011-06-22
    • 1970-01-01
    • 2013-11-19
    相关资源
    最近更新 更多