Jhipster CORS启用

use*_*181 2 java cors spring-boot jhipster

我正在使用Jhipster生成API。

我的api启用了:

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

我的FE应用程序已打开:

https://staging.test.com/

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

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
Run Code Online (Sandbox Code Playgroud)

我仍然收到此错误:

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

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

Jon*_*ell 5

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

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
Run Code Online (Sandbox Code Playgroud)

它由JHipsterProperties加载,并在WebConfigurer.java中用于应用CORS配置。