Spring CORS. Add pattern in the allowed origins

蓝咒 提交于 2020-06-26 14:08:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!