Applet在eclipse中运行,但在浏览器中运行 - java安全性

时间:2012-12-26 07:16:23

标签: java security applet accesscontrolexception policyfiles

下面是applet代码,它使用jna.jar(https://github.com/twall/jna)来访问system32中的DLL文件。

import javax.swing.*;
import javax.print.*;

import java.security.*;
import java.util.ArrayList;
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.io.StringWriter;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;

public class CallApplet extends JApplet implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 4654357272074276081L;
    static JTextField output;
    private final String ButtonText = "Print";

    public void init() {

        Container contentHolder = getContentPane();

        contentHolder.setLayout(new BorderLayout(18,18));

        output = new JTextField(20);
        //add(output);

        contentHolder.add(output, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();

        JButton b = new JButton(ButtonText);
        b.addActionListener(this);

        buttonPanel.add(b);
        contentHolder.add(buttonPanel, BorderLayout.SOUTH);
        validate();
    }

    public void actionPerformed(ActionEvent evt)
    {
        //get the text of the button that was pushed
        String command = evt.getActionCommand();

        output.setText(command);

        // if myButton was pressed, output a message
        if(ButtonText.equals(command)) {
            try {
                this.mprintt("TSC TTP-244 Plus");
            } catch (Exception e) {

            }
        }
    }

    public void setMessage(String message) {
        output.setText(message);
    }

    public interface TscLibDll extends Library {
        TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary ("TSCLIB", TscLibDll.class);
        int about ();
        int openport (String pirnterName);
        int closeport ();
        int sendcommand (String printerCommand);
        int setup (String width,String height,String speed,String density,String sensor,String vertical,String offset);
        int downloadpcx (String filename,String image_name);
        int barcode (String x,String y,String type,String height,String readable,String rotation,String narrow,String wide,String code);
        int printerfont (String x,String y,String fonttype,String rotation,String xmul,String ymul,String text);
        int clearbuffer ();
        int printlabel (String set, String copy);
        int formfeed ();
        int nobackfeed ();
        int windowsfont (int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
    }

    public void mprintt(String printer) {

        try {
            //TscLibDll.INSTANCE.about();
            TscLibDll.INSTANCE.openport(printer);
            //TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX", "UL.PCX");
            TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");
            TscLibDll.INSTANCE.setup("35", "15", "3", "8", "0", "3", "-1");
            TscLibDll.INSTANCE.clearbuffer();
            //TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\"");
            TscLibDll.INSTANCE.printerfont ("290", "8", "3", "0", "1", "1", "ARTICLE NO");
            TscLibDll.INSTANCE.barcode("290", "35", "128", "50", "1", "0", "2", "2", "123456789");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 0, 3, 1, "arial", "DEG 0");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 90, 3, 1, "arial", "DEG 90");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 180, 3, 1, "arial", "DEG 180");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 270, 3, 1, "arial", "DEG 270");
            TscLibDll.INSTANCE.printlabel("1", "1");
            TscLibDll.INSTANCE.closeport();
            output.setText("printed");
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            output.setText(sw.toString());
        }
    }
}

我的Html代码

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<applet codebase ="." code="CallApplet.class"
        archive="CallApplet.jar,jna.jar,platform.jar"
        height="100" width="100"/>
</body>
</html>

当我尝试点击打印时,我可以看到applet已加载并且浏览器中的所有swing元素都没有做任何事情。在eclipse运行中它运行正常。

我知道这是安全问题所以我还添加了策略(C:\ Program Files \ Java \ jre7 \ lib \ security \ java.policy文件)

grant codeBase "http://localhost:8080/appletproj/*" {
    permission java.security.AllPermission;
};

堆栈跟踪

Exception in thread "AWT-EventQueue-7" java.lang.NoClassDefFoundError: com/sun/jna/Library
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.defineClassHelper(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.access$100(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.plugin2.applet.Plugin2ClassLoader.findClassHelper(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at CallApplet.mprintt(CallApplet.java:185)
    at CallApplet.actionPerformed(CallApplet.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 55 more

但即便如此也无济于事。请指导我哪里出错了。

坦率地说,我没有得到解决方案,而不是安全问题所以创建了一个带有实际错误的新问题

因此,要获得我启用java控制台(由andrew建议)的确切错误,您可以go here了解如何执行此操作。

更新

几天后我就开始工作了,问题是jna.jar和platform.jar已经签名了,所以每当我用jarsigner签名时它都没用过,我从来没有得到任何错误(所以我从来没有看过它们),当我用jarsigner验证它时,我想通了。然后我取消签名并使用与我的jar一起使用的相同密钥签名(希望它会帮助其他人)

1 个答案:

答案 0 :(得分:1)

  

..还增加了政策..但即使这样也无济于事。

政策文件很少(如果有的话)是一个好主意。如果需要信任代码,请对存档进行数字签名。