首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >被安全配置忽略的UserDetailsService

被安全配置忽略的UserDetailsService
EN

Stack Overflow用户
提问于 2019-04-22 08:53:02
回答 1查看 940关注 0票数 2

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

安全配置忽略userDetailsServiceBean -我不能使用user:pass登录,但可以使用自动配置的凭据登录。

代码语言:javascript
复制
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 (片段):

代码语言:javascript
复制
<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>

我的安全配置:

代码语言:javascript
复制
@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. 为什么安全配置忽略userDetailsService?
  2. 春季安全文档中有错误吗?
EN

回答 1

Stack Overflow用户

发布于 2019-04-22 09:59:02

  1. 删除@EnableWebSecurity注释。它用于Servlet应用程序,而不是应用于WebFlux。
  2. 您还可以考虑在您的UserDetailsRepositoryReactiveAuthenticationManager配置中定义一个类型为WebFlux的bean;如下所示: @Bean public authenticationManager(ReactiveUserDetailsService ReactiveAuthenticationManager detailsService) {返回新的UserDetailsRepositoryReactiveAuthenticationManager(detailsService);}

在您的示例中,最有可能的情况是,@EnableWebSecurity注释配置InMemoryUserDetailsManager类型的bean,它是ReactiveUserDetailsManager的非反应性变体。

注意:如果您计划只使用WebFlux,可以从POM中删除以下内容:spring-boot-starter-tomcatspring-boot-starter-web

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55791711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档