Java 小程序 java.security.AccessControlException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5439568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java applet java.security.AccessControlException
提问by V1tOr
I'm working on an Java applet that prints a file. The applet is "self-signed".
我正在开发一个打印文件的 Java 小程序。小程序是“自签名的”。
The print function is:
打印功能是:
//argFilePath : path to file (http://localhost/Teste/pdf1.pdf)
//argPrintService : something like PrintServiceLookup.lookupDefaultPrintService()
private int print(String argFilePath, PrintService argPrintService){
try
{
DocPrintJob printJob = argPrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(argFilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
I get this exception when trying to open the file:
尝试打开文件时出现此异常:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:80 connect,resolve)
HTML/JavaScrip
HTML/Java 脚本
<input onclick="alert(document.getElementById('xpto').print('http://localhost/Teste/pdf1.pdf'));" type="button"/>
<applet width="180" height="120" code="printers.class" id="xpto" archive="printerAPI.jar"></applet>
is correct to use:
正确使用:
DocFlavor.INPUT_STREAM.AUTOSENSE
The idea seems to be to print as many file type as possible - pdf, docx, jpg, etc.
这个想法似乎是打印尽可能多的文件类型——pdf、docx、jpg 等。
How can you fix the exception?
你如何修复异常?
回答by V1tOr
Found the answer (on stackoverflow lol :D)!
找到了答案(在 stackoverflow 上哈哈:D)!
It looks like the problem was:
看起来问题是:
"javascript does not have file access permissions"
so the applet is blocked. we have to use
所以小程序被阻止了。我们必须使用
AccessController.doPrivileged()
Here is my implementation:
这是我的实现:
private int print(String argFilePath, PrintService argPrintService){
cPrint cP = new cPrint(argFilePath, argPrintService);
return (Integer) AccessController.doPrivileged(cP);
}
class cPrint implements PrivilegedAction<Object> {
String FilePath;
PrintService PrintService;
public cPrint(String argFilePath, PrintService argPrintService) {
this.FilePath = argFilePath;
this.PrintService = argPrintService;
};
public Object run() {
// privileged code goes here
try
{
DocPrintJob printJob = PrintService.createPrintJob();
Doc doc;
DocAttributeSet docAttrSet = new HashDocAttributeSet();
PrintRequestAttributeSet printReqAttr = new HashPrintRequestAttributeSet();
URL url = new URL(FilePath);
doc = new SimpleDoc(url.openStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, docAttrSet);
printJob.print(doc, printReqAttr);
} catch (Exception e) {
System.out.println(e);
return 1;
}
return 0;
}
}
回答by 425nesp
You probably got this:
你可能得到了这个:
java.security.AccessControlException: access denied (java.net.SocketPermission
127.0.0.1:80 connect,resolve)
because applets can't make connections to websites, other than the one they came from. Now, this is terribly silly because one would think localhost
is not another website, but the Java SecurityManager must only look at IP address. Therefore, if the browser is connected to 74.125.224.224
then the Java applet mustconnect to that address—which is different from localhost
, whose address is 127.0.0.1
.
因为小程序不能连接到网站,除了它们来自的网站。现在,这是非常愚蠢的,因为人们会认为localhost
不是另一个网站,而是 Java SecurityManager 必须只查看 IP 地址。因此,如果浏览器连接到 ,74.125.224.224
那么 Java 小程序必须连接到该地址——它不同于localhost
,其地址是127.0.0.1
。
This will just take care of the Socket Permission error. But, you'll probably run into something else if you're trying to access the user's hardware. In which case, you'll need to make a certificate and the user will choose whether or not to run your applet.
这将只处理 Socket Permission 错误。但是,如果您尝试访问用户的硬件,您可能会遇到其他问题。在这种情况下,您需要制作一个证书,用户将选择是否运行您的小程序。
If you just want to run this on your home computer then, you need a plain-text java.policy
file in your home directory. (~/.java.policy for Unix people.) In that file you'll type:
如果您只想在您的家用计算机上运行它,则java.policy
您的主目录中需要一个纯文本文件。(Unix 用户的 ~/.java.policy。)在该文件中,您将键入:
grant{
permission java.security.AllPermission;
};
After you save this file in your home directory, alljava applets will be given full permission to run—anything. It'll be like the SecurityManager doesn't exist, so try to be a bit careful. After you're done with testing, I'd recommend to delete this file.
在您将此文件保存在您的主目录中后,所有Java 小程序都将获得运行的完全权限——任何东西。就像 SecurityManager 不存在一样,所以要小心一点。完成测试后,我建议删除此文件。