繁体   English   中英

在 spring 上启用的 csrf 云网关不会在响应 header 中添加 csrf 令牌

[英]csrf enabled on spring cloud gateway does not add the csrf token in the response header

我在 spring 云网关应用程序中启用了 CSRF。 我已经允许登录 api 以便处理对应用程序的第一个请求,并且响应将具有我的前端(角度)使用它的 CSRF 令牌。 但是响应没有任何 csrf 令牌。

下面是我的配置

@Configuration
@EnableWebFluxSecurity
public class NettyConfiguration implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

@Value("${server.max-initial-line-length:65536}")
private int maxInitialLingLength;
@Value("${server.max-http-header-size:65536}")
private int maxHttpHeaderSize;

public void customize(NettyReactiveWebServerFactory container) {
    container.addServerCustomizers(
            httpServer -> httpServer.httpRequestDecoder(
                    httpRequestDecoderSpec -> {
                        httpRequestDecoderSpec.maxHeaderSize(maxHttpHeaderSize);
                        httpRequestDecoderSpec.maxInitialLineLength(maxInitialLingLength);
                        return httpRequestDecoderSpec;
                    }
            )
    );
}


@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    http
            // ...
            .csrf()
            .requireCsrfProtectionMatcher(new NegatedServerWebExchangeMatcher(pathMatchers("/i18n/*","/*","/assets/**","/service/webapi/login")))
            .and().csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()));
    return http.build();
}
}

我已禁用 CSRF 进行登录。 登录有效,但响应在 cookies 中没有 csrf 令牌。因此,我的前端无法获取令牌以发出其他请求。 GET 请求也需要 CSRF 令牌吗? 对于 GET 请求,我也收到“找不到预期的 csrf 令牌”。

添加了以下代码并在响应 header 中添加了令牌。

@Bean
public WebFilter addCsrfTokenFilter() {
    return (exchange, next) -> Mono.just(exchange)
            .flatMap(ex -> ex.<Mono<CsrfToken>>getAttribute(CsrfToken.class.getName()))
            .doOnNext(ex -> {
            })
            .then(next.filter(exchange));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM