Use Spring Properties in Java with CrossOrigin Annotation or in Spring-Config XML
Asked Answered
D

2

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!

Denticulation answered 30/11, 2017 at 13:19 Comment(1)
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.Cheroot
A
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.

Abbasid answered 30/11, 2017 at 15:5 Comment(6)
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..Denticulation
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.Abbasid
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!Denticulation
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="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.Denticulation
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.Denticulation
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...Cheroot
L
0

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

@CrossOrigin(origins = @Value("${settings.cors_origin}"))
Lesalesak answered 30/11, 2017 at 13:48 Comment(5)
I already tried that, it is not working: Found ...annotation.value ... required java.lang.string[]..Denticulation
try to configure CORS for the while application docs.spring.io/spring/docs/4.2.x/spring-framework-reference/…Lesalesak
I tried it to configure Global CORS with the documentation but cant get it to work...Denticulation
This expression doesn't even compileGeometrid
This doesn't workHypersensitive

© 2022 - 2024 — McMap. All rights reserved.