pxiphx
V2EX  ›  Java

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

  •  
  •   pxiphx · Jun 2, 2021 · 3339 views
    This topic created in 1988 days ago, the information mentioned may be changed or developed.
    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 不等于空?

    4 replies  •  2021-06-02 17:50:40 +08:00
    Patrick95
    Patrick95
    Jun 2, 2021
    1
    Origin 为空就不是 CORS 请求,不会进行 CORS 校验
    eason1874
    eason1874
    Jun 2, 2021
    2
    跨域请求才有 Origin,同一个域名请求没有这个,就没必要设置允许与否,设置了也没有意义。
    pxiphx
    pxiphx
    OP
    Jun 2, 2021
    3
    原来如此,感谢大佬们解答
    rrfeng
    rrfeng
    Jun 2, 2021 via Android
    4
    一般都在 options 上做吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1790 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 16:12 · PVG 00:12 · LAX 08:12 · JFK 11:12
    ♥ Do have faith in what you're doing.