当前位置:   article > 正文

EJBCA 6 通过调用WebService接口增加用户并获取证书

ejbca ws

今天终于搞定通过调用WebService 接口的方式在EJBCA 中增加用户。

本项目完整代码请参见http://git.oschina.net/xiangyunsoft/EjbcaWs

1、EJBCA6 默认会配置好ws服务,如果有其他配置需要在conf/jaxws.properties文件中进行配置。

2、编写客户端代码,调用ws接口服务

  1. package cn.com.rexen.ca;
  2. import org.cesecore.util.CryptoProviderTools;
  3. import org.cesecore.util.provider.TLSProvider;
  4. import org.ejbca.core.protocol.ws.client.gen.*;
  5. import javax.net.ssl.KeyManagerFactory;
  6. import javax.xml.namespace.QName;
  7. import java.io.IOException;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.security.*;
  11. import java.security.cert.CertificateException;
  12. import java.util.List;
  13. /**
  14.  * 调用EJBCA WS接口.
  15.  * Created by libo on 2014/6/16.
  16.  */
  17. public class CaWS {
  18.     /** 解决 java.security.cert.CertificateException: No subject alternative names matching IP address 172.17.2.248 found
  19.     172.17.2.248 换成自己的IP或机器名。
  20.      */
  21.     static {
  22.         javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
  23.                 new javax.net.ssl.HostnameVerifier() {
  24.                     public boolean verify(String hostname,
  25.                                           javax.net.ssl.SSLSession sslSession) {
  26.                         if (hostname.equals("172.17.2.248")) {
  27.                             return true;
  28.                         }
  29.                         return false;
  30.                     }
  31.                 }
  32.         );
  33.     }
  34.     private EjbcaWS ejbcaWS;
  35.     public static void main(String[] args) throws Exception {
  36.         CaWS caWS = new CaWS();
  37.         caWS.initEjbcaWs();
  38.         caWS.create();
  39.         caWS.findUser();
  40.     }
  41.     /**
  42.      * 查询用户信息.
  43.      */
  44.     public void findUser() throws MalformedURLException, EjbcaException_Exception, IllegalQueryException_Exception, EndEntityProfileNotFoundException_Exception, AuthorizationDeniedException_Exception, ApprovalException_Exception, UserDoesntFullfillEndEntityProfile_Exception, CADoesntExistsException_Exception, WaitingForApprovalException_Exception {
  45.         UserMatch usermatch = new UserMatch();
  46.         usermatch.setMatchwith(UserMatch.MATCH_WITH_EMAIL); //按EMAIL地址进行查询
  47.         usermatch.setMatchtype(UserMatch.MATCH_TYPE_EQUALS);    //查询匹配方式
  48.         usermatch.setMatchvalue("123@qq.com");
  49.         List<UserDataVOWS> result = ejbcaWS.findUser(usermatch);
  50.         System.out.println("result:" + result);
  51.         for (UserDataVOWS ud : result) {
  52.             System.out.println("==========================");
  53.             System.out.println("userName:" + ud.getUsername());
  54.             System.out.println("email:" + ud.getEmail());
  55.             System.out.println("SubjectDN:" + ud.getSubjectDN());
  56.             System.out.println("caName:" + ud.getCaName());
  57.             System.out.println("==========================");
  58.         }
  59.     }
  60.     /**
  61.      * 初始化ws 接口服务.
  62.      */
  63.     public void initEjbcaWs() {
  64.         CryptoProviderTools.installBCProvider();
  65.         String urlstr = "https://172.17.2.248:8443/ejbca/ejbcaws/ejbcaws?wsdl";
  66.         String fileName = "F:\\workspace\\caWS\\src\\superadmin_62.p12";
  67.         String password = "ejbca";
  68.         System.setProperty("javax.net.ssl.keyStore", fileName);
  69.         System.setProperty("javax.net.ssl.keyStoreType""pkcs12");
  70.         Provider tlsProvider = new TLSProvider();
  71.         Security.addProvider(tlsProvider);
  72.         Security.setProperty("ssl.TrustManagerFactory.algorithm""AcceptAll");
  73.         System.setProperty("javax.net.ssl.keyStorePassword", password);
  74.         try {
  75.             KeyManagerFactory.getInstance("NewSunX509");
  76.         } catch (NoSuchAlgorithmException e) {
  77.             e.printStackTrace();
  78.         }
  79.         Security.setProperty("ssl.KeyManagerFactory.algorithm""NewSunX509");
  80.         QName qname = new QName("http://ws.protocol.core.ejbca.org/""EjbcaWSService");
  81.         URL url = null;
  82.         try {
  83.             url = new URL(null, urlstr, new sun.net.www.protocol.http.Handler());
  84.         } catch (MalformedURLException e) {
  85.             e.printStackTrace();
  86.         }
  87.         EjbcaWSService service = new EjbcaWSService(url, qname);
  88.         ejbcaWS = service.getEjbcaWSPort();
  89.         String version = ejbcaWS.getEjbcaVersion();
  90.         System.out.println("ejbcaWS init successfully. EJBCA Version is :" + version);
  91.     }
  92.     /**
  93.      * 增加用户
  94.      */
  95.     public void create() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, IOException, WaitingForApprovalException_Exception, NotFoundException_Exception, AuthorizationDeniedException_Exception, ApprovalException_Exception, UserDoesntFullfillEndEntityProfile_Exception, CADoesntExistsException_Exception, EjbcaException_Exception, InvalidAlgorithmParameterException {
  96.         String password = "123456";
  97.         final UserDataVOWS userData = new UserDataVOWS();
  98.         userData.setUsername("t_123");
  99.         userData.setPassword(password); //如果模板指定自动生成密码,则不需要指定。
  100.         userData.setClearPwd(false);
  101.         userData.setSubjectDN("E=123@qq.com,UID=35,CN=t_123,OU=研发中心,O=qq.com,L=changchu,ST=jilin,C=china");
  102.         userData.setCaName("ManagementCA");
  103.         userData.setEmail("123@qq.com");
  104.         userData.setSubjectAltName(null);
  105.         userData.setStatus(UserDataVOWS.STATUS_NEW);
  106.         userData.setTokenType(UserDataVOWS.TOKEN_TYPE_P12);
  107.         userData.setEndEntityProfileName("EMPTY");
  108.         userData.setCertificateProfileName("ENDUSER");
  109. //        userData.setSendNotification(true);       //如果配置邮件发送,则可以设置增加用户时发送信息。
  110.         ejbcaWS.editUser(userData);
  111.         
  112.         writeFile(userData, ejbcaWS);
  113.         System.out.println("create user successfully.");
  114.     }
  115.     
  116.         /**
  117.      * 生成证书
  118.      */
  119.     public void writeFile(UserDataVOWS user1, EjbcaWS ws) throws InvalidAlgorithmParameterException, CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, UserDoesntFullfillEndEntityProfile_Exception, AuthorizationDeniedException_Exception, ApprovalException_Exception, WaitingForApprovalException_Exception, NotFoundException_Exception, EjbcaException_Exception, InvalidKeyException, NoSuchProviderException, SignatureException, CADoesntExistsException_Exception {
  120.         // For now, assume RSA and SHA1WithRSA.
  121.         String strKeySpec = "1024";
  122.         KeyPair keys = KeyTools.genKeys(strKeySpec,
  123.                 AlgorithmConstants.KEYALGORITHM_RSA);
  124.         PKCS10CertificationRequest pkcs10 = new PKCS10CertificationRequest("SHA256withRSA"new X500Principal(
  125.                 user1.getSubjectDN()), keys.getPublic(), null, keys.getPrivate());
  126.         CertificateResponse certenv = ws.certificateRequest(user1,
  127.                 new String(Base64.encode(pkcs10.getEncoded())),
  128.                 CertificateHelper.CERT_REQ_TYPE_PKCS10, null,
  129.                 CertificateHelper.RESPONSETYPE_CERTIFICATE);
  130. //
  131.         X509Certificate cert = certenv.getCertificate();
  132.         java.security.KeyStore jks = java.security.KeyStore
  133.                 .getInstance(user1.getTokenType().equals("JKS") ? "JKS"
  134.                         : "pkcs12");
  135.         jks.load(null, user1.getPassword().toCharArray());
  136.         java.security.cert.CertificateFactory cf = java.security.cert.CertificateFactory
  137.                 .getInstance("X.509");
  138.         java.security.cert.Certificate cert1 = cf
  139.                 .generateCertificate(new ByteArrayInputStream(cert
  140.                         .getEncoded()));
  141.         java.security.cert.Certificate[] certs = new java.security.cert.Certificate[1];
  142.         certs[0] = cert1;
  143.         // Following logic used in EjbcaWS.java, the alias is the common
  144.         // name, if present, and otherwise, is the username.
  145.         String alias = CertTools.getPartFromDN(user1.getSubjectDN(),
  146.                 "CN");
  147.         if (alias == null) {
  148.             alias = user1.getUsername();
  149.         }
  150.         String strFileName = "c:\\temp\\test.p12";
  151.         FileOutputStream out = new FileOutputStream(strFileName);
  152.         // storing keystore
  153.         java.security.PrivateKey ff = keys.getPrivate();
  154.         jks.setKeyEntry(alias, ff, user1.getPassword().toCharArray(),
  155.                 certs);
  156.         jks.store(out, user1.getPassword().toCharArray());
  157.         out.close();
  158.     }
  159. }

执行程序运行结果如下:

  1. ejbcaWS init successfully. EJBCA Version is :EJBCA 6.2.0 (r19221)
  2. create user successfully.
  3. result:[org.ejbca.core.protocol.ws.client.gen.UserDataVOWS@44c35c97]
  4. ==========================
  5. userName:t_123
  6. email:123@qq.com
  7. SubjectDN:E=123@qq.com,UID=35,CN=t_123,OU=研发中心,O=qq.com,L=changchu,ST=jilin,C=china
  8. caName:ManagementCA
  9. ==========================

工程所需要jar在ejcb_home/dist/ejbca-ws-cli/lib目录下。



转载于:https://my.oschina.net/thinker4self/blog/286979

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/zyabc/article/detail/62482
推荐阅读
相关标签
  

闽ICP备14008679号