Is there a setting in golang server that force cookie to be auto attach from the browser if ajax request's domain and path matches cookie domain and path?

xuanbao · · 416 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am trying to test if I am in domain A, can domain A client send domain B cookie to domain B.</p> <p>Here is my golang code</p> <pre><code>package main import ( &#34;fmt&#34; &#34;net/http&#34; &#34;log&#34; &#34;time&#34; &#34;encoding/json&#34; ) func setCookie(w http.ResponseWriter, r *http.Request) { expiration := time.Now().Add(365 * 24 * time.Hour) cookie := http.Cookie{Path: &#34;/test_receive_cookie&#34;, Name: &#34;test_cors&#34;, Value: &#34;test_cors&#34;, Expires: expiration} http.SetCookie(w, &amp;cookie) fmt.Fprintf(w, &#34;Success&#34;) } func receiveCookie(w http.ResponseWriter, r *http.Request) { fmt.Println(r.Cookies()) w.Header().Set(&#34;Access-Control-Allow-Origin&#34;, &#34;*&#34;) data := make(map[string]interface{}) for _, cookie := range r.Cookies() { data[cookie.Name] = cookie.Value } w.Header().Set(&#34;Content-Type&#34;, &#34;application/json&#34;) json.NewEncoder(w).Encode(data) } func main() { http.HandleFunc(&#34;/set_cookie&#34;, setCookie) http.HandleFunc(&#34;/test_receive_cookie&#34;, receiveCookie) err := http.ListenAndServe(&#34;:8012&#34;, nil) if err != nil { log.Fatal(&#34;ListenAndServe: &#34;, err) } } </code></pre> <p>I first hit <code>http://localhost:8012/set_cookie</code> , then I open a html file containing a javascript using this <a href="https://github.com/naugtur/xhr" rel="nofollow">library</a></p> <pre><code> this._xhr.get( &#34;http://localhost:8012/test_receive_cookie&#34;, function(err, resp) { console.log(resp); console.log(err); }); </code></pre> <p>Here is result of <code>fmt.Println(r)</code></p> <pre><code> &amp;{GET /test_receive_cookie HTTP/1.1 1 1 map[Accept:[*/*] Accept-Encoding:[gzip, deflate, br] Accept-Language:[en-US,en;q=0.9] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36] Connection:[keep-alive] Pragma:[no-cache] Cache-Control:[no-cache] Origin:[null]] {} &lt;nil&gt; 0 [] false localhost:8012 map[] map[] &lt;nil&gt; map[] [::1]:61817 /test_receive_cookie &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; 0xc420014600} </code></pre> <p>The following happened</p> <ol> <li><p>My server print <code>[]</code> from <code>fmt.Println(r.Cookies())</code></p></li> <li><p>If I hit <code>http://localhost:8012/test_receive_cookie</code> I can see the cookie I set gets print out on browser, but when I open a html that calls the endpoint the server will have empty cookie</p></li> </ol> <p>My questions is how can I pass the cookie set by <code>http://localhost:8012/test_receive_cookie</code> back to <code>http://localhost:8012/test_receive_cookie</code> using client code?</p> <p>Am I missing some configuration code?</p>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

416 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传