Jhipster CORS启用 [英] Jhipster CORS enable

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

问题描述

我正在使用Jhipster生成API.

I am using Jhipster to generate API.

我的api已启用,可以调用它:

My api is on lets call it :

https://api.staging.test.com/

并且我的FE应用程序处于打开状态:

and my FE app is on :

https://staging.test.com/

这是我在application-prod.yml中启用Cors的配置

Here is my config for Cors enable in application-prod.yml

cors:
     allowed-origins: "https://staging.test.com/"
     allowed-methods: "*"
     allowed-headers: GET, PUT, POST, DELETE, OPTIONS
     exposed-headers: "Authorization,Link,X-Total-Count"
     allow-credentials: true
     max-age: 1800

我仍然收到此错误:

对预检请求的响应未通过访问控制检查:否 请求中存在"Access-Control-Allow-Origin"标头 资源.因此,来源' https://staging.test.com '不是 允许访问.如果不透明的响应满足您的需求,请设置 请求的模式设置为"no-cors",以在禁用CORS的情况下获取资源.

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://staging.test.com' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

要启用CORS,在Spring启动中还需要做更多的事情吗?

Is there anything more that needs to be done in Spring boot to enable CORS ?

推荐答案

在JHipster的Prod模式下启用CORS所需的唯一配置是设置jhipster.cors配置,如下所示.需要注意的一件事是,如果您的前端使用的是端口,则需要将其包含在allowed-origins键中.

The only config needed to enable CORS in JHipster's Prod mode is to set the jhipster.cors configuration like below. One thing to note is that if your frontend is using a port, that needs to be included in the allowed-origins key.

jhipster:
    cors:
        allowed-origins: "https://staging.test.com:8080"
        allowed-methods: "*"
        allowed-headers: "*"
        exposed-headers: "Authorization,Link,X-Total-Count"
        allow-credentials: true
        max-age: 1800

这是由JHipsterProperties加载的,并用于

This is loaded by JHipsterProperties and used in WebConfigurer.java to apply the CORS configuration.

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

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