I am trying to define origins
which are to be allowed to execute a REST Service. I am working on Java(1.8), SpringBoot (1.5.9). I have tried to implement CORS in 2 ways:
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping(value="/questions")
public String readCSV() throws IOException, URISyntaxException {
/** Some Logic **/
return str;
}
OR
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
String origins = "http://localhost:8080";
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "PUT", "POST")
.allowedOrigins(origins)
.maxAge(3600);
super.addCorsMappings(registry);
}
}
Both the above cases works. But I want to change that http://localhost:8080
to some meaningful regex, which will match to some particular domain name, but the subdoamin name and the port number may vary.
For example, let's say the desired domain name is innovation.com and few accepted urls would be we.innovation.com:8081, my.innovation.com:8090 etc.
The problem is, whenever I put some regex instead of the static URLs, it throws CORS error and is not allowed to access the service. Even in the case when I used regex which allows any valid url, it blocks localhost:8080
.
I have tried regex like:
^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+innovation.com(:\d{2,4})
^https?://(?:[^./@]+\.)*domain\.com(?![^/])
^(http[s]:\/\/)?([A-Za-z0-9-]{1,63}\\.)+innovation.com(:\d{2,4})