我尝试过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);
}
}发布于 2019-04-22 09:59:02
@EnableWebSecurity注释。它用于Servlet应用程序,而不是应用于WebFlux。UserDetailsRepositoryReactiveAuthenticationManager配置中定义一个类型为WebFlux的bean;如下所示:
@Bean public authenticationManager(ReactiveUserDetailsService ReactiveAuthenticationManager detailsService) {返回新的UserDetailsRepositoryReactiveAuthenticationManager(detailsService);}在您的示例中,最有可能的情况是,@EnableWebSecurity注释配置InMemoryUserDetailsManager类型的bean,它是ReactiveUserDetailsManager的非反应性变体。
注意:如果您计划只使用WebFlux,可以从POM中删除以下内容:spring-boot-starter-tomcat和spring-boot-starter-web
https://stackoverflow.com/questions/55791711
复制相似问题