不懂就问,这个判断是啥意思?

2021-06-02 17:29:25 +08:00
 pxiphx
String origin = request.getHeader("Origin");
if (!"".equals(origin) && origin != null) {
  response.setHeader("Access-Control-Allow-Origin", "xxx");
  response.setHeader("Access-Control-Allow-Methods", "POST, GET, HEAD, OPTIONS, DELETE");
  response.setHeader("Access-Control-Max-Age", "3600");
  response.setHeader("Access-Control-Allow-Credentials", "true");
  response.setHeader("Access-Control-Allow-Headers",
                     "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
}
filterChain.doFilter(request, response);

CORS 相关的代码,这里为啥要判断 origin 不等于空?

1947 次点击
所在节点    Java
4 条回复
Patrick95
2021-06-02 17:37:52 +08:00
Origin 为空就不是 CORS 请求,不会进行 CORS 校验
eason1874
2021-06-02 17:46:20 +08:00
跨域请求才有 Origin,同一个域名请求没有这个,就没必要设置允许与否,设置了也没有意义。
pxiphx
2021-06-02 17:48:39 +08:00
原来如此,感谢大佬们解答
rrfeng
2021-06-02 17:50:40 +08:00
一般都在 options 上做吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/780916

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX