我有一個與Java Applet安全性有關的問題/問題...
我使用Applet必須從服務器(ASP.NET)獲取文件並從中表示信息。小程序使用的代碼需要的文件:對於Java Applets,http:// localhost:8000 /和http://127.0.0.1:8000/之間的區別是什麼
URL u = new URL(getCodeBase(), filename);
BufferedReader d = new BufferedReader(new InputStreamReader(u.openStream()));
出現此代碼在兩個地方:
Init()
方法- 一些其他方法
Test()
是人工呼叫從JavaScript
所以,當我嘗試使用URL http://127.0.0.1:8000/Test.aspx
加載Applet的頁面,一切工作正常,我可以從兩個m中讀取文件內容編制方法。但是,如果我改變http://localhost:8000/
的URL,只有第一種方法正常工作,我可以得到的文件內容,併爲第二個我得到JavaConsole一個錯誤信息:
java.security.AccessControlException:訪問被拒絕( java.net.SocketPermission 127.0.0.1:8000連接,解析)
在這種情況下有什麼區別?爲什麼'localhost'在這種情況下是不可能的?有沒有辦法像127.0.0.1一樣授予對'localhost'的訪問權限?
這裏是最簡單的小程序的例子:
public class TestApplet extends Applet {
public void init()
{
System.out.println("init...");
readDocument();
}
public void readDocument()
{
System.out.println("read test.txt file...");
URL base = getCodeBase();
String filename = "test.txt";
try {
URL u = new URL(base, filename);
BufferedReader d = new BufferedReader(new InputStreamReader(u.openStream()));
System.out.println(d.readLine());
System.out.println("Done!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
,並在客戶端使用下面的代碼:
<applet archive="/Content/test.jar" code="test.TestApplet.class" name="testApplet" mayscript></applet>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var testApplet = document.testApplet;
testApplet.readDocument();
});
</script>
此代碼的工作完美,當我嘗試使用http://127.0.0.1:8000/Test.aspx 和不工作的時候我用戶http://localhost:8000/Test.aspx。我java控制檯我看到下一個:
init...
read test.txt file...
some text...
Done!
read test.txt file...
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:8000 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at test.TestApplet.readDocument(TestApplet.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.plugin.javascript.JSInvoke.invoke(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
at sun.plugin2.liveconnect.JavaClass$MethodInfo.invoke(Unknown Source)
at sun.plugin2.liveconnect.JavaClass$MemberBundle.invoke(Unknown Source)
at sun.plugin2.liveconnect.JavaClass.invoke0(Unknown Source)
at sun.plugin2.liveconnect.JavaClass.invoke(Unknown Source)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$DefaultInvocationDelegate.invoke(Unknown Source)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo.doObjectOp(Unknown Source)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$LiveConnectWorker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
P.S .: Applet已簽名。
在您訪問「127.0.0.1」這兩種情況下,我沒有看到你所說的「本地主機」,或與本地主機的一個問題... – woliveirajr
怎樣的JavaScript調用'Test'方法?它如何創建URL?你能確保它使用與Applet在瀏覽器中相同的URL嗎? – sudocode
是...拼寫錯誤...已更改 –