繁体   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