add multiple cross origin urls in spring boot
The way you are setting will only set the third origin and the other two will be gone.
if you want all the three origins to be set then you need to pass them as comma separated Strings.
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://domain1.com","http://domain2.com"
"http://domain3.com");
}
you can find the actual code here:
https://github.com/spring-projects/spring-framework/blob/00d2606b000f9bdafbd7f4a16b6599fb51b53fa4/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/CorsRegistration.java#L61
https://github.com/spring-projects/spring-framework/blob/31aed61d1543f9f24a82a204309c0afb71dd3912/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java#L122
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@PropertySource("classpath:config.properties")
public class CorsClass extends WebMvcConfigurerAdapter {
@Autowired
private Environment environment;
@Override
public void addCorsMappings(CorsRegistry registry) {
String origins = environment.getProperty("origins");
registry.addMapping("/api/**")
.allowedOrigins(origins.split(","));
}
}
In Spring boot there is an annotation @CrossOrigin which will simply add header in the response.
1. For multiple:
@CrossOrigin(origins = {"http://localhost:7777", "http://someserver:8080"})
@RequestMapping(value = "/abc", method = RequestMethod.GET)
@ResponseBody
public Object doSomething(){
...
}
2. If you wanna allow for everyone then simply use.
@CrossOrigin