带有“ *”通配符的Spring CORS映射不起作用 [英] Spring CORS Mapping with '*' wildcard not working

查看:81
本文介绍了带有“ *”通配符的Spring CORS映射不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot应用程序。在Application类中,我定义了这样的CORS映射:

I have a Spring Boot Application. In the Application class I define my CORS mappings like this:

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/lists");
            }
        };
    }

一切都可以使用,但是当我想添加一个包含例如这样的ID

Everything works with this, but when I want to add a mapping that contains e.g. an ID like this

registry.addMapping("/lists/**");  // it also doesn't work with '/lists/*'

,映射不起作用可以正常工作了,我在诸如 / lists / 142之类的GET请求上收到无访问权限-允许标题报头错误

, the mapping doesn't work anymore and I receive a "No Access-Control-Allow-Origin Header" error on a GET request like "/lists/142"

https://spring.io/blog/2015/06/08 / cors-support-in-spring-framework ,我尝试使用 / **作为映射,以允许所有端点URL上的请求,但仅端点没有动态值(例如ID)似乎工作正常。

As shown in https://spring.io/blog/2015/06/08/cors-support-in-spring-framework , I have tried to use "/**" as a mapping to allow requests on all endpoint URLs, but only endpoints without dynamic values (e.g. IDs) seem to work fine.

推荐答案

尝试使用

@CrossOrigin

此功能可以用作通配符。

This works properly as wildcard.

这篇关于带有“ *”通配符的Spring CORS映射不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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