Spring Boot 通过 application.properties 启用 CORS [英] Spring Boot enabling CORS by application.properties

查看:31
本文介绍了Spring Boot 通过 application.properties 启用 CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用由您的实体类自动启动的 Spring Boot API RESTful.我正在从前端网络应用程序中使用这个 apiRest,但它给了我这个错误:

I using Spring Boot API RESTful that get up automatically by your Entities Class. I'm consuming this apiRest from a front-end web app but it gives me this error:

请求的资源上没有Access-Control-Allow-Origin"标头

No 'Access-Control-Allow-Origin' header is present on the requested resource

我正在使用指定的申请属性设置 CORS 配置 这里.

I'm setting the CORS configuration using the applicantion.properties specified here.

我的基本配置是:

endpoints.cors.allow-credentials=true
endpoints.cors.allowed-origins=*
endpoints.cors.allowed-methods=*
endpoints.cors.allowed-headers=*

我在这些变量中尝试了不同的组合,但仍然无法正常工作.有什么想法吗?

I have tried different combinations in those variables but still not working. Any ideas?

推荐答案

我自己得到了答案:

只需将其添加到 application.java

Just add this to application.java

  @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/greeting-javaconfig").allowedOrigins("http://localhost:9000");
            }
        };
    }

这篇关于Spring Boot 通过 application.properties 启用 CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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