【发布时间】:2018-05-14 10:37:36
【问题描述】:
我在 Spring 中使用 CrossOrigin 注解。 现在我想注入一个属性作为注释的值。我不让这个工作。
现在我像这样访问我的财产:
@Value("${settings.cors_origin}")
String cors_origin;
我想将此属性注入 CrossOrigin 注释:
....
@CrossOrigin(origins = cors_origin)
@RequestMapping(value="/request", method= RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> getMethod()
{
...
我已经尝试过这样的事情:
@CrossOrigin(origins = "${settings.cors_origin}")
编辑 1:
现在我尝试在我的 spring 配置中全局设置 CORS-Header:
<context:property-placeholder location="classpath*:*appsettings.properties"/>
<mvc:cors>
<mvc:mapping path="/api/**"
allowed-origins="${test}"
allowed-methods="GET, PUT, OPTIONS, POST"/>
</mvc:cors>
此设置也不起作用!允许的来源与属性文件中指定的来源不同。我认为它不会将变量转换为值?当我在 spring config allowed-origins 中手动设置 IP 地址时,它正在工作。设置有问题...
appsettings.properties:
test=http://192.168.1.200
编辑 2:
S O L V E D
经过一段时间的故障排除后,我现在为我解决了这个问题 :-) 现在我再次使用注释@CrossOrigin。我必须将 RequestMethod Options 添加到 Spring RequestMapping:
@RestController
@CrossOrigin(origins = {"${settings.cors_origin}"})
@RequestMapping("/api/v1")
public class MainController {
@RequestMapping(value="/request", method = {RequestMethod.GET, RequestMethod.OPTIONS}, produces = "application/json")
public ResponseEntity<?> getMethod()
{
感谢您的帮助!
【问题讨论】:
-
这个解决方案是否适用于 Spring Boot 2.0.2.RELEASE 而不是 2.0.4.RELEASE?所有包含 SpEL 的字符串都不再翻译,例如保持为
${settings.cors_origin}。
标签: java spring spring-mvc spring-security annotations