簡體   English   中英

Spring Cloud Gateway 上啟用的 CSRF 不允許登錄 api POST 休息調用

[英]CSRF enabled on spring cloud gateway does not allow login api POST rest call

我有一個到我的 rest api 微服務的 api 網關。 網關使用spring cloud gateway項目實現。 我想在 api 網關上啟用 CSRF。 我使用文檔中提供的以下代碼來啟用它。

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    http
            // ...
            .csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()));
    return http.build();
}

為了登錄我的應用程序,GUI 向我的 rest web 服務發出 POST api 請求,該請求通過 api 網關。 此調用因消息“找不到預期的 CSRF 令牌”而被阻止。

因此,我只想允許登錄請求,因此進行了如下更改。

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    http
            // ...
            .csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()))
            .authorizeExchange().pathMatchers("/login")
            .permitAll();
    return http.build();
}

現在當我重新啟動我的應用程序時,它不會轉到我的登錄頁面,而是提供自己的登錄頁面。 在此處輸入圖像描述

下面是我的整個配置。 我有角度運行我的 GUI。

@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(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()))
            .authorizeExchange().pathMatchers("/login")
            .permitAll();
    return http.build();
}

}

在 Spring Weblux 中檢查給定路徑的禁用身份驗證和 csrf? .

它的要點是 authorizeRequests() 不關心 csrf。 您應該使用 requireCsrfProtectionMatcher 而不是哪些網址需要接受 CORS 驗證

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM