第三方 cookie 写入问题

来源:互联网 发布:魔兽世界网络断开连接 编辑:程序博客网 时间:2024/05/16 16:05

场景:

xx.com/y.html 代码为:

 

Html代码 复制代码 收藏代码
  1. <iframe id='ok' name='ok' src='http://zz.com/w.htm?authKey=ertwg'></iframe>  

 

w.htm 需要根据 authKey 写入 cookie ,授权y.html 嵌入zz.com 站点的其他页面(例如即时重定向到另一个页面显示写入的 cookie )。

 

w.htm 内容为:

 

Html代码 复制代码 收藏代码
  1. if (request.getParameter("authKey") != null) {   
  2.             response.addCookie(new Cookie("logined", "ok"));   
  3.             response.sendRedirect(request.getContextPath() + request.getServletPath());   
  4.   
  5.         } else {   
  6.             Cookie[] cs = request.getCookies();   
  7.             if (cs != null) {   
  8.                 for (Cookie c : cs) {   
  9.                     if (c.getName().equals("logined")) {   
  10.                         response.getWriter().println("logined : " + c.getValue());   
  11.                     }   
  12.                 }   
  13.             }   
  14.   
  15.         }  

问题 :

但是在 ie6,7 以及 safari 中发现 cookie 并没有被写入,重定向的读页面读不出刚刚设置的cookie。

解决:

涉及到 p3p 简洁策略 的设置以及在 safari 下的特殊处理:

ie6,7

需要在 w.htm 的返回响应中加入 p3p 简明策略响应头:

 

Java代码 复制代码 收藏代码
  1. // ie need this   
  2. response.addHeader("P3P""CP=\"CAO PSA OUR\"");  
 

允许在嵌入自己情况下写入cookie。

safari

不能直接通过 iframe.src 来请求第三方页面,需要通过表单 post 提交来允许第三方页面 cookie 写入 :

 

Js代码 复制代码 收藏代码
  1. S.use("ua,dom"function(S, UA, DOM) {   
  2.     var ok = S.get("#ok");   
  3.     var action = "http://zz.com/w.htm?authKey=ssdf";   
  4.     if (UA.safari) {   
  5.         var form = DOM.create("<form " +   
  6.                 " method='post' " +   
  7.                 "action='" + action + "'" +   
  8.                 " target='ok'" +   
  9.                 " style='position: absolute;left: -9999;top: -9999px'>");   
  10.         DOM.append(form,document.body);   
  11.         DOM.append(DOM.create("<input name='authKey' value='ssdf'/>"), form);   
  12.         form.submit();   
  13.     } else {   
  14.         ok.src = action;   
  15.     }   
  16. });  
 

 

update 2011-05-26

 

1.该问题和 iframe 没有关系,只要是当前页面往第三方页面发送 get 请求,该 get 请求无论是通过 iframe发送,还是通过 img.src 或者通过 script.src ,第三方页面都会写不进 cookie.

 

2.多次重定向以及 https 情景下依然可用。

 

 

场景:

 

http://a.com/demo.html 嵌入 iframe 页面 http://b.com/cookie.htm?set=2

 

a.com/demo.html :

 

Html代码 复制代码 收藏代码
  1. <iframe src='http://b.com/cookie.htm?set=2'></iframe>  

 

b.com/cookie.htm :

 

Java代码 复制代码 收藏代码
  1. if (request.getParameter("set") != null) {   
  2.             // ie need this  
  3.             //response.addHeader("P3P", "CP=\"CAO PSA OUR\"");  
  4.             String set = request.getParameter("set");   
  5.             if (set.equals("1")) {   
  6.                 System.out.println(request.getPathInfo());   
  7.                 response.addCookie(new Cookie("logined""ok"));   
  8.                 response.sendRedirect(request.getContextPath() + request.getServletPath());   
  9.             } else {   
  10.                 response.sendRedirect("https://a.com/iframe_post.html");   
  11.             }   
  12.         } else {   
  13.             Cookie[] cs = request.getCookies();   
  14.             if (cs != null) {   
  15.                 for (Cookie c : cs) {   
  16.                     if (c.getName().equals("logined")) {   
  17.                         response.getWriter().println("logined : " + c.getValue());   
  18.                     }   
  19.                 }   
  20.             }   
  21.   
  22.         }  

 

会使得 a.com 中的 iframe 跳转多次,

 

iframe -> b.com/cookie.htm?set=2 -> https://a.com/iframe_post.htm

 

a.com/iframe_post.htm 会再次发送请求给 b.com/cookie.htm?set=1 ,这时会设置 cookie:

 

a.com/iframe_post.htm :

 

Html代码 复制代码 收藏代码
  1. <meta charset='utf-8'/>  
  2. <script type="text/javascript" src="../../base/javascript/kissy.js"></script>  
  3. <script type="text/javascript">  
  4.     KISSY.ready(function(S) {   
  5.         S.use("ua,dom", function(S, UA, DOM) {   
  6.             var ok = S.get("#ok");   
  7.             var action = "https://b.com/cookies?set=1";   
  8.             if (UA.safari) {   
  9.                 var form = DOM.create("<form " +   
  10.                         " method='post' " +   
  11.                         "action='" + action + "'" +   
  12.                           
  13.                         " style='position: absolute;left: -9999;top: -9999px'>");   
  14.                 DOM.append(form,document.body);   
  15.                 DOM.append(DOM.create("<input name='set' value='1'/>"), form);   
  16.                 form.submit();   
  17.   
  18.             } else {   
  19.                 window.location = action;   
  20.             }   
  21.         });   
  22.     });   
  23. </script>  
 

关键在于设置 cookie 前的这一请求在 safari 下必须为 post 过去的!即 a.com/iframe_post.html 中的 UA 判断,通过 form 提交使得自身跳转到 b.com/cookie.htm

 

转载地址:http://yiminghe.iteye.com/blog/1055256/

原创粉丝点击