安全配置忽略userDetailsServiceBean-我无法使用user:pass登录,但可以使用自动配置的凭据登录
<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>
我的pom.xml(片段):
@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);
}
}
我的安全配置:
class ID {
String title;
String content;
String timestamp;
Map<String, dynamic> toJson() {
final Map<String, dynamic> ID = new Map<String, dynamic>();
ID['title'] = this.title;
ID['content'] = this.content;
ID['timestamp'] = this.timestamp;
return ID;
}
}
答案 0 :(得分:0)
删除@EnableWebSecurity
批注。它用于Servlet应用程序,而不应用于WebFlux。
您还可以考虑在WebFlux配置中定义类型为UserDetailsRepositoryReactiveAuthenticationManager
的bean。例如以下内容:
@Bean
public ReactiveAuthenticationManager authenticationManager(ReactiveUserDetailsService detailsService) {
return new UserDetailsRepositoryReactiveAuthenticationManager(detailsService);
}
在您的情况下,最可能是@EnableWebSecurity
注释配置了InMemoryUserDetailsManager
类型的Bean,它是ReactiveUserDetailsManager
的非反应变量。
注意::如果您打算仅使用WebFlux,则可以从POM中删除以下内容:spring-boot-starter-tomcat
和spring-boot-starter-web