![](/img/trans.png)
[英]CSRF enabled on spring cloud gateway does not allow login api POST rest call
[英]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.