在springboot @CrossOrigin中添加逗号分隔值

gr8qqesn  于 2023-05-17  发布在  Spring
关注(0)|答案(3)|浏览(181)

application.properties

my.cross.origin.urls=https://test.com,https://abcd.com

我的控制器方法

@CrossOrigin(origins = "${my.cross.origin.urls}")
@PostMapping(value = RequestMappings.CREATE_USER_V3)
    public @ResponseBody TestResponse(HttpServletRequest request, HttpServletResponse response){

}

如何将'my.cross.origin.urls'值绑定为数组?

piok6c0g

piok6c0g1#

使用这种方法

//In application.properties
 my.cross.origin.urls=https://test.com,https://abcd.com

@CrossOrigin("${my.cross.origin.urls}")
 @PostMapping(value = RequestMappings.CREATE_USER_V3)
 public @ResponseBody TestResponse(HttpServletRequest request, 
 HttpServletResponse response){

 }

让我知道,如果这工程有其他的方法也,但为了简单起见,我认为这是更好的

t98cgbkg

t98cgbkg2#

您可以使用SpEL将属性转换为列表,如下所示:

@CrossOrigin(origins = "#{'${my.cross.origin.urls}'.split(',')}")
j5fpnvbx

j5fpnvbx3#

像这样添加URL:

@CrossOrigin(origins = {"https://test.com", "https://abcd.com"})
@PostMapping(value = RequestMappings.CREATE_USER_V3)
public @ResponseBody TestResponse(HttpServletRequest request, HttpServletResponse response) {

}

我希望它能解决你的问题,或者你可以检查这篇文章add multiple cross origin urls in spring boot

相关问题