關於Flex安全沙箱問題的解決

來源:互聯網
上載者:User

前些天做了一個flex的小程式,通過訪問服務前端的xml來更新資料,在本地調試調用本地的xml檔案是沒有問題的,可把url換成伺服器端時就出現了Security Error,通過上網尋找斷定是安全沙箱的問題,下面是在網上找到的解決安全沙箱問題的方法,僅供參考。

方法一:在目標伺服器上布署crossdomain.xml檔案(我用的此方法很管用,放上就沒問題了) 需要遠程服務根目錄定義有crossdomain.xml檔案,如下:

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

方法二:使用代理,把Flex要訪問的遠程檔案通過asp, php, jsp等指令碼讀取到本地,然後再由Flex去訪問;

方法三:使用Adobe flash player 9 開啟程式後,點擊功能表列中檔案->建立播放器...即產生exe檔案,運行exe檔案即可突破安全限制;

方法四:     1、找到這個檔案夾:c:\Documents and Settings\<UserName>\Application         Data\Macromedia\Flash Player\#Security     2、在其下建立一個名為"FlashPlayerTrust"的檔案夾     3、在"FlashPlayerTrust"檔案夾下建立一TXT檔案,內容如下:         c:\         d:\         e:\         f:\     4、將該txt檔案命名為:"myTrustFiles.cfg"     再開啟你硬碟裡的SWF檔案,就不會出現那個煩人的安全設定提示視窗了!

方法五: 用HttpService它預設是有Proxy的,需要配置flex-config.xml,裡面有一段: <http-service-proxy>      <whitelist>         ………………     </whitelist> </http-service-proxy>

這個是白名單,一般情況下是注釋掉的,也就是預設只有本地的http://{localserver}/*和https://{localserver}/*可以訪問。其他的需要在flex-config.xml裡的自行修改成需要的就可以了。

訪問本地自然不會跨域,不過你肯定訪問區域網路其他機器了,所以是依照白名單規則,屬於跨域

 

Socket沙箱問題

在Flex中使用Socket進行通訊時,也會受到Flash9的新安全性原則的困擾. 解決方案不能像在Web伺服器中布置一個crossdomain.xml來解決,或是在伺服器上專門開啟843連接埠來提供安全性原則. 有一種方法就是在接收到用戶端的串連後,向其發送 安全性原則.

比如我是用JAVA來開發, 用戶端的Flex會先搜尋同域,及伺服器的843口,看是否能得到安全性原則,這時候Socket是先建立好的,可以在接收到Socket ,即Accept事件發生是,馬上向其發送 策略串,否則用戶端就會因為安全性原則不過關,於斷開, 如果成功擷取策略,則用戶端將斷掉先前的那次Socket, 再真正進行程式中你要求的Socket串連請求.

下面看看我找的例子吧!

public void run() {
ServerSocket ss;
String ip = "";
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
String xml = "<cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/>";
xml = xml + "<allow-access-from domain=\"" + ip + "\" to-ports=\"1234\" />";
xml = xml + "</cross-domain-policy>";

try {
ss = new ServerSocket(port);
while (!cancle) {
Socket s = ss.accept();

BufferedReader br = new BufferedReader(new InputStreamReader(s
.getInputStream(), "UTF-8"));
PrintWriter pw = new PrintWriter(s.getOutputStream());
char[] by = new char[22];
br.read(by, 0, 22);
String head = new String(by);
                                     //判斷是不是第一求請求串連的安全驗證,當用戶端連socket時,as3會自動向服務端發送<policy-file-request/>這個字串請求返回策略檔案,所以當伺服器收到這個串後給client返回就好了。
                                           //如果是返回xML資訊
if (head.equals("<policy-file-request/>")) {
System.out.println("串連伺服器");
pw.print(xml + "\0");
pw.flush();
br.close();
pw.close();
} else {
               //你自己的正常請求處理邏輯
}

}
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}

 

 

   <http-service-proxy>
   ..........................
  <whitelist>
   <!-- whitelist config for unnamed services -->
   <unnamed>
   <url>http://{localserver}/*</url>
   <url>https://{localserver}/*</url
  ..............................................
  </unnamed>
   <!-- whitelist config for named services -->
   <named>
  ....................
  <service name="SampleEmployeeSrv">
   <url>{context.root}/explorer/data/employees.jsp</url>
   <use-custom-authentication>true</use-custom-authentication>
   </service>
  ................................................
  再看在flex目錄下的 flex-config.xml檔案
  <whitelist>
   <!-- whitelist config for unnamed services -->
   <unnamed>
   <url></url>
   <!--
   For security, the whitelist is locked down by default.
   Uncomment the first two urls below to enable access to all URLs,
   or the last two urls to enable access to the local server,
   or add above the individual URLs you wish to access.
   <url>http://*</url>
   <url>https://*</url>
   <url>http://{localserver}/*</url>
   <url>https://{localserver}/*</url>
   -->
   </unnamed>
   <!-- whitelist config for named http services -->
   <!-- <named> -->
   <!-- define an http service which may be referenced by name from mxml -->
   <!-- <service name="service"> -->
   <!-- enables use of custom fault code on the client for handling authentication failures -->
   <!-- <use-custom-authentication>false</use-custom-authentication> -->
   <!-- actual url to use when accessing the named http service -->
   <!-- <url>http://localhost:8100/flex/servicename</url> -->
   <!-- user-name and password to use when accessing this http service -->
   <!-- <run-as user="user" password="pwd"/> -->
   <!-- Adds the service's url to the unnamed whitelist. If false, these can never be used unnamed -->
   <!-- This should be set to false if using web application security with this named service -->
   <!-- <allow-unnamed-access>true</allow-unnamed-access> -->
   <!-- </service> -->
   <!-- </named> -->
   </whitelist>
  於是,我把
  <url>http://*</url>
   <url>https://*</url>
   <url>http://{localserver}/*</url>
   <url>https://{localserver}/*</url>
  這段的注釋符去掉就可以了。
  還有一種訪問servlet的方法就是通過named來的,如在上面<named>...</named>加上
   <service name="ServletService">
   <url>{context.root}/servlet/XMLFacadeServlet</url>
   <use-custom-authentication>true</use-custom-authentication>
   </service>
  mxml這樣使用:
  <mx:HTTPService id="ServletService1" serviceName="ServletService">
  mx:HTTPService 需要一個send操作才能執行發送GET或POST命令以獲得資料,如:
   <mx:Button label="Get Employee List" click="employeeSrv.send();"/>
  修改flex-config.xml後要重起tomcat的,如果你修改後flex-config.xml存在問題(如:把named寫成name),
  訪問mxml的時候就會出現 url is not available 這個錯誤了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.