5

我正在尝试在我的 Spring Boot 应用程序上配置 CORS。我将 CrossOrigin 注释添加到我的控制器类中。

@CrossOrigin
@RestController
@RequestMapping("api/user")
public class UserApiController {
    ...
}

当我在本地机器上运行它时,我得到了 OPTIONS 请求的这些响应标头:

Access-Control-Allow-Credentials →true
Access-Control-Allow-Methods →GET
Access-Control-Allow-Origin →http://www.test.be
Access-Control-Max-Age →1800
Allow →GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Cache-Control →no-cache, no-store, max-age=0, must-revalidate
Content-Length →0
Date →Wed, 28 Mar 2018 09:13:33 GMT
Expires →0
Pragma →no-cache
Vary →Origin
X-Content-Type-Options →nosniff
X-Frame-Options →DENY
X-XSS-Protection →1; mode=block

我将此应用程序部署在 Tomcat 服务器上,位于 Linux 上运行的 Apache2 服务器后面。当我在那里做同样的请求时,我得到了这个:

Allow →GET,HEAD
Cache-Control →no-cache, no-store, max-age=0, must-revalidate
Connection →Keep-Alive
Date →Wed, 28 Mar 2018 09:42:42 GMT
Expires →0
Keep-Alive →timeout=5, max=100
Pragma →no-cache
Server →Apache/2.4.18 (Ubuntu)
Transfer-Encoding →chunked
X-Content-Type-Options →nosniff
X-Frame-Options →DENY
X-XSS-Protection →1; mode=block

这就是我将 Apache2 配置为代理 Tomcat 的方式

<Proxy *>
Order deny,allow
Allow from all
</Proxy>
#
ProxyRequests Off
ProxyPreserveHost On
#
#
ProxyPass /app  http://localhost:8080/my_app
ProxyPassReverse  /app  http://localhost:8080/my_app

两个请求都返回了 200 OK 状态码,但在服务器版本上我没有得到 Access-Control-Allow 标头。我在 Allow 标头中只看到 GET,HEAD 。为什么 Apache2 不允许 OPTIONS?我该如何解决?

4

0 回答 0