3

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

4

2 に答える 2

3

問題は、クライアントでリクエストが必要でありwithCredentials = true、その後ブラウザがすべてを処理することです。ヘッダーから Cookie を取得し、set-cookieヘッダー経由で Cookie を送信しますcookie。つまり、結局のところ、ゴリラ セッションの問題ではありませんでした。

于 2015-07-20T06:07:26.590 に答える
0

他の誰かが私が抱えていたのと同じ問題を抱えていて、すべてのドメイン/ワイルドカードをホワイトリストに登録したい場合 (または、スキャンできる配列内のドメインのリストを持っている場合)、次のようなことができます。

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")
}

行くのが大好き

于 2017-10-12T23:25:00.043 に答える