【问题标题】:Path security in Spring Security 5Spring Security 5 中的路径安全
【发布时间】:2019-02-25 02:06:16
【问题描述】:

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

我尝试了以下方法,但它在各种路径和 http 方法上执行的安全性不一致:

  • 在 GET /accounts?name=Checking 时强制执行
  • 在 POST /accounts 上强制执行
  • 未在 GET /customers?name=John 上强制执行
  • 在 POST /customers 上强制执行

为什么它会在 POST 到 /customers 时强制执行?

@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();
   }

}

【问题讨论】:

  • 我部分解决了这个问题。

标签: spring-boot spring-security spring-security-oauth2


【解决方案1】:

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

  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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2014-02-05
    • 2016-12-27
    • 2011-10-04
    • 2020-12-03
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多