Java SpringWebFlux安全性允许除一个Url外的所有Url
我想允许在特定控制器中定义除一个以外的所有URL 假设我的控制器公开了3个基本urlJava SpringWebFlux安全性允许除一个Url外的所有Url,java,spring,spring-security,spring-webflux,Java,Spring,Spring Security,Spring Webflux,我想允许在特定控制器中定义除一个以外的所有URL 假设我的控制器公开了3个基本url/users “/”列出所有用户 “/{id}”按id列出用户 “/admin”获取用户的管理员级别详细信息 我想允许除最后一个用户外的任何用户访问1和2个URL 我一直在做这样的事情 @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http.authorizeExchange
/users
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange()
.pathMatchers("/users/**")
.permitAll()
.pathMatchers("/users/admin")
.hasRole("ADMIN")
.anyExchange()
.authenticated()
.and()
.httpBasic()
.and()
.formLogin();
return http.build();
}
但它不起作用。我也试过相反的方法,即
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange()
.pathMatchers("/users/admin")
.hasRole("ADMIN")
.anyExchange()
.authenticated()
.pathMatchers("/users/**")
.permitAll()
.and()
.httpBasic()
.and()
.formLogin();
return http.build();
}
由于anyExchange()
已经注册,因此无法联系到下一个路径匹配器
。我找到了解决方案。
anyExchange()
和authenticated()
导致问题anyExchange()
不允许进一步添加任何路径匹配程序,而authenticated()
正在使整个应用程序安全,从而导致每个url提示进行身份验证
删除这两个选项是有效的
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange()
.pathMatchers("/users/admin/**")
.hasRole("ADMIN")
.pathMatchers("/**").permitAll()
.and().httpBasic();
return http.build();
}