小程序运行在Eclipse中,但不是在浏览器 - Java安全性 [英] Applet runs in eclipse but not in browser - java security

查看:908
本文介绍了小程序运行在Eclipse中,但不是在浏览器 - Java安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是小程序code,它使用jna.jar( https://github.com/twall/ JNA )来访问一个DLL文件在System32中。

 进口的javax.swing *。
javax.print中导入*。导入java.security *。
进口的java.util.ArrayList;
进口com.sun.jna.Library;
进口com.sun.jna.Native;
进口java.awt中的*。
java.awt.event中导入*。
进口的java.io.PrintWriter;
进口java.io.StringWriter中;进口com.sun.jna.Pointer;
进口com.sun.jna.platform.win32.WinDef.HWND;
进口com.sun.jna.platform.win32.WinUser;
进口com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
进口com.sun.jna.win32.StdCallLibrary;公共类CallApplet扩展JApplet的实现的ActionListener {    / **
     *
     * /
    私有静态最后的serialVersionUID长= 4654357272074276081L;
    静态的JTextField输出;
    私人最终字符串ButtonText =打印;    公共无效的init(){        集装箱contentHolder =的getContentPane();        contentHolder.setLayout(新的BorderLayout(18,18));        输出=新的JTextField(20);
        //添加(输出);        contentHolder.add(输出,BorderLayout.CENTER);        JPanel的buttonPanel =新JPanel();        JButton的B =的新的JButton(ButtonText);
        b.addActionListener(本);        buttonPanel.add(二);
        contentHolder.add(buttonPanel,BorderLayout.SOUTH);
        验证();
    }    公共无效的actionPerformed(ActionEvent的EVT)
    {
        //获取所按按钮的文字
        字符串命令= evt.getActionCommand();        output.setText(命令);        //如果是则myButton pressed,输出消息
        如果(ButtonText.equals(命令)){
            尝试{
                this.mprintt(「TSC TTP-244加);
            }赶上(例外五){            }
        }
    }    公共无效setMessage(字符串消息){
        output.setText(消息);
    }    公共接口TscLibDll扩展库{
        TscLibDll INSTANCE =(TscLibDll)Native.loadLibrary(TSCLIB,TscLibDll.class);
        诠释约();
        INT openport(字符串pirnterName);
        INT closeport();
        INT sendcommand(字符串printerCommand);
        INT设置(字符串宽度,高度字符串,字符串的速度,密度字符串,字符串传感器,垂直字符串,字符串偏移);
        INT downloadpcx(字符串的文件名,字符串IMAGE_NAME);
        INT酒吧code(字符串x,y字符串,字符串类型,字符串高度可读的字符串,字符串旋转,字符串窄,宽字符串,字符串code);
        INT printerfont(字符串x,y字符串,字符串fonttype,旋转字符串,字符串XMUL,字符串ymul,字符串文本);
        INT clearbuffer();
        INT printlabel(字符串集,字符串复印件);
        换页的int();
        INT nobackfeed();
        INT windowsfont(INT X,INT Y,INT fontheight,诠释旋转,INT fontstyle的,诠释fontunderline,字符串szFaceName,字符串的内容);
    }    公共无效mprintt(字符串打印机){        尝试{
            //TscLibDll.INSTANCE.about();
            TscLibDll.INSTANCE.openport(打印机);
            //TscLibDll.INSTANCE.downloadpcx(\"C:\\\\UL.PCX,UL.PCX);
            TscLibDll.INSTANCE.sendcommand(REM *****这是一个测试通过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,第否);
            TscLibDll.INSTANCE.bar code(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(印刷);
        }赶上(例外五){
            StringWriter的SW =新的StringWriter();
            PrintWriter的PW =的新PrintWriter(SW);
            e.printStackTrace(PW);
            output.setText(sw.toString());
        }
    }
}

我的HTML code

 < HTML和GT;
< HEAD>
<标题>< /标题>
< META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集= UTF-8>
< /头>
<身体GT;
<小程序codeBase的=。 code =CallApplet.class
        归档=CallApplet.jar,jna.jar,platform.jar
        HEIGHT =100WIDTH =100/>
< /身体GT;
< / HTML>

我可以看到装载的小应用程序和浏览器中的所有元素的摆动时,我尝试点击打印它只是不会做任何事情。在Eclipse中运行正常工作。

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

 拨款codeBase的HTTP://本地主机:8080 / appletproj / *{
    权限java.security.AllPermission;
};

堆栈跟踪

 异常螺纹AWT-EventQueue的-7java.lang.NoClassDefFoundError的:COM /阳光/ JNA /库
    在java.lang.ClassLoader.defineClass1(本机方法)
    在需要java.lang.ClassLoader.defineClass(来源不明)
    在java.security.SecureClassLoader.defineClass(来源不明)
    在java.net.URLClassLoader.defineClass(来源不明)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
    在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)
    在java.lang.reflect.Method.invoke(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.defineClassHelper(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.access $ 100(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader $ 2.run(来源不明)
    在java.security.AccessController.doPrivileged(本机方法)
    在sun.plugin2.applet.Plugin2ClassLoader.findClassHelper(来源不明)
    在sun.plugin2.applet.Applet2ClassLoader.findClass(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.loadClass0(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.loadClass(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.loadClass(来源不明)
    在java.lang.ClassLoader.loadClass(来源不明)
    在CallApplet.mprintt(CallApplet.java:185)
    在CallApplet.actionPerformed(CallApplet.java:60)
    在javax.swing.AbstractButton.fireActionPerformed(来源不明)
    在javax.swing.AbstractButton中的$ Handler.actionPerformed(来源不明)
    在javax.swing.DefaultButtonModel.fireActionPerformed(来源不明)
    在javax.swing.DefaultButtonModel.set pressed(来源不明)
    在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(来源不明)
    在java.awt.Component.processMouseEvent(来源不明)
    在javax.swing.JComponent.processMouseEvent(来源不明)
    在java.awt.Component.processEvent(来源不明)
    在java.awt.Container.processEvent(来源不明)
    在java.awt.Component.dispatchEventImpl(来源不明)
    在java.awt.Container.dispatchEventImpl(来源不明)
    在java.awt.Component.dispatchEvent(来源不明)
    在java.awt.LightweightDispatcher.retargetMouseEvent(来源不明)
    在java.awt.LightweightDispatcher.processMouseEvent(来源不明)
    在java.awt.LightweightDispatcher.dispatchEvent(来源不明)
    在java.awt.Container.dispatchEventImpl(来源不明)
    在java.awt.Component.dispatchEvent(来源不明)
    在java.awt.EventQueue.dispatchEventImpl(来源不明)
    在java.awt.EventQueue.access $ 200(来源不明)
    在java.awt.EventQueue中的$ 3.run(来源不明)
    在java.awt.EventQueue中的$ 3.run(来源不明)
    在java.security.AccessController.doPrivileged(本机方法)
    在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(​​来源不明)
    在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(​​来源不明)
    在java.awt.EventQueue中的$ 4.run(来源不明)
    在java.awt.EventQueue中的$ 4.run(来源不明)
    在java.security.AccessController.doPrivileged(本机方法)
    在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(​​来源不明)
    在java.awt.EventQueue.dispatchEvent(来源不明)
    在java.awt.EventDispatchThread.pumpOneEventForFilters(来源不明)
    在java.awt.EventDispatchThread.pumpEventsForFilter(来源不明)
    在java.awt.EventDispatchThread.pumpEventsForHierarchy(来源不明)
    在java.awt.EventDispatchThread.pumpEvents(来源不明)
    在java.awt.EventDispatchThread.pumpEvents(来源不明)
    在java.awt.EventDispatchThread.run(来源不明)
抛出java.lang.ClassNotFoundException:引起com.sun.jna.Library
    在sun.plugin2.applet.Applet2ClassLoader.findClass(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.loadClass0(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.loadClass(来源不明)
    在sun.plugin2.applet.Plugin2ClassLoader.loadClass(来源不明)
    在java.lang.ClassLoader.loadClass(来源不明)
    ... 55更多

但是,即使这没有帮助。请指导我我该怎么错在何处。

SOLUTION

坦率地说,我没有得到解决工作,而它不是那么创建具有实际的错误新的问题是安全问题。

因此​​,要得到确切的错误我启用Java控制台(由安德鲁建议的),你可以的去这里如何做到这一点。

更新

在几天我得到了它的工作,这个问题是jna.jar和已经签署所以每当我使用的jarsigner它从来没有工作签署他们,我从来没有得到任何错误platform.jar(所以我从来没有看过他们),当我使用的jarsigner验证它,我理解了它。然后我用我与我的罐子使用相同的密钥无符号和他们签署了(希望这将有助于其他)


解决方案

  

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


政策文件很少,如果有的话,是个好主意。如果code需要信任,数字签名存档(S)。

Below is applet code, it uses jna.jar (https://github.com/twall/jna) to access a DLL file in system32.

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

My Html code

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

I can see the applet loaded and all swing elements in browser when i try to click print it just wont do anything. In eclipse run it works fine.

I know this is security issue so i have also added policy (C:\Program Files\Java\jre7\lib\security\java.policy file)

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

Stacktrace

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

But even this does not help. please guide me where am I going wrong.

SOLUTION

Frankly i did not got solution to work, rather it was not a security issue so creating new question with actual error

So to get the exact error i enabled java console (suggested by andrew), you can go here for how to do it.

UPDATE

After few days I got it working, the issue was jna.jar and platform.jar which were already signed so whenever i signed them using jarsigner it never worked and i never got any error (so i never looked at them), when i verified it using jarsigner i figured it out. Then i unsigned them and signed it using same key which i used with my jar (Hope it will help others)

解决方案

..also added policy .. But even this does not help.

Policy files are rarely, if ever, a good idea. If the code needs to be trusted, digitally sign the archive(s).

这篇关于小程序运行在Eclipse中,但不是在浏览器 - Java安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆