简体   繁体   中英

Java applet java.security.AccessControlException

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?

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

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 connect to that address—which is different from localhost , whose address is 127.0.0.1 . 连接到该地址 - 该地址与localhost不同, 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.

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, 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. After you're done with testing, I'd recommend to delete this file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM