【发布时间】:2018-01-24 11:16:08
【问题描述】:
我在使用 CORS 的基本 HTTP 身份验证时遇到了一些问题:我们有一个 node express Web 服务器 (UI),从 Java Dropwizard (Jersey) 服务器调用 HTTP API,在同一主机上运行。
API 受到 HTTP 基本身份验证的保护,我在我的 Jersey 服务器上实现了以下过滤器(摘自这篇文章:How to handle CORS using JAX-RS with Jersey):
@Provider
public class CORSFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext request,
ContainerResponseContext response) throws IOException {
response.getHeaders().add("Access-Control-Allow-Origin", "http://localhost:9000");
response.getHeaders().add("Access-Control-Allow-Headers",
"Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
response.getHeaders().add("Access-Control-Allow-Credentials", "true");
response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
}
}
但是,当我尝试加载 Web UI 时,我的控制台会给出以下输出:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9000/intraday/parameters. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘http://localhost:9000’)
我无法理解这个错误。显然来源是相同的 (http://localhost:9000),所以我不明白为什么它不匹配。
我还确保使用 HTTP 代码 200 响应任何预检 OPTIONS 请求。
【问题讨论】:
-
@sascha10000 您提供的链接与我在问题中已经链接到的帖子相同。
-
哦,抱歉,我真的没有看到这是一个链接...