4

在我升级到 spring-boot 1.3 后,我试图切换到 spring 4.2 的原生 Global CORS 支持,但它似乎不适用于 CAS 过滤器进程 url(/login/cas)。

最初,我使用 spring-boot 1.2.7 和 spring 4.2 和 spring-security 4.0.2,并使用自制的基于过滤的 cors 支持。我自己的休息服务或 CAS ST 验证 URL 运行良好。在我升级到 spring-boot 1.3 并带有 spring 和 spring-security 版本之后。它停止工作。经过一番挖掘,通过AddFilterBefore修复了这个问题。因此,基于过滤的 CORS 似乎也适用于 spring-boot 1.3.0 + spring-security-cas。

但是,我想使用本机全局 CORS,但似乎无法识别 CAS ST 验证 URL(/login/cas),尽管其他其余端点都可以。

请帮忙。

设置非常简单。

@Configuration
public class CorsConfiguration {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {

                registry.addMapping("/**");
            }
        };
    }
}   

以下是一些流量:

    Request URL:http://localhost:9000/login/cas?ticket=ST-1357-15aQrv93jGEUsQpQRF1P-cas01.example.org
    Request Method:GET
    Status Code:302 Found

    Cache-Control:no-cache, no-store, max-age=0, must-revalidate
    Content-Length:0
    Date:Thu, 19 Nov 2015 09:19:31 GMT
    Expires:0
    Location:http://localhost:9000/
    Pragma:no-cache
    Server:Apache-Coyote/1.1
    X-Content-Type-Options:nosniff
    X-Frame-Options:DENY
    X-XSS-Protection:1; mode=block

以下是控制台错误:

XMLHttpRequest cannot load http://localhost:9000/login/cas?ticket=ST-1357-15aQrv93jGEUsQpQRF1P-cas01.example.org. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
4

1 回答 1

7

CORS 本机支持默认在 Spring MVCHandlerMapping级别完成,因此预计您的 CAS 过滤器不会启用 CORS,因为它更早地处理请求。

要考虑的一种选择是使用org.springframework.web.filter.CorsFilter我们还提供的 Spring Framework 4.2AddFilterBefore方法。

请注意,CorsConfiguration它的默认配置与@CrossOriginor不同CorsRegistry,因此您需要自己定义大部分属性,例如:

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true); // you USUALLY want this
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("PUT");
    source.registerCorsConfiguration("/**", config);
    CorsFilter filter = new CorsFilter(source);
    // ...
于 2015-11-20T09:32:40.553 回答