Spring Framework 4.2/Boot 1.3中如何根据Origin HTTP请求头设置动态Access-Control-Allow-Origin?

问题描述 投票:0回答:1

Spring 4.2 支持 CORS ,但是必须手动设置允许的 Origin。与身份验证结合使用时会出现问题,因为

Access-Control-Allow-Origin: *
不支持身份验证。

如何自动设置?对于 Ionic/Cordova 客户非常有用。

即如果客户端发送

Origin: assets-library://whatever/
那么服务器将发送
Access-Control-Allow-Origin: assets-library://whatever/

功能请求:https://github.com/spring-projects/spring-framework/issues/18088(以前的SPR-13511)

spring cordova spring-boot cors spring-data-rest
1个回答
1
投票

通过 Spring Boot 的“默认”,Spring 4.2 在使用

@CrossOrigin
addCorsMappings()
时确实支持我描述的场景。

什么是不是默认,是使用Sébastien在这里描述的技术(我已经更新了答案)。必须注意:

config.setAllowCredentials(true);

因为 CorsConfiguration

real
默认值是
null
,被视为
false

© www.soinside.com 2019 - 2024. All rights reserved.