簡體   English   中英

Java applet java.security.AccessControlException

[英]Java applet java.security.AccessControlException

我正在開發一個打印文件的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等。

你怎么解決這個例外?

找到答案(在stackoverflow上lol: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;
    }
}

你可能得到了這個:

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

因為applet不能與網站建立連接,除了它們來自網站。 現在,這非常愚蠢,因為人們會認為localhost不是另一個網站,但Java SecurityManager必須只查看IP地址。 因此,如果瀏覽器連接到74.125.224.224那么Java applet 必須連接到該地址 - 該地址與localhost不同, localhost的地址為127.0.0.1

這只會處理Socket Permission錯誤。 但是,如果您嘗試訪問用戶的硬件,您可能會遇到其他問題。 在這種情況下,您需要制作證書,用戶將選擇是否運行您的小程序。

如果您只想在家用計算機上運行此程序,則需要在主目錄中使用純文本java.policy文件。 (〜/ .java.policy for Unix people。)在該文件中,您將鍵入:

grant{
    permission java.security.AllPermission;
};

將此文件保存在主目錄中后, 所有 Java小程序都將獲得運行任何內容的完全權限。 它就像SecurityManager不存在一樣,所以盡量小心一點。 完成測試后,我建議刪除此文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM