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

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

问题描述

我使用Spring Boot API RESTful,该实体由Entities类自动启动。我正在从前端Web应用程序中使用此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

我正在使用指定的applicantion.properties设置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天全站免登陆