Flash Player (9,0,124,0) 版本升级后安全沙箱解决方案(http)
概述:由于2008年4月Adobe对Flash Player 进行了版本升级(version 9,0,124,0),这一升级对于安全沙箱有较为重大的影响,以前一些配置crossdomain.xml的方式在新的player中变为不可用。因此很多开发人员都遇到了新的问题,由于项目需要,我对http访问外部资源的过沙箱操作进行了一些研究,稍作整理与大家分享
Flash Player 9,0,124,0 以前老版本的过沙箱方法
例如:
你的swf存放位置: http://www.yourswf.com:8080/flexProjectName/test.swf
你想调用的外部资源位置: http://www.othersxml.com:8088/xmlProjectName/resource.xml
老的解决方案:
1、在 http://www.othersxml.com:8088/xmlProjectName/ ,外部资源项目上下文根目录下存放 crossdomain.xml. 这个xml文件中配置对 www.yourswf.com:8080 这一域名的许可。
2、在 test.swf 代码中加入 Security.loadPolicyFile(http://www.othersxml.com:8088/xmlProjectName/crossdomain.xml); 载入许可文件
版本升级后的改变:
即使在http://www.othersxml.com:8088/xmlProjectName/ 下存放了crossdomain.xml文件,并使用loadPolicyFile加载这个文件,也并不意味着 /xmlProjectName/ 下的所有资源对swf进行了开放。
新的策略要求,在http://www.othersxml.com:8088/ 这个服务器根路径上需要存放一份crossdomain.xml文件,这里称之为主策略文件。而如果是仅仅在 /xmlProjectName/ 下存放crossdomain.xml是没有什么作用的
新的解决方案1:
在http://www.othersxml.com:8088/ 下添加crossdomain.xml
格式如下
<cross-domain-policy>
<site-control permitted-cross-domain-policies=”all” />
<allow-access-from domain=”www.yourswf.com” to-ports=”8080″ secure=”true” />
<allow-http-request-headers-from headers=”*” domain=”*” />
</cross-domain-policy>
然后在Swf代码中加载这个文件Security.loadPolicyFile(http://www.othersxml.com:8088/crossdomain.xml);
新的解决方案2:
对于因为某些原因无法在服务器根目录下存放crossdomain.xml文件,例如 swf需要访问http://www.othersxml.com:8088/xmlProjectName/ 下的资源,而你作为一个项目开发维护人员仅仅拥有对于 /xmlProjectName/ 下资源的操作权限,而没有对整个服务器资源配置的权限,因此你无法往http://www.othersxml.com:8088/下存放主策略文件crossdomain.xml。这种情况是经常发生的,对于租赁服务器空间来说这是很常见的现象,因此adobe给了另外一套解决方案。
后台开发人员需要在http://www.othersxml.com:8088/xmlProjectName/ 下建立一个servlet,这里我们访问的url取名为 /getPolicyFile.do
在这个servlet中我们需要实现代码级别的策略(crossdomain属于文件基本的策略)
关键代码如下
File file = new File(“D:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\othersxml\\crossdomain.xml”);
BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file));
byte[] buffer = new byte[4096];
response.addHeader(“Content-Type”, “text/xml; charset=UTF-8″);
response.addHeader(“X-Permitted-Cross-Domain-Policies”, “all”);
OutputStream os = response.getOutputStream();
int length = 0;
while ((length = bis.read(buffer)) != -1)
{
os.write(buffer, 0, length);
os.close();
}
bis.close();
通过代码的形式将policy file 打印出来,并加上http header X-Permitted-Cross-Domain-Policies = all
在swf代码段则使用 Security.loadPolicyFile(http://www.othersxml.com:8088/xmlProjectName/getPolicyFile.do);的形式来进行加载即可突破新的沙箱限制
参考文档:
http://www.riachina.com/showtopic-12813.aspx cimmicola 版主的回帖给予了很大帮助
http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security.html adobe官方的介绍,很详细
