【问题标题】:Spring 4.2's native Global CORS support won't work with CAS filterProcessesUrlSpring 4.2 的原生全局 CORS 支持不适用于 CAS filterProcessesUrl
【发布时间】:2016-02-21 07:56:18
【问题描述】:

我在升级到 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("/**");
            }
        };
    }
}   

以下是一些流量:

    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.

【问题讨论】:

    标签: spring spring-security spring-boot cors spring-security-cas


    【解决方案1】:

    默认情况下,CORS 原生支持在 Spring MVC HandlerMapping 级别完成,因此预计您的 CAS 过滤器不会启用 CORS,因为它更早地处理请求。

    要考虑的一个选项是使用org.springframework.web.filter.CorsFilter,我们还通过AddFilterBefore 方法提供Spring Framework 4.2。

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

    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);
        // ...
    

    【讨论】:

    • 谢谢。我不确定我是否做对了。 CORS 本机支持默认在 Spring MVC HandlerMapping 级别完成,因为 CAS filterProcessesUrl(/login/cas) 不在处理程序映射中(我们可以通过检查 Actuator 端点:/mappings 来验证这一点),所以 CORS原生支持不起作用?
    • Spring CORS 支持在HandlerMapping 级别默认启用和启用,但由于org.springframework.web.filter.CorsFilter,它也可以作为过滤级别的替代方案。当您使用过滤器时,HandlerMapping 级别处理is automatically disabled if the response already contains CORS headers。在您的用例中,您可能只想配置和依赖CorsFilter
    猜你喜欢
    • 2014-11-04
    • 2015-07-16
    • 2019-05-25
    • 2020-05-29
    • 1970-01-01
    • 2017-11-25
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多