简体   繁体   English

Spring 启动 2.2.4.RELEASE 在 Origin 被定义为 webapp 自己的主机名以外的任何内容时返回 403 获取请求

[英]Spring boot 2.2.4.RELEASE returning 403 for GET request when Origin is defined as anything other than the webapp's own hostname

Good morning everyone, When enabling CORS on a subset of endpoints in my Spring Boot application (which we deploy as an war), the preflight works perfectly, however when the actual GET request is made, the webapp returns a 403 with no logging indicating why it was forbidden (even with trace logging enabled in logback.xml) with the following being the last set of logs for a given request.大家早上好,当在我的 Spring 引导应用程序(我们将其部署为战争)的端点子集上启用 CORS 时,预检工作正常,但是当发出实际的 GET 请求时,webapp 返回一个 403,没有记录说明原因它被禁止(即使在 logback.xml 中启用了跟踪日志记录),以下是给定请求的最后一组日志。

[https-jsse-nio-9643-exec-3] o.s.s.w.a.i.FilterSecurityInterceptor    : Authorization successful
[https-jsse-nio-9643-exec-3] o.s.s.w.a.i.FilterSecurityInterceptor    : RunAsManager did not change Authentication object
[https-jsse-nio-9643-exec-3] o.s.security.web.FilterChainProxy        : /path/myendpoint reached end of additional filter chain; proceeding with original chain
[https-jsse-nio-9643-exec-3] o.s.w.f.CommonsRequestLoggingFilter      : Before request [GET /path/myendpoint]
[https-jsse-nio-9643-exec-3] o.s.w.f.CommonsRequestLoggingFilter      : After request [GET /path/myendpoint]
[https-jsse-nio-9643-exec-3] o.s.s.w.a.ExceptionTranslationFilter     : Chain processed normally
[https-jsse-nio-9643-exec-3] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
[https-jsse-nio-9643-exec-3] o.s.b.w.s.f.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1b766a09

The only exception is when I omit the Origin Header from the request or set it to the exact same value as the Spring boot host, in which case the request successfully completes.唯一的例外是当我从请求中省略 Origin Header 或将其设置为与 Spring 引导主机完全相同的值时,在这种情况下请求成功完成。

Here is my WebSecurityConfigurerAdapter configuration这是我的 WebSecurityConfigurerAdapter 配置


@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Collections.singletonList("*"));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
    configuration.setAllowedHeaders(Arrays.asList("X-Requested-With", "Origin", "Content-Type", "Accept", "Authorization"));
    configuration.setAllowCredentials(true);
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/path/**", configuration);
    return source;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors(withDefaults())
    .authorizeRequests()
            .mvcMatchers("/**").authenticated()
            .and()
            .exceptionHandling().accessDeniedHandler(datamartSecurityErrorHandler)
            .and()
            .oauth2ResourceServer().jwt().and().authenticationEntryPoint(datamartSecurityErrorHandler)
    .and().csrf(AbstractHttpConfigurer::disable);
}

@Bean
JwtDecoder jwtDecoder() {
    NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder) JwtDecoders.fromOidcIssuerLocation(issuer);

    OAuth2TokenValidator<Jwt> audienceValidator = new DatamartAudienceValidator(audience);
    OAuth2TokenValidator<Jwt> issuerValidator = JwtValidators.createDefaultWithIssuer(issuer);
    OAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<>(issuerValidator, audienceValidator);

    jwtDecoder.setJwtValidator(validator);

    return jwtDecoder;
}

Any requests made to endpoints outside of the /path/** pattern still work fine (without CORS headers).对 /path/** 模式之外的端点发出的任何请求仍然可以正常工作(没有 CORS 标头)。

Has anyone else ran into this or have any suggestions on how to troubleshoot further.有没有其他人遇到过这个问题或对如何进一步排除故障有任何建议。 Thanks and let me know if I can provide anything else to help answer.谢谢,如果我能提供任何其他帮助回答,请告诉我。

It turns out that there was a CORS filter defined in a web.xml file hiding in our tomcat server's conf folder.事实证明,在 web.xml 文件夹中定义了一个 CORS 过滤器。 Removing the filter and letting Spring handle cors inside the application solved the problem.移除过滤器并让 Spring 在应用程序内处理 cors 解决了问题。

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

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