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!
This @CrossOrigin annotation enables cross-origin resource sharing only for this specific method. By default, its allows all origins, all headers, and the HTTP methods specified in the @RequestMapping annotation. Also, a maxAge of 30 minutes is used.
You will need to add the application. properties file in your classpath. If you are using Maven or Gradle, you can just put the file under src/main/resources . If you are not using Maven or any other build tools, put that under your src folder and you should be fine.
properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With