Spring Security UserDetailsService被安全配置忽略

68de4m5k  于 2023-10-20  发布在  Spring
关注(0)|答案(2)|浏览(193)

我尝试了spring webFlux的安全配置,如文档中所述:https://docs.spring.io/spring-security/site/docs/current/reference/html/jc-webflux.html#explicit-webflux-security-configuration
安全配置忽略userDetailsServiceBean - i无法使用user:pass登录,但可以使用autoconfigurated中的凭据登录

UserDetailsServiceAutoConfiguration:

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

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

My pom.xml(fragment):

<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.为什么安全配置忽略userDetailsService?

  1. spring-security文档中是否有错误?
n7taea2i

n7taea2i1#

1.删除@EnableWebSecurity注解。它用于Servlet应用程序,而不应用于WebFlux。
1.你也可以考虑在WebFlux配置中定义一个UserDetailsRepositoryReactiveAuthenticationManager类型的bean;例如:

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

在您的例子中,@EnableWebSecurity注解很可能配置了一个InMemoryUserDetailsManager类型的bean,它是ReactiveUserDetailsManager的非React性变体。

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

vecaoik1

vecaoik12#

我遇到了同样的问题,我通过将WebSecurityConfig类移动到同一个包来解决这个问题,在这个包中,我有运行Sping Boot 应用程序的类(@SpringBootApplication)。

相关问题