UserDetailsService игнорируется конфигурацией безопасности - PullRequest
1 голос
/ 22 апреля 2019

Я попытался настроить безопасность для Spring WebFlux, как описано в документации: https://docs.spring.io/spring-security/site/docs/current/reference/html/jc-webflux.html#explicit-webflux-security-configuration

Конфигурация безопасности игнорирует userDetailsServiceBean - я не могу войти с 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. Почему конфигурация безопасности игнорирует userDetailsService?
  2. Есть ли ошибка в документации по безопасности пружин?

1 Ответ

0 голосов
/ 22 апреля 2019
  1. Удалить аннотацию @EnableWebSecurity. Он используется для приложений сервлетов и не применяется в WebFlux.

  2. Вы также можете рассмотреть определение bean-компонента типа UserDetailsRepositoryReactiveAuthenticationManager в своей конфигурации WebFlux; такие как следующее:

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

В вашем случае, наиболее вероятно, аннотация @EnableWebSecurity настраивает компонент типа InMemoryUserDetailsManager, который является нереактивным вариантом ReactiveUserDetailsManager.

ПРИМЕЧАНИЕ: Вы можете удалить из POM следующее, если вы планируете использовать только WebFlux: spring-boot-starter-tomcat и spring-boot-starter-web

...