0

I want to use @CrossOrigin annotation on my RestController in my Spring Boot application and set origins parameter with the values from application.properties file.

@CrossOrigin(origins = {"${app.cors.origins}"})
public class SomeController(){
//
//
}

I set the property in my application.properties file like

app.cors.origins =http://www.google.com,http://localhost:8001

However that doesn't work as a cross origin request from http://localhost:8001 to my app fails with CORS error on the browser.

Am I missing something on setting the property?

Update : Problem is to set origins as a string array from the value of application property entry. When I hardcode the urls in origin, it works.

Thanks

serkanz
  • 391
  • 5
  • 21
  • 1
    Actually problem is to set `origins` parameter of `@CrossOrigin` annotation, when I hardcode the url into origins it works. Problem is to create an string array from application property in annotation – serkanz Oct 14 '22 at 16:05

2 Answers2

0

If you want to use application.properties to set origins of CORS then this is the solution.

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${app.cors.origins}")
    private String corsAllowedOrigins;

    @Value("${app.cors.methods}")
    private String corsAllowedMethods;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(corsAllowedOrigins)
                .allowedMethods(corsAllowedMethods);

    }

application.properties

app.cors.origins=http://www.google.com,http://localhost:8001
app.cors.methods=GET,OPTIONS

Rohit Agarwal
  • 763
  • 2
  • 5
  • 10
  • 1
    Hi Rohit, our requirement is to use cors on controller level, not globally. so I can't use configurations, thank you for your reply. – serkanz Oct 14 '22 at 18:19
  • Looks like it is not possible to use environment variables on @CrossOrigin annotation because annotations are processed at compile time only. You can read more on this here https://stackoverflow.com/questions/45974133/specifying-crossorigin-orgins-via-an-environment-variable – Rohit Agarwal Oct 15 '22 at 04:54
0

I'm also working on this issue, I found another post mentioned about controller level parameter annotation for Cors. I haven't try it yet. share it with you:

Create your custom annotation and annotate the API with that.

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@CrossOrigin
public @interface CrossOriginsList {

    public String[] crossOrigins() default  {

            "http://domain1.com", "http://domain1.com"
            "http://domain1.com", "http://domain1.com"
            // Pass as many as you want
    };
}

And now Annotate your API with this custom Annotation

@CrossOriginsList
    public String methodName() throws Exception
{
        //Business Logic
}
  • Does this approach allow me to define crossorigins values dynamically? It seems it's populated statically again. – serkanz Nov 05 '22 at 20:02