开发中遇到的flex安全沙箱的问题

来源:互联网 发布:尤克里里推荐 知乎 编辑:程序博客网 时间:2024/05/18 13:44

开发中遇到了安全沙箱的问题,一下有几种解决方案:

1.

播放器在 8.0 以上可以使用通配符 “*” 来允许所有域:
System.security.allowDomain("*");

如果要允许多个域,可以用逗号隔开:
System.security.allowDomain("www.windshow.com", "windshow.com", "player.windshow.com");

2.关于flex跨域读取一个图片

比如  假设你的服务器事  www.myserver.com    而你的一个flex文件位于   www.myserver.com /flex/myfalsh.swf

当一个客户来访问你的这个myfalsh.swf 文件  而你的这个文件又要去 www.otherserver.com/img/1.jpg  这里把这个图片加载到flash中

怎么办? 最简单的方法就是 var l:loader = new loader; l.load(new urlrequest(""));    这个方式如果你的flash是直接放在本地目录上运行那还可以

放在服务器上  加载 立马 报错 安全沙箱冲突

看了看loader 的 load的方法load(request:.chm::/langref/flash/net/URLRequest.html]URLRequest, context:.chm::/langref/flash/system/LoaderContext.html]LoaderContext = null):.chm::/langref/specialTypes.html#void]void

context:.chm::/langref/flash/system/LoaderContext.html]LoaderContext  这个干吗用的? 再看看帮助 原来是用来设置运行域 和 设置是否加载安全策略文件的
当然使用方法是  var lc:.chm::/langref/flash/system/LoaderContext.html]LoaderContext = new .chm::/langref/flash/system/LoaderContext.html]LoaderContext(true);
var l:loader = new loader; l.load(new urlrequest(""),lc);    这样行吗? 一样不行
还要放一个 安全策略文件 放在对方服务器的根目录下面  名称为:crossdomain.xml 最好是这个名字 省得多写代码
内容为

<?xml version="1.0"?>
<?xml version="1.0"?>

 


所以这种方式有个很大弊病 就是要在对方服务器上放这么一个文件 , 别人还肯给你放啊 ? 要不就是傻了
3.很自然的就是想到用代理方法  就是 用asp,php 等类似的程序去把这种图片读回来 然后传给flex 
具体: 放一个如 getpic.asp 在服务器上 和myfalsh.swf 同一个目录
getpic.asp的代码为

Java代码 复制代码
  1. <%   
  2.   
  3. function reReader(url)    
  4. dim http   
  5. set http = server.CreateObject("Microsoft.XMLHTTP")    
  6. with http    
  7. .Open "get", url, false""""    
  8. .Send    
  9. reReader = .ResponseBody   
  10. end with    
  11.   
  12. set http = nothing    
  13. end function   
  14. dim url   
  15. url =Request.QueryString("url")   
  16. response.Clear   
  17. Response.ContentType = "image/gif"  
  18. Response.BinaryWrite reReader(url)   
  19. response.End    
  20. %>  



myfalsh.swf 种的代码这样写

Java代码 复制代码
  1. private var _loader:Loader;   
  2. private var _LoadUrl:String;   
  3. public function FileLoader(url:String){   
  4.     _LoadUrl = url;   
  5.     _loader = new Loader();   
  6. _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,OnLoadCompleateEvent);   
  7. _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,OnLoadIOErrorEvent);   
  8.         }   
  9.            
  10.         //加载   
  11. public function Load():void{   
  12.     var req:URLRequest = new URLRequest(_LoadUrl);   
  13.     _loader.load(req);   
  14. }   
  15.            
  16. public function get content():DisplayObject{   
  17.             return _loader.content;   
  18.         }   
  19.            
  20. private function OnLoadCompleateEvent(e:Event):void{   
  21. this.dispatchEvent(new Event(Event.COMPLETE));   
  22.         }   
  23.            
  24. private function OnLoadIOErrorEvent(e:IOErrorEvent):void{   
  25.     Alert.show("加载错误");   
  26.             //this.dispatchEvent(new Event(IOErrorEvent.IO_ERROR));   
  27.         }  




调用方法
  FileLoader(http://www.myserver.com /flex/myfalsh.swf?url=http://www.otherserver.com/img/1.jpg);

4.BlaseDS时候使用的解决方案
<mx:RemoteObject destination="remo" id="serviceyellow"
        result="ResultHandleYellow(event)" endpoint="http://109.111.4.123:8080/SNUMSUNG/messagebroker/amf" >
    </mx:RemoteObject>
将endpoint中IP地址改为服务器的IP地址

原创粉丝点击