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変数を静的文字列に置き換えました. ゴリラ セッションはゴリラ コンテキストを使用して新しいリクエストを登録します。ポストマンからのリクエストcontext.data[r]は null ではありませんが、クライアントからは常に null -> 常に新しいセッションです。
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行目
EDIT 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:
そこで、フィドラーでデバッグを試みました(提案に感謝します)。郵便配達員からのリクエストには太字のエントリがCookies / Loginあり、クライアントからのリクエストには含まれていないことがわかりました。その値を取得/設定する方法はありますか? Postmanでどういうわけか自動的に設定されます。認証リクエストで、必要なすべてのデータを含む set-cookie ヘッダーを取得しますが、クライアントで取得できません。私は得るRefused to get unsafe header set-cookie。