【问题标题】:Gorilla sessions not working for CORS from clientGorilla 会话不适用于客户端的 CORS
【发布时间】:2015-10-05 13:41:22
【问题描述】:

我已经设置了一个 Go rest api。在登录时我这样做:

session, _ := store.New(r, sessionId)
session.Options.MaxAge = 12 * 3600
err := session.Save(r, w)
//treat error

为了检查会话,我有这样的感觉:

    session, err := store.Get(r, sessionId)
    //treat error
    if session.IsNew {
        http.Error(w, "Unauthorized session.", http.StatusUnauthorized)
        return
    }

如果我从邮递员那里发出请求,它工作正常,但是当我从我的客户那里发出请求时,我得到 401。你们中有人经历过这样的事情吗?该商店是一个 CookieStore。

我已经检查了 id,我用静态字符串替换了 sessionId 变量。 Gorilla 会话使用 gorilla 上下文注册新请求,当我从邮递员 context.data[r] 发出请求时,它不为空,但从客户端它始终为空 -> 始终为新会话。

https://github.com/gorilla/context/blob/master/context.go - 第 33 行

它被调用

https://github.com/gorilla/sessions/blob/master/sessions.go - 第 122 行

wich 用在 CookieStore.Get 函数中

https://github.com/gorilla/sessions/blob/master/store.go - 第 77 行

编辑 1: 对于我使用聚合物的客户端,我也尝试了 xmlhttp。 聚合物:

<iron-ajax
  id="ajaxRequest"
  auto
  url="{{requestUrl}}"
  headers="{{requestHeaders}}"
  handle-as="json"
  on-response="onResponse"
  on-error="onError"
  content-type="application/json"
  >
</iron-ajax>

和处理程序

  onResponse: function(response){
    console.log(response.detail.response);
    this.items = response.detail.response
  },
  onError: function(error){
    console.log(error.detail)
  },
  ready: function(){
    this.requestUrl = "http://localhost:8080/api/fingerprint/company/" + getCookie("companyId");
    this.requestHeaders = {"Set-cookie": getCookie("api_token")}
  }

cookie 成功到达后端。

还有xmlhttp:

  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
      if(xmlhttp.status == 200){
        //do stuff
      }else if(xmlhttp.status == 401){
        page.redirect("/unauthorized")
      }else{
        page.redirect("/error")
      }
    }
  }

  xmlhttp.open("GET","http://localhost:8080/api/fingerprint/company/" + getCookie("companyId"),true);
  xmlhttp.setRequestHeader("Set-cookie", getCookie("api_token"));
  xmlhttp.send();

编辑 2:

所以我尝试使用 fiddler 进行调试(感谢您的建议),我发现邮递员的请求有一个粗体条目 Cookies / Login,而来自客户端的请求没有。知道如何获取/设置该值吗?它以某种方式在 Postman 中自动设置。在身份验证请求中,我得到一个 set-cookie 标头,其中包含我需要的所有数据,但我无法在客户端上获取它。我得到Refused to get unsafe header set-cookie

【问题讨论】:

  • 看起来这可能是您的客户端如何处理 cookie 的问题。可以添加客户端代码吗?
  • 您可以使用 fiddler 之类的其他工具来检查并提供这两种情况的请求标头和正文吗?我解决这个问题的方法是,摆弄每一个,将数据复制到文本编辑器中,比较两者,使我的客户请求与 Postmans 相同。
  • 当您从“客户端”说时,这是在同一个域上吗?听起来不是。
  • 标题中有CORS这个词=>不是同一个域

标签: session go gorilla


【解决方案1】:

问题是在客户端的请求需要withCredentials = true,然后浏览器处理所有事情。它从set-cookie 标头获取cookie,并通过cookie 标头发送cookie。所以,毕竟这不是大猩猩会话问题。

【讨论】:

  • 对于那些寻求澄清 withCredentials 的人,这是 XMLHttpRequest 对象的一部分,因此在原始帖子中,用户需要添加 xmlhttp.withCredentials = true; 行。
【解决方案2】:

如果其他人遇到与我相同的问题,并且您想将所有域/通配符列入白名单(或在一个可以扫描的数组中列出域列表),您可以执行以下操作。

domain_raw := r.Host
domain_host_parts := strings.Split(domain_raw, ".")
domain := domain_host_parts[1] + "." + domain_host_parts[2]
domains := getDomains() // stores a slice of all your allowable domains
has_domain := false
for _, d := range domains {
    if d == domain {
        has_domain = true
        break
    }
}

if has_domain == false {
    return
} else {
    w.Header().Add("Access-Control-Allow-Origin", "https://"+domain_raw)
    w.Header().Add("Access-Control-Allow-Credentials", "true")
}

我喜欢去

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 2018-06-04
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多