Spring Webflux 安全允许除一个外的所有 Url
Spring Webflux Security Allow All But One Url
我想允许在特定控制器中定义的所有 url,但一个除外。
假设我的控制器公开了 3 个 url 基数 url /users
- "/" 列出所有用户
- "/{id}" 按 id 列出用户
- "/admin" 获取用户的管理员级别详细信息。
我想允许除最后一个用户之外的任何用户访问 1 和 2 url。
我一直在做这样的事情
@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()
已经注册,下一个 pathMatcher
无法到达。
我找到了解决方案。
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();
}
我想允许在特定控制器中定义的所有 url,但一个除外。
假设我的控制器公开了 3 个 url 基数 url /users
- "/" 列出所有用户
- "/{id}" 按 id 列出用户
- "/admin" 获取用户的管理员级别详细信息。
我想允许除最后一个用户之外的任何用户访问 1 和 2 url。
我一直在做这样的事情
@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()
已经注册,下一个 pathMatcher
无法到达。
我找到了解决方案。
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();
}