V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
pxiphx
V2EX  ›  Java

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

  •  
  •   pxiphx · Jun 2, 2021 · 2803 views
    This topic created in 1799 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
        1
    Patrick95  
       Jun 2, 2021
    Origin 为空就不是 CORS 请求,不会进行 CORS 校验
    eason1874
        2
    eason1874  
       Jun 2, 2021
    跨域请求才有 Origin,同一个域名请求没有这个,就没必要设置允许与否,设置了也没有意义。
    pxiphx
        3
    pxiphx  
    OP
       Jun 2, 2021
    原来如此,感谢大佬们解答
    rrfeng
        4
    rrfeng  
       Jun 2, 2021 via Android
    一般都在 options 上做吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1082 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 18:10 · PVG 02:10 · LAX 11:10 · JFK 14:10
    ♥ Do have faith in what you're doing.