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 不等于空?
1
Patrick95 2021-06-02 17:37:52 +08:00
Origin 为空就不是 CORS 请求,不会进行 CORS 校验
|
2
eason1874 2021-06-02 17:46:20 +08:00
跨域请求才有 Origin,同一个域名请求没有这个,就没必要设置允许与否,设置了也没有意义。
|
3
pxiphx OP 原来如此,感谢大佬们解答
|
4
rrfeng 2021-06-02 17:50:40 +08:00 via Android
一般都在 options 上做吧
|