第三方 cookie 写入问题

场景:

xx.com/y.html 代码为:

 

Html代码 复制代码  收藏代码
  1. <iframe id='ok' name='ok' src='http://zz.com/w.htm?authKey=ertwg'></iframe>  
<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.         }  
if (request.getParameter("authKey") != null) {
			response.addCookie(new Cookie("logined", "ok"));
			response.sendRedirect(request.getContextPath() + request.getServletPath());

		} else {
			Cookie[] cs = request.getCookies();
			if (cs != null) {
				for (Cookie c : cs) {
					if (c.getName().equals("logined")) {
						response.getWriter().println("logined : " + c.getValue());
					}
				}
			}

		}

问题 :

但是在 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\"");  
// ie need this
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>  
<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.         }  
if (request.getParameter("set") != null) {
			// ie need this
			//response.addHeader("P3P", "CP=\"CAO PSA OUR\"");
			String set = request.getParameter("set");
			if (set.equals("1")) {
				System.out.println(request.getPathInfo());
				response.addCookie(new Cookie("logined", "ok"));
				response.sendRedirect(request.getContextPath() + request.getServletPath());
			} else {
				response.sendRedirect("https://a.com/iframe_post.html");
			}
		} else {
			Cookie[] cs = request.getCookies();
			if (cs != null) {
				for (Cookie c : cs) {
					if (c.getName().equals("logined")) {
						response.getWriter().println("logined : " + c.getValue());
					}
				}
			}

		}

 

会使得 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>  
<meta charset='utf-8'/>
<script type="text/javascript" src="../../base/javascript/kissy.js"></script>
<script type="text/javascript">
    KISSY.ready(function(S) {
        S.use("ua,dom", function(S, UA, DOM) {
            var ok = S.get("#ok");
            var action = "https://b.com/cookies?set=1";
            if (UA.safari) {
                var form = DOM.create("<form " +
                        " method='post' " +
                        "action='" + action + "'" +
                       
                        " style='position: absolute;left: -9999;top: -9999px'>");
                DOM.append(form,document.body);
                DOM.append(DOM.create("<input name='set' value='1'/>"), form);
                form.submit();

            } else {
                window.location = action;
            }
        });
    });
</script>
 

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

 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
闪灵CMS新闻门户系统是一个以s-cms为核心进行开发的闪灵新闻门户系统网站模板。网站使用方法:将文件上传只PHP空间,运行http://你的域名/install.php进行安装。只需两步即可安装完成,为了保证网站安全,请修改默认后台路径及数据库名称。闪灵新闻门户系统前台页面【环境支持】PHP环境 / 200M或以上空间大小 / 开启父路径 / 设置index.php为默认首页 / 目录写入权限需要开启【安装步骤】将文件上传至空间目录,运行“http://域名/install.php”进入安装页面,按照提示安装完成即可【目录支持】支持根目录/子目录安装【数据库】MYSQL。【常见问题1】数据库连接错误:(1)64位系统的用户请在iis应用程序池开启对32位的支持(2)网站目录给users用户完全控制的权限(3)C:\windows\temp文件夹给everyone完全控制的权限【常见问题2】未安装直接显示初始化完成:(1)网站目录给users用户完全控制的权限闪灵CMS新闻门户系统 v5.0  build20190817更新说明1.优化:对上传图片时的随机命名规则进行了优化,防止出现重名图片2.修复:修复后台部分模块权限设置对应错误的问题3.优化:论坛模块的登录链接增加直接返回的功能,方便用户会员原始页面闪灵CMS新闻门户系统 v5.0  build20190921更新说明1.优化:对开启httponly模式下无法读取cookie的主机作了兼容2.优化:https模式下对第三方分享代码做了兼容3.优化:联系页面百度地图的调用增加了对https协议的支持闪灵CMS新闻门户系统 v5.0  build20191018更新说明1.修复:修复了在同步本地文件到OSS时一个文件出错就终止上传的BUG2.优化:优化代码,消除了一些不影响正常功能的报错3.新增:精简了目录结构,删除了一些冗余文件闪灵CMS新闻门户系统 v5.0  build20191028更新说明1.新增:在安装界面完成后,提供保存后台信息的功能,防止用户忘记后台路径或帐号秘密2.修复:修复了在https状态下,后台无法显示模板预览及帮助手册的问题3.修复:修复前台会员注册时储存型XSS漏洞闪灵CMS新闻门户系统 v5.0  build20191109更新说明1.修复:修复了后台download.php跨目录下载文件的问题2.修复:修复了在某些模板下调用全部产品和全部新闻的背景图出错的问题3.修复:修复了在使用微信登录/注册会员时无法获取邀请人信息的问题闪灵CMS新闻门户系统 v5.0  build20191113更新说明1.新增:新增支持伪静态访问,网站可同时支持动态/真静态/伪静态三种2.新增:后台新增清理网站缓存功能3.新增:安装界面可以展示代理商的LOGO及官网标题及链接闪灵CMS新闻门户系统 v5.0  build20191120更新说明1.修复:修复后台留言管理审核界面直接显示代码的错误2.修复:修复接入微信公众号时有几率出现token错误的问题3.优化:后台客服设置改为可视化,防止部分客户因格式错误导致客服无法显示

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值