Java 小程序 java.security.AccessControlException

发布于 2024-10-26 22:09:18 字数 1420 浏览 1 评论 0原文

我正在开发一个打印文件的 Java 小程序。 该小程序是“自签名的”。

打印功能是:

//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;
}

尝试打开文件时出现此异常:

java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:80 connect,resolve)

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>

使用正确:

DocFlavor.INPUT_STREAM.AUTOSENSE

这个想法似乎是打印尽可能多的文件类型 - pdf、docx、jpg 等。

您如何解决例外吗?

I'm working on an Java applet that prints a file.
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

<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.

How can you fix the exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2024-11-02 22:09:18

找到了答案(在 stackoverflow 上,哈哈:D)!

看起来问题是:

"javascript 没有文件访问权限”

因此该小程序被阻止。我们必须使用

AccessController.doPrivileged()

doPrivileged

这是我的实现:

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;
    }
}

Found the answer (on stackoverflow lol :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()

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;
    }
}
青衫儰鉨ミ守葔 2024-11-02 22:09:18

您可能了解到这一点:

java.security.AccessControlException: access denied (java.net.SocketPermission
127.0.0.1:80 connect,resolve)

因为小程序无法连接到除它们来源的网站之外的网站。现在,这是非常愚蠢的,因为人们会认为 localhost 不是另一个网站,但 Java SecurityManager 必须只查看 IP 地址。因此,如果浏览器连接到 74.125.224.224,那么 Java 小程序必须 连接到该地址,这与 localhost 不同,后者的地址是127.0.0.1

这只会解决套接字权限错误。但是,如果您尝试访问用户的硬件,您可能会遇到其他问题。在这种情况下,您需要制作一个证书,并且用户将选择是否运行您的小程序。

如果您只想在家庭计算机上运行它,那么您的主目录中需要一个纯文本 java.policy 文件。 (~/.java.policy 对于 Unix 用户。)在该文件中,您将键入:

grant{
    permission java.security.AllPermission;
};

将此文件保存到主目录后,所有 java 小程序将被授予运行任何内容的完全权限。就像 SecurityManager 不存在一样,所以要小心一点。完成测试后,我建议删除此文件。

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 must connect to that address—which is different from localhost, whose address is 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.

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:

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文