Jhipster CORS启用 [英] Jhipster CORS enable
问题描述
我正在使用Jhipster生成API.
I am using Jhipster to generate API.
我的api已启用,可以调用它:
My api is on lets call it :
并且我的FE应用程序处于打开状态:
and my FE app is on :
这是我在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
This is loaded by JHipsterProperties and used in WebConfigurer.java to apply the CORS configuration.
这篇关于Jhipster CORS启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!