春季安全性中的路径安全性 5.



Using org.springframework.security:spring-security-oauth2-resource-server5.1.0.RC2 (via org.springframework.boot:spring-boot-starter-security:2.1.0.M3( 我正在尝试为某些 http 路径启用安全性,但使用基于 JWT 的令牌安全性为其他路径禁用它。

我尝试了以下方法,但它在各种路径和 http 方法上强制实施安全性不一致:

  • 在 GET/帐户上强制实施?名称=检查
  • 在开机自检/帐户上强制执行
  • 未在 GET/customers?name=John 上强制执行
  • 在开机自检/客户上强制执行

为什么它会在 POST 上强制发给/客户?

@Configuration
public class SecurityConfig {
@Autowired
private ReactiveJwtDecoder jwtDecoder;
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
// @formatter:off
http
.authorizeExchange().pathMatchers("/customers").permitAll()
.and()
.authorizeExchange().pathMatchers("/customers/**").permitAll()
.and()
.authorizeExchange().pathMatchers("/accounts/**").authenticated()
.and()
.oauth2ResourceServer()
.jwt().jwtDecoder(this.jwtDecoder);
// @formatter:on
return http.build();
}
}

我通过两件事部分解决了这个问题:

  1. 添加了csrf((.disable(((也许这没关系,因为这些是无状态的REST服务?
  2. 在我发送我提到的身份验证正在强制执行的请求的授权标头之前。 我以为它会跳过授权,即使有标题,但它还是处理了它。

更新的代码:

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
// @formatter:off
http
.csrf()
.disable()
.authorizeExchange().pathMatchers("/customers/**").permitAll()
.and()
.authorizeExchange().pathMatchers("/accounts/**").authenticated()
.and()
.oauth2ResourceServer()
.jwt().jwtDecoder(this.jwtDecoder);
return http.build();
}

最新更新