Add comma separated value into springboot @CrossOrigin origins

264 Views Asked by At

application.properties

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

my controller method.

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

}

how can I bind 'my.cross.origin.urls' values as an array?

3

There are 3 best solutions below

1
Alpha Momin On

Add URLs like this:

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

}

I hope it will solve your problem or you can check this post add multiple cross origin urls in spring boot

1
Alpha Momin On

use this approach

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

and

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

 }

let me know if this works there are other approaches also but for the sake of simplicity I think it is better

2
Mar-Z On

You can convert the properties to a list with SpEL like this:

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