从 java applet 中写入文件时遇到问题
我创建了简单的 Java 类来测试小程序的文件写入:
更新出现
public class localfile extends Applet{
public localfile(){
try {
File f = new File("testfile.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
out.write("test");
out.close();
}
catch(Exception x)
System.err.println(x.toString());
}
}
我已经创建并签署了 jar:
jar cvf localfile.jar localfile.java
jarsigner localfile.jar yourkey
html 如下所示:
每次运行此小程序时出现的错误是:
java.lang.SecurityException: trusted loader attempted to load sandboxed resource from file:/home/w/test/
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.check(CPCallbackHandler.java:308)
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.access$1400(CPCallbackHandler.java:121)
at com.sun.deploy.security.CPCallbackHandler$ChildElement.checkResource(CPCallbackHandler.java:473)
at sun.plugin2.applet.Plugin2ClassLoader.checkResource(Plugin2ClassLoader.java:701)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:206)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:520)
at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:2940)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1444)
at java.lang.Thread.run(Thread.java:619)
Exception: java.lang.SecurityException: trusted loader attempted to load sandboxed resource from file:/home/w/test/
奇怪的是:我创建了类似的小程序来读取文件,并且工作正常。
有什么想法吗?
我在浏览器和小程序查看器上运行这个小程序。奇怪的是,小程序在小程序查看器上不起作用并引发异常,但在浏览器上却没问题。
java.security.AccessControlException: access denied (java.util.PropertyPermission java.security.policy write)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.System.setProperty(System.java:725)
at localfile.<init>(localfile.java:15)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:619)
因此,除了这种奇怪的行为之外,我认为我的问题已经解决了。谢谢大家:)
I have created simple Java class to test file writes from applets:
update appeared
public class localfile extends Applet{
public localfile(){
try {
File f = new File("testfile.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
out.write("test");
out.close();
}
catch(Exception x)
System.err.println(x.toString());
}
}
I have created and signed jar:
jar cvf localfile.jar localfile.java
jarsigner localfile.jar yourkey
html looks like:<applet code="localfile.class" archive="localfile.jar", width=300, height=600
>
The error I get every time I run this applet is:
java.lang.SecurityException: trusted loader attempted to load sandboxed resource from file:/home/w/test/
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.check(CPCallbackHandler.java:308)
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.access$1400(CPCallbackHandler.java:121)
at com.sun.deploy.security.CPCallbackHandler$ChildElement.checkResource(CPCallbackHandler.java:473)
at sun.plugin2.applet.Plugin2ClassLoader.checkResource(Plugin2ClassLoader.java:701)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:206)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:520)
at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:2940)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1444)
at java.lang.Thread.run(Thread.java:619)
Exception: java.lang.SecurityException: trusted loader attempted to load sandboxed resource from file:/home/w/test/
What is strange: I have created similar applet to read files and it works ok.
Any thoughts?
I was running this applet on both browser and applet viewer. What is strange given applet doesn't work on applet viewer and throws exception, but on browser it is fine.
java.security.AccessControlException: access denied (java.util.PropertyPermission java.security.policy write)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.System.setProperty(System.java:725)
at localfile.<init>(localfile.java:15)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:619)
So, beside this strange behaviour I consider my problem solved. Thanks everyone:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否提供了允许从文件系统读取文件的策略?
看来您只签署了 jar,但没有使用policytool。
Did you provide a policy to allow reading files from the filesystem?
Seems that you only signed the jar but didn't use policytool.
通过一些花招,您可以将策略文件包含在 jar 中。有关详细信息,请参阅 SO 问题 jar 策略文件。
否则,请考虑制作一个 Java WebStart 应用程序,它可以更轻松地读取/写入文件。
With some finagling, you can include a policy file in a jar. Refer to the SO question jar policy file for more information.
Otherwise, consider making a Java WebStart application, which can read/write files more easily.
http://java.sun.com/docs/books/ tutorial/security/tour1/step2.html
这应该可以帮助您创建策略文件并与您的代码库关联
http://java.sun.com/docs/books/tutorial/security/tour1/step2.html
This should help you out with creating the policy file and associating with your code base
我知道这已经很晚了。但只是为了帮助任何人查找此错误 -
使用 Ant,可以一次性签署多个 jar,例如 java-comm.jar 等
这将签署目录中的所有 jar。
I knew this is quite late. But just to help whoever look-up to this error -
Using Ant, multiple jars can be signed at one shot, for example java-comm.jar etc
This wil sign all the jar in the directory.
我相信您的问题是,包含您尝试加载的文件的目录位于查找类文件和应用程序资源的代码库中。因此,您最终会混合可信和不可信的资源,这是不安全的。如果小程序托管在 http 或更好的 https 服务器上,则文件不会出现此问题。
请注意,您可以使用小程序的 JNLP API 通过文件对话框“打开”或写入文件。
在出现异常的情况下,您的资源处理会使文件保持打开状态。资源处理应按以下格式编写:
在您的具体情况下:
I believe your problem is that directory including the file you are trying to load is within the codebase where class files and application resources are looked up. So, you end up mixing trusted and untrusted resources, which is not secure. If the applet is hosted on an http, or better https, server then the issue doesn't arise for files.
Note you can use the JNLP APIs for applets to "open" or write files through a file dialog.
Your resource handling leaves the file open in the case of exceptions. Resource handling should be written in the style:
In your specific case: