【问题标题】:Disable authentication and csrf for a given path in Spring Weblux?在 Spring Webflux 中为给定路径禁用身份验证和 csrf?
【发布时间】:2019-06-21 20:48:29
【问题描述】:

我想为整个应用程序启用 oauth2,除了一个 url。

我的配置:

@EnableWebFluxSecurity
class SecurityConfig {

    @Bean
    fun securityWebFilterChain(http: ServerHttpSecurity) =
        http
            .authorizeExchange()
            .pathMatchers("/devices/**/register").permitAll()
            .and()
            .oauth2Login().and()
            .build()
}

application.yml:

spring.security.oauth2.client.registration.google.client-id: ...
spring.security.oauth2.client.registration.google.client-secret: ...

所有路径都受到 oauth2 的保护,但问题是当我调用允许 /devices/123/register 的端点时,我得到的响应是:

CSRF Token has been associated to this client

我需要以其他方式配置此路径吗?

【问题讨论】:

    标签: spring-security kotlin spring-security-oauth2 spring-webflux csrf-protection


    【解决方案1】:

    permitAll 只是关于权限的声明——所有典型的 Web 应用程序漏洞仍然像 XSS 和 CSRF 一样得到缓解。

    如果你试图表明 /devices/**/register 应该被 Spring Security 完全忽略,那么你可以这样做:

    http
        .securityMatcher(new NegatedServerWebExchangeMatcher(
            pathMatchers("/devices/**/register")))
        ... omit the permitAll statement
    

    但是,如果您仍然希望该端点获得安全响应标头,而不是 CSRF 保护,那么您可以这样做:

    http
        .csrf()
            .requireCsrfProtectionMatcher(new NegatedServerWebExchangeMatcher(
                pathMatchers("/devices/**/register")))
        ... keep the permitAll statement
    

    【讨论】:

      猜你喜欢
      • 2014-03-18
      • 2020-06-19
      • 2018-05-01
      • 1970-01-01
      • 2019-03-18
      • 2019-11-07
      • 1970-01-01
      • 2018-09-07
      • 2020-10-07
      相关资源
      最近更新 更多