Gorilla 会话不适用于客户端的 CORS
Gorilla sessions not working for CORS from client
我设置了 Go 休息 api。在登录时我这样做:
session, _ := store.New(r, sessionId)
session.Options.MaxAge = 12 * 3600
err := session.Save(r, w)
//treat error
为了检查 session 我有这样的东西:
session, err := store.Get(r, sessionId)
//treat error
if session.IsNew {
http.Error(w, "Unauthorized session.", http.StatusUnauthorized)
return
}
如果我执行邮递员的请求,它工作正常,但是当我从我的客户那里执行请求时,我得到 401。你们有没有遇到过这样的事情?该商店是 CookieStore。
我已经检查了 id,我用静态字符串替换了 sessionId 变量。 Gorilla session 使用 gorilla 上下文来注册一个新请求,当我从邮递员 context.data[r]
发出请求时,它不是空的,但来自客户端的它总是空的 -> 总是一个新的 session。
https://github.com/gorilla/context/blob/master/context.go - 第 33 行
在
中被调用
https://github.com/gorilla/sessions/blob/master/sessions.go - 第 122 行
在
中的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
而客户端的请求没有。知道如何 get/set 该值吗?它以某种方式在 Postman 中自动设置。在身份验证请求中,我得到一个 set-cookie header ,其中包含我需要的所有数据,但我无法在客户端上获取它。我得到 Refused to get unsafe header set-cookie
.
问题是在客户端中,请求需要 withCredentials = true
,然后浏览器会处理所有事情。它从 set-cookie
header 获取 cookie,并通过 cookie
header 发送 cookie。所以,归根结底,这不是大猩猩会话的问题。
如果其他人遇到了我遇到的同样问题,并且您想将所有 domains/wildcards 列入白名单(或者有一个数组中的域列表,您可以扫描),您可以这样做。
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")
}
我爱围棋
我设置了 Go 休息 api。在登录时我这样做:
session, _ := store.New(r, sessionId)
session.Options.MaxAge = 12 * 3600
err := session.Save(r, w)
//treat error
为了检查 session 我有这样的东西:
session, err := store.Get(r, sessionId)
//treat error
if session.IsNew {
http.Error(w, "Unauthorized session.", http.StatusUnauthorized)
return
}
如果我执行邮递员的请求,它工作正常,但是当我从我的客户那里执行请求时,我得到 401。你们有没有遇到过这样的事情?该商店是 CookieStore。
我已经检查了 id,我用静态字符串替换了 sessionId 变量。 Gorilla session 使用 gorilla 上下文来注册一个新请求,当我从邮递员 context.data[r]
发出请求时,它不是空的,但来自客户端的它总是空的 -> 总是一个新的 session。
https://github.com/gorilla/context/blob/master/context.go - 第 33 行
在
中被调用https://github.com/gorilla/sessions/blob/master/sessions.go - 第 122 行
在
中的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
而客户端的请求没有。知道如何 get/set 该值吗?它以某种方式在 Postman 中自动设置。在身份验证请求中,我得到一个 set-cookie header ,其中包含我需要的所有数据,但我无法在客户端上获取它。我得到 Refused to get unsafe header set-cookie
.
问题是在客户端中,请求需要 withCredentials = true
,然后浏览器会处理所有事情。它从 set-cookie
header 获取 cookie,并通过 cookie
header 发送 cookie。所以,归根结底,这不是大猩猩会话的问题。
如果其他人遇到了我遇到的同样问题,并且您想将所有 domains/wildcards 列入白名单(或者有一个数组中的域列表,您可以扫描),您可以这样做。
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")
}
我爱围棋