签名Java Applet在从JavaScript调用时提供AccessControlException

时间:2012-06-28 20:12:10

标签: java javascript swing security applet

首先,我知道我的问题与in here相同。但这个问题对我没有帮助。

我有自签名小程序。

jarsigner -verify sJSApplet.jar
jar verified.

Warning:
This jar contains entries whose signer certificate will expire within six months.

Applet的目的是从LAN机器打开MS Word文档。 到目前为止,我已尝试使用Desktop.open()Runtime.exec()开放。使用AccessController.doPrivileged而不使用java.security.AccessControlException: access denied。我总是得到<html> <head> <script> function openFile( command ) { var applet = "<object type='application/x-java-applet' height='100' width='100' name='jsApplet'><param name='code' value='com.avacoda.swing.JSApplet'/><param name='archive' value='sJSApplet.jar' /><param name='mayscript' value='true'/><param name='filePath' value='C:\\note.txt'/>Applet failed to run. No Java plug-in was found.</object>"; var body = document.getElementsByTagName("body")[0]; var div = document.createElement("div"); div.innerHTML = applet; body.appendChild(div); } </script> </head> <body> <a href="#" onclick="openFile('C:/note.txt');">Open file</a> </body> </html>

我没有选择权。我还能做什么?

我不能使用java.policy文件。

HTML

public class WordApplet extends JApplet {

    @Override
    public void init() {
        openFile(getParameter("filePath"));
    };

    public void openFile(final String path) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {

            @Override
            public Object run() {
                try {
                        Runtime.getRuntime().exec("winword " + path); 
                        //Desktop.getDesktop().open(new File(path));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }
}

Java代码:

java.security.AccessControlException: access denied (java.io.FilePermission <<ALL FILES>> execute)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkExec(Unknown Source)
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at com.test.applet.JSApplet$1.run(JSApplet.java:34)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.avacoda.swing.JSApplet.openFile(JSApplet.java:29)
    at com.avacoda.swing.JSApplet.init(JSApplet.java:25)
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.init(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
basic: Applet initialized

完整堆栈跟踪

{{1}}

2 个答案:

答案 0 :(得分:0)

本来不允许您从applet在用户的计算机上执行代码。这可能会导致很多麻烦。但是,请考虑使用Applet将文件输出到用户的计算机,然后使用jScript和ActiveX在记事本中打开文档。我已经在网上找到了一个例子:

<html>
    <head>
        <script type="text/javascript">
            function runApp(which) {
                WshShell = new ActiveXObject("WScript.Shell");
                WshShell.Run (which,1,false);
            }
        </script>
    </head>
    <body>
        <!-- Two ways to create a link to run the app. -->
        <font onClick="runApp('file://c:/winnt/notepad.exe');" style="cursor: hand;">
            <u>Notepad</u>
        </font>
        <br>
        <!-- Or use <a> descriptor -->
        <a href="runApp('file://c:/test.bat');">Batch File</a>
    </body>
</html>

答案 1 :(得分:0)

上面的代码示例没有任何问题。 Desktop.getDesktop().open()Runtime.getRuntime().exec()这两种情况都非常有效。

我的问题是包装严重的罐子。