春季CORS。在允许的来源中添加图案 [英] Spring CORS. Add pattern in the allowed origins

查看:76
本文介绍了春季CORS。在允许的来源中添加图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参见CORS的春季指南,以下代码启用了所有允许的来源:

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("*");
    }
}

对于多个原点,方法allowOrigins允许一个域,例如:

And for multiples origins, the method allowedOrigins permits more than one domain, eg:

registry.addMapping("/**").allowedOrigins("http://domain1.com", "http://domain2.com");

因此,是否可能在allowedOrigins中使用常规模式?在我的工作中,我需要在开发环境中测试REST服务,并且它们的宿主在创建时即是可变的,例如: http://www.devXXXXXX.company.com ,其中XXXXXX是随机数。

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.

推荐答案

com.ge.predix.web.cors.CORSFilter 具有一种机制,可让您使用 cors.xhr.allowed.origins 属性。

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.

您可以将 cors.xhr.allowed.origins 属性放入一个 application.propertie s 文件

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 

这篇关于春季CORS。在允许的来源中添加图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆