0

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?

Dinesh Appuhami
  • 710
  • 1
  • 11
  • 24

3 Answers3

0

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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

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

0

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

@CrossOrigin(origins = "#{'${my.cross.origin.urls}'.split(',')}")
Mar-Z
  • 2,660
  • 2
  • 4
  • 16
  • already tried this and not working – Dinesh Appuhami May 12 '23 at 03:47
  • OK. It looks like the CorsConfiguration does not accept placeholders for the property allowedOrigins. From JavaDoc: "note that such placeholders must be resolved externally." Probably the only way would be to configure them programmatically. – Mar-Z May 12 '23 at 06:17