我从未签名的小程序访问本地文件时遇到问题。我阅读了很多不同的资源(oracle、sun 文档、论坛,包括 stackoverflow),他们都告诉我,我可以修复安全策略,而不是签署小程序。
我想做的是播放 .wav 文件。为了简化,我将所有文件放在同一个目录中:
D:\<username>\IdeaProjects\SimpleAppletTest\out\production\SimpleAppletTest
在这个目录中,我有下一个文件:
01.wav
SimpleApplet.class
SimpleApplet.html
.java.policy
.java.policy
我使用创建的文件policytool
具有以下内容:
/* AUTOMATICALLY GENERATED ON Fri Mar 13 20:44:28 MSK 2015*/
/* DO NOT EDIT */
grant codeBase "file:/D:/<username>/IdeaProjects/-" {
permission java.io.FilePermission "<<ALL FILES>>", "read";
};
此外,我添加了下一个字符串(第三个)java.security
位于${java.home}/lib/security/
:
policy.url.1=file:${java.home}/lib/security/java.policy
policy.url.2=file:${user.home}/.java.policy
policy.url.3=file:/D:/<username>/IdeaProjects/SimpleAppletTest/out/production/SimpleAppletTest/.java.policy
但是当我在 Internet Explorer 中启动我的小程序时(请不要开玩笑,我只是用它来测试我的小程序)我在 java-console 中有下一个堆栈跟踪:
Java Plug-in 11.31.2.13
Using JRE version 1.8.0_31-b13 Java HotSpot(TM) Client VM
User home directory = D:\<username>
...
Exception in thread "AWT-EventQueue-2" java.security.AccessControlException: access denied ("java.io.FilePermission" "D:\<username>\IdeaProjects\SimpleAppletTest\out\production\SimpleAppletTest\01.wav" "read")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at sun.plugin2.applet.AWTAppletSecurityManager.checkPermission(Unknown Source)
at SimpleApplet$1$1.run(SimpleApplet.java:56)
at java.security.AccessController.doPrivileged(Native Method)
at SimpleApplet$1.run(SimpleApplet.java:33)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
小程序代码:
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
File audioSource = new File(
"D:\\<username>\\IdeaProjects\\SimpleAppletTest\\out\\production\\SimpleAppletTest",
"01.wav");
FilePermission perm = new FilePermission(audioSource.getAbsolutePath(), "read");
SecurityManager appsm = System.getSecurityManager();
appsm.checkPermission(perm);
return null;
}
});
我的 JDK 和 JRE 版本:
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
实际上,我有 Windows 7。再一次,我只是想在没有签署我的小程序的情况下播放 .wav文件。