问题
Seeing the spring guides of CORS, the following code enable all allowed origins:
public class MyWebMVCConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
}
And for multiples origins, the method allowedOrigins permits more than one domain, eg:
registry.addMapping("/**").allowedOrigins("http://domain1.com", "http://domain2.com");
So, it's possible use a regular pattern in allowedOrigins? At my work I need to test a REST service in a develop enmviroment and their host is variable in the moment of creation, eg: http://www.devXXXXXX.company.com where XXXXXX is a random number.
回答1:
com.ge.predix.web.cors.CORSFilter has a mechanism to allow you to specify a comma-delimited list of regular-expression origin patterns using a cors.xhr.allowed.origins
property.
You can put the cors.xhr.allowed.origins
property into an application.properties file:
cors.xhr.allowed.origins=http:\/\/www\.dev[0-9]+\.company\.com
来源:https://stackoverflow.com/questions/42162874/spring-cors-add-pattern-in-the-allowed-origins