【问题标题】:as3 Security Sandbox Violationas3 安全沙盒违规
【发布时间】:2012-07-11 21:17:56
【问题描述】:

我已经阅读了所有类似的主题,但没有运气,所以我发布了一个关于这个错误的新问题。

我正在尝试使用此代码从另一个 swf 文件加载一个 swf 文件:

var loader:Loader = new Loader()

//listen for loading progress
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);

//listen for when the load is finished
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoaderError);

//load!
var rr:URLRequest = new URLRequest("http://localhost/Gen-Tree.swf")
loader.load(rr);


function onLoaderError(event:SecurityErrorEvent) {
    trace("hi")
}

function onProgress(event:ProgressEvent):void
{
    //calculate how much has been loaded
    var percentageLoader:Number = event.bytesLoaded / event.bytesTotal;

    //use your percentage number here to drive a loader bar graphic
}

function onComplete(event:Event):void
{
    //remove listeners now that loading is done
    loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);

    //add loaded swf to the stage
    addChild(loader.content);

}

我收到如下错误消息:

SecurityDomain 'http://localhost/Gen-Tree.swf' tried to access incompatible context 'file:///C|/Users/Alex/Desktop/Gen%2DTree%202012/Programming/loader.swf'
*** Security Sandbox Violation ***
SecurityDomain 'http://localhost/Gen-Tree.swf' tried to access incompatible context 'file:///C|/Users/Alex/Desktop/Gen%2DTree%202012/Programming/loader.swf'
*** Security Sandbox Violation ***
SecurityDomain 'http://localhost/Gen-Tree.swf' tried to access incompatible context 'file:///C|/Users/Alex/Desktop/Gen%2DTree%202012/Programming/loader.swf'

有什么想法吗?

【问题讨论】:

    标签: actionscript-3 load flash sandbox


    【解决方案1】:

    您正在尝试从文件系统加载外部 swf,这会引发安全错误。将它放在服务器上,它应该可以正常工作,只要两个 swf 在同一个域上。或者运行本地服务器并在其上尝试。

    如果两个 swf 不在同一个域中,您需要添加一个crossdomain.xml。它会在服务器根目录上运行,看起来像这样:

    <?xml version="1.0"?>
    <cross-domain-policy>
        <allow-access-from domain="*" />
    </cross-domain-policy>
    

    除了你不应该只使用*,因为它会让你面临安全风险。您需要专门将其他域列入白名单。您可以了解更多关于跨域策略文件here

    更新:
    此外,由于加载器 swf 正在访问它正在加载的内容(通过loader.content),您需要为该内容 swf 添加安全权限(看起来它被称为Gen-Tress.swf):

    import flash.system.Security;
    
    Security.allowDomain("*");
    

    还值得注意的是,Loader 是一个DisplayObject,这意味着您可以使用addChild(loader) 而不是addChild(loader.content) 直接将其添加到stage。通过不访问Loader 的内容,您通常可以避免安全沙箱违规错误,并且不必处理允许域和跨域策略。

    【讨论】:

    • 实际上我正在运行本地 Wamp 服务器。如果我把它上传到某个地方会有什么不同?
    • 如果您在 Wamp 上运行,为什么要硬编码文件系统路径来加载 SWF?而不是file:///C|/Users/Alex...,应该是http://localhost/loader.swf
    • 我不是,是 loader.swf 正在访问 Gen-Tree.swf,反之亦然。由于这个原因,错误声明令人困惑。
    • 你是通过 Wamp 打开加载器 swf 吗?还是直接在浏览器中?给出文件系统路径的错误很奇怪。
    • 如果我从浏览器打开 loader.swf,我得到的只是一个空白屏幕。当我直接从 Flash 执行它时,我得到上面提到的错误。确实,这有点奇怪。
    【解决方案2】:

    您可以尝试加载 swf,然后使用 loader.loadBytes() 重新加载加载器内容。这样您就可以创建 swf 的 bytearray 克隆并绕过安全沙箱。到目前为止,它与图像配合得很好。我在我的博客上解释了这个过程:http://www.inklink.co.at/blog/?p=14

    图片示例如下:

    function loadPicture():void
    {
        var req:URLRequest = new URLRequest("YOUR_URL_HERE");
        var _picLoader:Loader = new Loader();
        _picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loader2ByteArray);
        _picLoader.load(req);
    }
     
    function loader2ByteArray(evt:Event):void
    {
        var lInfo:LoaderInfo = LoaderInfo(evt.target);
        var ba:ByteArray = lInfo.bytes;
        reloadByteArray(ba);
    }
     
    function reloadByteArray(ba:ByteArray):void
    {
        var reloader:Loader = new Loader();
        reloader.loadBytes(ba);
        reloader.contentLoaderInfo.addEventListener(Event.COMPLETE, reloaderComplete);
    }
     
    function reloaderComplete(evt:Event):void
    {
        var imageInfo:LoaderInfo = LoaderInfo(evt.target);
        var bmd:BitmapData = new BitmapData(imageInfo.width,imageInfo.height);
        bmd.draw(imageInfo.loader);
        var resultBitmap:Bitmap = new Bitmap(bmd);
        addChild(resultBitmap);
    }
    

    希望对你有帮助!

    【讨论】:

    • 这太骇人听闻了。
    • @Saariko 这不再起作用了。它在某些版本之前被 Adob​​e 修补过......
    【解决方案3】:
    var loader_context:LoaderContext = new LoaderContext();
    if (Security.sandboxType!='localTrusted') loader_context.securityDomain = SecurityDomain.currentDomain;
    loader_context.applicationDomain = ApplicationDomain.currentDomain;
    currentLoader = new Loader();
    currentLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
    currentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
    currentLoader.load(new URLRequest(baseLink + pName + ".swf"), loader_context);
    

    也许你可以尝试添加loadercontext

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多