10

I use the CrossOrigin annotation in Spring. Now i want to inject a Property as value to the annotation. I dont get this to work.

Now i access my property like this:

@Value("${settings.cors_origin}")
String cors_origin;

I want to inject this property to the CrossOrigin annotation:

....
@CrossOrigin(origins = cors_origin)
@RequestMapping(value="/request", method= RequestMethod.GET, produces = "application/json")
public ResponseEntity<?> getMethod()
{
...

I tried already something like this:

@CrossOrigin(origins = "${settings.cors_origin}")

Edit 1:

Now i try to set the CORS-Header Globally in my spring config:

  <context:property-placeholder location="classpath*:*appsettings.properties"/>

 <mvc:cors>
 <mvc:mapping path="/api/**"
             allowed-origins="${test}"
             allowed-methods="GET, PUT, OPTIONS, POST"/>
</mvc:cors>

This setting is also not working! The allowed origin is not the same as the one specified in the properties file. I think it will not translate the variable to the value? When i set the IP-Address in the spring config allowed-origins manually it is working. There is something wrong with the settings...

appsettings.properties:

test=http://192.168.1.200

Edit 2:

S O L V E D

I solved it for me now after a hart time of troubleshooting :-) Now i work again with the annotation @CrossOrigin. I had to add the RequestMethod Options to the 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()
  {

Thanks for your help!

Hannes
  • 491
  • 5
  • 21
  • Is there a reason why this solution worked with Spring Boot 2.0.2.RELEASE and not with 2.0.4.RELEASE? All the strings containing SpEL are not translated anymore and stay as `${settings.cors_origin}` for example. – Snackoverflow Aug 28 '18 at 11:42

2 Answers2

6

you need to pass an array of strings to the origins argument of the @CrossOrigin annotation. This is done with curly braces:

@CrossOrigin(origins = {"${settings.cors_origin}"})

Then in Spring, src/main/resources/application.properties:

settings.cors_origin:http://localhost:4200

Note, don't include comments or extra spaces to the right of the key-value pair in application.properties. The extra spaces after the value might cause the key-value pair to be read incorrectly.

Gene
  • 10,819
  • 1
  • 66
  • 58
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • I specified settings.cors_origin in my properties file which is in the resource folder located. I use the PropertyPlaceholder. The setting is not working. It will not translate the path to the setting to the right value.. – Hannes Dec 01 '17 at 07:45
  • do you get an error that the property is not found? Or is cors not working? It's hard to help if you do not show the relevant part of your configuration and the usage. I tried this, putting the cors_origin in my application.properties and running and testing a REST service. – P.J.Meisch Dec 01 '17 at 08:20
  • 1
    Cors is working but there is the wrong value in the cors header (Allowed-Origin), because the variable from the settings is not translating to the value.I dont get anyhting about the property that is not found or something like that. I get HTTP 403 from the Webservice. I think all relevant informations are in my post at the top. What do you need for informations regarding the configuration/usage?! Thanks! – Hannes Dec 01 '17 at 08:32
  • 1
    But when i add the IP address directly without the property and without the port in the spring-config, cors will work -> when i set this: allowed-origins="http://192.168.1.200" it works without the port. Thats why i think it is not a problem with the cors value. It is a problem with the settings itself?! But i tried to add the port and it changed nothing. – Hannes Dec 01 '17 at 08:40
  • 1
    Yes i am sure.In another controller i tested this: @Value("${test}") String Test; System.out.println(Test); and i get the right value from the properties file. I start the service with the jetty maven plugin. – Hannes Dec 01 '17 at 09:03
  • 1
    With Spring Boot version 2.0.2.RELEASE this solution worked: `@CrossOrigin(origins = {"${settings.cors_origin}"})`. However, with 2.0.4.RELEASE it does not anymore, any suggestions for how to get it to work? Currently, the string does not get translated to the configuration value anymore, but stays as `${settings.cors_origin}` during runtime... – Snackoverflow Aug 26 '18 at 19:52
0

you can try to access the property directly in the origins parameter

@CrossOrigin(origins = @Value("${settings.cors_origin}"))
Amr Alaa
  • 545
  • 3
  • 7
  • 2
    I already tried that, it is not working: Found ...annotation.value ... required java.lang.string[].. – Hannes Nov 30 '17 at 14:01
  • try to configure CORS for the while application https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/cors.html#_global_cors_configuration – Amr Alaa Nov 30 '17 at 14:14
  • I tried it to configure Global CORS with the documentation but cant get it to work... – Hannes Nov 30 '17 at 15:33
  • This expression doesn't even compile – iChrome Jun 26 '21 at 13:29