Spring 4.2的原生全局CORS支持不适用于CAS filterProcessesUrl

bit*_*ain 3 spring spring-security cors spring-boot spring-security-cas

我试图在升级到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.

但是,我想使用本机Global 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("/**");
            }
        };
    }
}   
Run Code Online (Sandbox Code Playgroud)

以下是一些交通:

    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
Run Code Online (Sandbox Code Playgroud)

以下是控制台错误:

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.
Run Code Online (Sandbox Code Playgroud)

Séb*_*uze 7

默认情况下,在Spring MVC HandlerMapping级别完成CORS本机支持,因此预期CAS过滤器不会启用CORS,因为它会更早地处理请求.

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

请注意,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);
    // ...
Run Code Online (Sandbox Code Playgroud)