Is there a way to configure cross-origin scripting in Spring Boot from a properties file?

307 Views Asked by At

I'm using Spring Boot 2 and Java 11. When developing locally (frontend is a React app), I'm configuring allowing cross-origin requests by creating a configuration file and adding this to it

@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("GET","POST","OPTIONS","PATCH","DELETE");
    }
}

Is there a simper way to do this from a properties file or enabling some other option? I don't like the idea of adding code only to accommodate my local environment.

1

There are 1 best solutions below

0
On

You can @CrossOrigin annotation to the handler method as well as to the controller to enable cross origin. For more information follow below link.

https://spring.io/guides/gs/rest-service-cors/