Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 857
Hannes Avatar asked Nov 30 '17 13:11

Hannes


People also ask

What is CrossOrigin annotation in spring boot?

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.

Where do I put spring boot application properties?

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.

What is the use of properties file in spring?

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.


Video Answer


1 Answers

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.

like image 128
P.J.Meisch Avatar answered Nov 02 '22 23:11

P.J.Meisch