我尝试了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?
- spring-security文档中是否有错误?
2条答案
按热度按时间n7taea2i1#
1.删除
@EnableWebSecurity
注解。它用于Servlet应用程序,而不应用于WebFlux。1.你也可以考虑在WebFlux配置中定义一个
UserDetailsRepositoryReactiveAuthenticationManager
类型的bean;例如:在您的例子中,
@EnableWebSecurity
注解很可能配置了一个InMemoryUserDetailsManager
类型的bean,它是ReactiveUserDetailsManager
的非React性变体。**注意:**如果您只打算使用WebFlux,则可以从POM中删除以下内容:
spring-boot-starter-tomcat
和spring-boot-starter-web
vecaoik12#
我遇到了同样的问题,我通过将WebSecurityConfig类移动到同一个包来解决这个问题,在这个包中,我有运行Sping Boot 应用程序的类(@SpringBootApplication)。