AS3:SecurityError:Error #2121:Security sandbox violation:LoaderInfo.content: x cannotaccess y.This maybe worked around by calling Security.allowDomain

huangapple go评论59阅读模式
英文:

AS3:SecurityError:Error #2121:Security sandbox violation:LoaderInfo.content: x cannotaccess y.This maybe worked around by calling Security.allowDomain

问题

以下是您要翻译的部分:

我在本地运行我的swf时遇到了这个错误(在浏览器中也不起作用):
SecurityError: 错误 #2121:安全沙箱冲突:LoaderInfo.content:file:///C|/Users/Admin/Desktop/whatever.swf 无法访问 https://content.mspcdns.com/swf/headwear/story_2022_girlhat_tk.swf。可以通过调用Security.allowDomain来解决此问题。
	at flash.display::LoaderInfo/get content()
	at Function/com.mspicker.utils:SWFLoader/loadSWF/com.x.utils:onComplete()[C:\Users\x\Adobe Flash Builder 4.6\whatever\src\com\x\utils\SWFLoader.as:77]

我在互联网上搜索了解如何通过allow domain来解决安全问题,但仍然无法解决,我不知道该怎么办!

content.mspcdns.com的crossdomain.xml文件如下:

<?xml version="1.0"?>
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" secure="false"/>
<allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy> 

这是我的当前代码(错误发生在第77行 -> catch(error:Error) at onComplete(e:Event)):

public function loadSWF(param1: String):void {
	var fullUrl:String = param1;
	//fullUrl 例如:https://content.mspcdns.com/swf/headwear/story_2022_girlhat_tk.swf
	var loader:Loader = new Loader();
			
	Security.allowDomain("*");
	Security.allowInsecureDomain("*");
	Security.allowDomain(loader.contentLoaderInfo.url);
	Security.loadPolicyFile("https://content.mspcdns.com/crossdomain.xml");
			
	var context: LoaderContext = new LoaderContext();
	context.allowCodeImport = true;
	loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			
	try {
		loader.load(new URLRequest(fullUrl), context);
	} catch(error:Error) {
		ErrorHandler.triggerError("error", "无法加载SWF文件!");
		return;
	}
			
	function onComplete(e:Event):void {
		var movie:MovieClip = new MovieClip();
		try {
			movie = e.currentTarget.content;
		} catch(error:Error) {
			//当无法转换为MovieClip时,将其转换为Bitmap
			var bitmapData:BitmapData = new BitmapData(e.currentTarget.content.width, e.currentTarget.content.height, true, 0x00000000);
			bitmapData.draw(e.currentTarget.content);
					
			var bitmap:Bitmap = new Bitmap(bitmapData);
			//将Bitmap添加到MovieClip中
			movie.addChild(bitmap);	
		}
				
		movie.x = 25;
		movie.y = 30;
				
		stage.addChild(movie);
	}
}

也许有经验的人可以帮助我解决这个问题?
谢谢


<details>
<summary>英文:</summary>

I get this error when I run my swf locally (also doesn&#39;t work with browser):

SecurityError: Error #2121: Security sandbox violation: LoaderInfo.content: file:///C|/Users/Admin/Desktop/whatever.swf cannot access https://content.mspcdns.com/swf/headwear/story_2022_girlhat_tk.swf. This may be worked around by calling Security.allowDomain.
at flash.display::LoaderInfo/get content()
at Function/com.mspicker.utils:SWFLoader/loadSWF/com.x.utils:onComplete()[C:\Users\x\Adobe Flash Builder 4.6\whatever\src\com\x\utils\SWFLoader.as:77]

I searched on the internet to fix the security problem with allow domain, but it is still not working and I don&#39;t know what to do!

The crossdomain.xml file of content.mspcdns.com looks like this:

<?xml version="1.0"?>
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="" secure="false"/>
<allow-http-request-headers-from domain="
" headers="*" secure="false"/>
</cross-domain-policy>


This is my current code (The error occurs at line 77 -&gt; here after catch(error:Error) at onComplete(e:Event)):

public function loadSWF(param1: String):void {
var fullUrl:String = param1;
//fullUrl for example: https://content.mspcdns.com/swf/headwear/story_2022_girlhat_tk.swf
var loader:Loader = new Loader();

Security.allowDomain(&quot;*&quot;);
Security.allowInsecureDomain(&quot;*&quot;);
Security.allowDomain(loader.contentLoaderInfo.url);
Security.loadPolicyFile(&quot;https://content.mspcdns.com/crossdomain.xml&quot;);
		
var context: LoaderContext = new LoaderContext();
context.allowCodeImport = true;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
		
try {
	loader.load(new URLRequest(fullUrl), context);
} catch(error:Error) {
	ErrorHandler.triggerError(&quot;error&quot;, &quot;Could not load SWF file!&quot;);
	return;
}
		
function onComplete(e:Event):void {
	var movie:MovieClip = new MovieClip();
	try {
		movie = e.currentTarget.content;
	} catch(error:Error) {
		//Convert it to Bitmap, when it can&#39;t be converted to MovieClip
		var bitmapData:BitmapData = new BitmapData(e.currentTarget.content.width, e.currentTarget.content.height, true, 0x00000000);
		bitmapData.draw(e.currentTarget.content);
				
		var bitmap:Bitmap = new Bitmap(bitmapData);
		//Add Bitmap to MovieClip
		movie.addChild(bitmap);	
	}
			
	movie.x = 25;
	movie.y = 30;
			
	stage.addChild(movie);
}

}


Maybe someone is experienced enough to help me out with this problem?
Thank you

</details>


# 答案1
**得分**: 1

我自己找到了一个解决方案,但可能不是最佳的,只能在 swf 在 Web 上运行时才有效:

由于问题是访问外部域中的 swf,我创建了一个 PHP 脚本,它将从外部域中获取 swf 并将其回显。PHP 文件位于与 SWF 文件相同的服务器上。
PHP 脚本如下(q = swf 文件的完整 URL):

```php
<?php
header('Content-Type: application/x-shockwave-flash');
echo file_get_contents($_GET["q"]);

如果有更好的解决方案,请告诉我!

英文:

I found a solution myself, but its probably not the best one and works only if the swf is run on the web:

Since the problem has been to access the swf from an external domain, I created a PHP script which will fetch the swf from the external domain and echo it. The PHP file is on the same server as the SWF file.
The PHP script looks like this (q = the full url to the swf file):

&lt;?php
header(&#39;Content-Type: application/x-shockwave-flash&#39;);
echo file_get_contents($_GET[&quot;q&quot;]);

If someone has a better solution, please let me know!

huangapple
  • 本文由 发表于 2023年1月8日 23:10:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定