2015年7月13日 星期一

[JAVA]透過HiSecure Java api認證IC卡(自然人憑證工商憑證)之Applet


文章出處:文武人尹

透過HiSecure Java api認證IC卡(自然人憑證工商憑證)之Applet

1. Hisecure API 線上申請系統註冊(目前提供工商憑證線上註冊),審核時間約三到五天!或直接上網找P11JNI.jar(放在java classpath下如JRE_HOME/lib/ext底下)及P11JNI.dll(放在JRE_HOME/lib/i386以及SystemRoot/system32/底下),不過當然是自己申請比較安全點!

2. client端安裝HiCOS

3. NewJApplet1.java:


package hisecure;

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import tw.com.chttl.Module;
import tw.com.chttl.Token;

public class NewJApplet1 extends javax.swing.JApplet {
    JLabel label;
    JTextField pinField;
    JButton btn;
    /** Initializes the applet NewJApplet1 */
    public void init() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2, 2, 2, 2));
        label = new JLabel("請輸入憑證PIN碼");
        pinField = new JTextField("        ");
        p.add(label);
        p.add(pinField);
        btn = new JButton("登入");
        btn.addActionListener(new B1());
        p.add(btn);
        Container con = getContentPane();
        con.setLayout(new GridBagLayout()); // Used to center the panel
        con.add(p);
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                      


   class B1 implements ActionListener,PrivilegedAction<Object> {
        private String checkLogin(){
            String msg = "none";
            try {
                Module.initialize();
                Module module = Module.getInstance();
                int[] tokIDs = module.getTokens();
                if (tokIDs.length != 0) {
                    msg = "Ready!";
                    String pin = "";
                    if(pinField!=null){
                        pin =pinField.getText();
                    }
                    Token tok=module.getToken(tokIDs[0]);
                    boolean login=tok.login(pin);
                    if(label!=null){
                        label.setText("認證成功:" + login);
                    }
                } else {
                    msg = "Confirm the reader is ready yet!";
                }
            } catch (Exception e) {
                msg = e.getMessage();
            } finally {
                return msg;
            }
       }
    public void actionPerformed(ActionEvent e) {
        checkLogin();
    }
         public Object run() {
            return  "checkLogin";
        }
  }
}

4. index.html:

< applet code="hisecure.NewJApplet1.class" archive="HiSecure.jar,P11JNI.jar" width="760" height="350" >
< /applet >

5. 含applet class的jar檔放在與index.html同一目錄

p.s. 由於HiSecure Java api使用了JNI呼叫底層程式,而在applet中JNI屬特權模式,因此需要將jar簽章後同時獲得客戶端的同意才得以在瀏覽器中執行(a.使用keytool -genkey -alias test -keysize 2048 -keyalg RSA -sigalg SHA1withRSA -keypass test1234 -storepass test1234 -validity 365 -dname "CN=testname, OU=testunit, O=testorg, L=testlocation,S=teststate , C=TW" -keystore test.jks建立簽章 b.jarsigner HiSecure.jar test將jar檔簽章)

沒有留言:

張貼留言