简体   繁体   English

Spring Cloud Gateway 上启用的 CSRF 不允许登录 api POST 休息调用

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

I have a api gateway to my rest api micro service.我有一个到我的 rest api 微服务的 api 网关。 the gateway is implemented using the spring cloud gateway project.网关使用spring cloud gateway项目实现。 I want to enable CSRF on the api gateway.我想在 api 网关上启用 CSRF。 I used the below code provided in the documentation to enable it.我使用文档中提供的以下代码来启用它。

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

To log in to my app, the GUI makes a POST api request to my rest web service, which goes through the api gateway.为了登录我的应用程序,GUI 向我的 rest web 服务发出 POST api 请求,该请求通过 api 网关。 This call is blocked with the message "An expected CSRF token cannot be found".此调用因消息“找不到预期的 CSRF 令牌”而被阻止。

So I wanted to permit only the login request and hence made the changes as below.因此,我只想允许登录请求,因此进行了如下更改。

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

now when I restart my application, it does not go to my landing page, instead provides its own log in page.现在当我重新启动我的应用程序时,它不会转到我的登录页面,而是提供自己的登录页面。 在此处输入图像描述

Below is my entire configuration.下面是我的整个配置。 I have angular running my GUI.我有角度运行我的 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();
}

} }

Check Disable authentication and csrf for a given path in Spring Weblux? 在 Spring Weblux 中检查给定路径的禁用身份验证和 csrf? . .

The gist of it is that authorizeRequests() does not care about csrf.它的要点是 authorizeRequests() 不关心 csrf。 You should use requireCsrfProtectionMatcher instead to which urls would be subject to CORS verification您应该使用 requireCsrfProtectionMatcher 而不是哪些网址需要接受 CORS 验证

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

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