生活随笔
收集整理的這篇文章主要介紹了
EJBCA使用之注册用户及创建证书
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?http://blog.csdn.net/xanxus46/article/details/9103031
分類:?ejbca2013-06-15 21:39?
199人閱讀??
收藏?
舉報
JBossebjca
研究ejbca源碼快一個月了,從openipmp中的老版ejbca到最新的4.0.15,感覺看別人代碼實在太痛苦了,而且之前自己對ejb并不是很熟悉,還必須自己重新學了一點基本的ejb知識。
我最開始是研究openipmp的,里面自帶就有ejbca的jar包,所以一開始我看openipmp怎么調(diào)用ejbca就行,但是由于openipmp實在太老了,它使用的ejbca是遵守ejb2.1標準的,調(diào)用起來實在太復雜太麻煩了,所以經(jīng)過一周后我徹底放棄它,重新看最新版的ejbca。
最新版的ejbca可以從官網(wǎng)下,官網(wǎng)下的包括java源碼、jsp和一堆云里霧里的文檔。但注意官網(wǎng)下的java源碼并不是一個完整eclipse工程,我們需要修改源碼的話,還是用svn檢出eclipse工程的源碼比較好。svn檢出地址可以從http://ejbca.org/repository.html找到,是https://svn.cesecore.eu/svn/ejbca/branches/Branch_4_0/ejbca
檢出完ejbca后,我們可以參考維基上關(guān)于ejbca的api?http://wiki.ejbca.org/developers#toc6,我們首先導入工程,然后會發(fā)現(xiàn)有編譯錯誤,因為工程使用了JBOSS5_HOME類變量,我們需要添加該變量,步驟是Window->Preferences->Java->Build Path->Classpath,指向jboss的根目錄。設(shè)置完這個后還是不行,我們要使用ant命令編譯得到一些必需的類,就在ejbca的根目錄下使用ant,或者使用eclipse集成的ant。(注意,千萬不要在windows X64上使用ejbca,因為windows X64的jdk缺少一個庫,好像是sun的一個關(guān)于security的庫,而ejbca必需這個庫)。
做完以上步驟后,應(yīng)該就沒有問題了,我們現(xiàn)在就需要做最痛苦的事情——讀代碼。還好,ejbca的注釋還是比較詳盡的,而且都是javadoc注釋,在eclipse查看很方便。好了,廢話不多說,馬上進入正題,使用ejbca注冊新用戶并且為用戶導出證書。
注冊新用戶:ejbca使用方式有兩種:web和命令行。web很簡單,網(wǎng)上很多教程,這里主要介紹命令行方式。我們可以找到modules/ejbca-ejb-cli/src這個包里面都是使用命令行方式操作ejbca,而注冊新用戶是屬于ra的操作,我們需要看org.ejbca.ui.cli.ra.RaAddUserCommand類,它的execute方法就是添加一個用戶,那我們只需要調(diào)用該方法就添加用戶,實在不明白這么好用的方法,ejbca的官方api為什么不給出,但是這個方法是不推薦的,因為注意到execute方法的參數(shù)是一個string數(shù)組,這就意味著我們可以出錯的概率大大增加,他實際上用的是UserAdminSession的addUser方法,這個方法有很多重載方法,其中官方推薦的是使用一個userDataVo的對象作為注冊參數(shù),的確這比較好。然后下面是我寫的一個junit測試用例(PS:舔一下Junit的菊花,junit確實是一個好東東)。
[java]?view plaincopy
public?class?AddUser?{?? ?? ????RaAddUserCommand?raCommand?=?null;?? ?? ????@Before?? ????public?void?setUp()?throws?Exception?{?? ????????raCommand?=?new?RaAddUserCommand();?? ????}?? ?? ????@Test?? ????public?void?test()?{?? ????????String[]?args?=?{?"",?"xanxus1",?"12345",?? ????????????????"CN=xanxus",?"NULL",?"AdminCA1",?? ????????????????"xanxusiou@163.com",?"2",?"P12"?};?? ????????try?{?? ????????????raCommand.execute(args);?? ????????}?catch?(ErrorAdminCommandException?e)?{?? ?????????????? ????????????e.printStackTrace();?? ????????}?? ????}?? ?? }??
這里解釋一下參數(shù)的含義,第一個是沒意義的,所以為空字符串,接著是用戶名,密碼,dn(這個需要注意,必須要有cn,而且cn必須是唯一的),subjectAltName(這個沒什么用,就NULL吧),ca名字,用戶郵箱,用戶類型(2是終端用戶),最后的是證書類型(這里是pkcs12)。
為用戶導出證書:這里就不能簡單的使用junit測試了,因為我們需要使用ejb遠程調(diào)用,所以我們需要創(chuàng)建一個web工程,放在jboss環(huán)境下。然后我們創(chuàng)建一個servlet,在dopost里面導出證書吧(注意,必須是post里),代碼如下:
[java]?view plaincopy
protected?void?doPost(HttpServletRequest?request,?? ????????HttpServletResponse?response)?throws?ServletException,?IOException?{?? ?????? ????response.setContentType("text/html;charset=UTF-8");?? ?? ????String?name?=?request.getParameter("name");?? ????String?password?=?request.getParameter("password");?? ?? ????try?{?? ????????Context?jndiContext?=?new?InitialContext();?? ????????UserAdminSessionLocal?userAdminSession?=?(UserAdminSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/UserAdminSessionBean/local");?? ????????AuthenticationSessionLocal?authenticationSession?=?(AuthenticationSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/AuthenticationSessionBean/local");?? ????????CAAdminSessionLocal?caAdminSession?=?(CAAdminSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/CAAdminSessionBean/local");?? ????????KeyRecoverySessionLocal?keyRecoverySession?=?(KeyRecoverySessionLocal)?jndiContext?? ????????????????.lookup("ejbca/KeyRecoverySessionBean/local");?? ????????SignSessionLocal?signSession?=?(SignSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/RSASignSessionBean/local");?? ????????EndEntityProfileSessionLocal?endEntityProfileSession?=?(EndEntityProfileSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/EndEntityProfileSessionBean/local");?? ????????Admin?admin?=?new?Admin(Admin.TYPE_PUBLIC_WEB_USER,?? ????????????????request.getRemoteAddr());?? ????????UserDataVO?user?=?userAdminSession.findUser(new?Admin(?? ????????????????Admin.TYPE_PUBLIC_WEB_USER),?name);?? ????????GlobalConfigurationSessionLocal?globalConfigurationSession?=?(GlobalConfigurationSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/GlobalConfigurationSessionBean/local");?? ????????RaAdminSessionLocal?raAdminSession?=?(RaAdminSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/RaAdminSessionBean/local");?? ????????CertificateProfileSessionLocal?certificateProfileSession?=?(CertificateProfileSessionLocal)?jndiContext?? ????????????????.lookup("ejbca/CertificateProfileSessionBean/local");?? ?????????? ????????boolean?usekeyrecovery?=?false;?? ????????usekeyrecovery?=?globalConfigurationSession?? ????????????????.getCachedGlobalConfiguration(admin).getEnableKeyRecovery();?? ?????????? ????????boolean?savekeys?=?user.getKeyRecoverable()?? ????????????????&&?usekeyrecovery?? ????????????????&&?(user.getStatus()?!=?UserDataConstants.STATUS_KEYRECOVERY);?? ????????boolean?loadkeys?=?(user.getStatus()?==?UserDataConstants.STATUS_KEYRECOVERY)?? ????????????????&&?usekeyrecovery;?? ?? ????????int?endEntityProfileId?=?user.getEndEntityProfileId();?? ????????int?certificateProfileId?=?user.getCertificateProfileId();?? ????????EndEntityProfile?endEntityProfile?=?endEntityProfileSession?? ????????????????.getEndEntityProfile(admin,?endEntityProfileId);?? ????????boolean?reusecertificate?=?endEntityProfile?? ????????????????.getReUseKeyRecoveredCertificate();?? ????????GenerateToken?token?=?new?GenerateToken(authenticationSession,?? ????????????????userAdminSession,?caAdminSession,?keyRecoverySession,?? ????????????????signSession);?? ????????KeyStore?keyStore?=?token.generateOrKeyRecoverToken(admin,?name,?? ????????????????password,?user.getCAId(),?"1024",?? ????????????????AlgorithmConstants.KEYALGORITHM_RSA,?false,?loadkeys,?? ????????????????savekeys,?reusecertificate,?endEntityProfileId);?? ????????System.out.println("size:"?+?keyStore.size());?? ????????sendP12Token(keyStore,?name,?password,?response);?? ????}?catch?(Exception?exception)?{?? ????????exception.printStackTrace();?? ????}?? ?? }?? ?? private?void?sendP12Token(KeyStore?ks,?String?username,?String?kspassword,?? ????????HttpServletResponse?out)?throws?Exception?{?? ????ByteArrayOutputStream?buffer?=?new?ByteArrayOutputStream();?? ????ks.store(buffer,?kspassword.toCharArray());?? ?? ????out.setContentType("application/x-pkcs12");?? ????out.setHeader("Content-disposition",?"filename="?+?username?+?".p12");?? ????out.setContentLength(buffer.size());?? ????buffer.writeTo(out.getOutputStream());?? ????out.flushBuffer();?? ????buffer.close();?? }?? 首先,導出用戶的前提是你必須已經(jīng)在jboss里部署好ejbca,然后第一步是使用jndi找出遠程對象,使用lookup方法,參數(shù)可以從jboss的jndiview里找出,注意這里都是local對象,接著就是使用GenerateToken對象生成keystore,有一個false參數(shù)代表生成的是p12證書,還能指定密碼的長度和算法,最后就是使用response輸出證書。
以上就是目前為止我看ejbca的成果,以后會繼續(xù)更新,ejbca資料實在太少,希望大家一起努力,共同研究。
總結(jié)
以上是生活随笔為你收集整理的EJBCA使用之注册用户及创建证书的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。