[英]Java applet java.security.AccessControlException
I'm working on an Java applet that prints a file. 我正在开发一个打印文件的Java小程序。 The applet is "self-signed".
小程序是“自签名”。
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 /的javascrip
<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? 你怎么解决这个例外?
Found the answer (on stackoverflow lol :D)! 找到答案(在stackoverflow上lol:D)!
It looks like the problem was: 看起来问题是:
"javascript does not have file access permissions" “javascript没有文件访问权限”
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;
}
}
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. 因为applet不能与网站建立连接,除了它们来自网站。 Now, this is terribly silly because one would think
localhost
is not another website, but the Java SecurityManager must only look at IP address. 现在,这非常愚蠢,因为人们会认为
localhost
不是另一个网站,但Java SecurityManager必须只查看IP地址。 Therefore, if the browser is connected to 74.125.224.224
then the Java applet must connect to that address—which is different from
localhost
, whose address is 127.0.0.1
. 因此,如果浏览器连接到
74.125.224.224
那么Java applet 必须连接到该地址 - 该地址与localhost
不同, localhost
的地址为127.0.0.1
。
This will just take care of the Socket Permission error. 这只会处理Socket Permission错误。 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.
在这种情况下,您需要制作证书,用户将选择是否运行您的小程序。
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
文件。 (~/.java.policy for Unix people.) In that file you'll type: (〜/ .java.policy for Unix people。)在该文件中,您将键入:
grant{
permission java.security.AllPermission;
};
After you save this file in your home directory, all java applets will be given full permission to run—anything.
将此文件保存在主目录中后, 所有 Java小程序都将获得运行任何内容的完全权限。 It'll be like the SecurityManager doesn't exist, so try to be a bit careful.
它就像SecurityManager不存在一样,所以尽量小心一点。 After you're done with testing, I'd recommend to delete this file.
完成测试后,我建议删除此文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.