【转自】http://hi.baidu.com/kangqii/blog/item/f8495043f70c3a1572f05d4e.html

最近要做一个CRM,登录时数字证书要有验证的功能,在用户登录时除了效验用户名密码,还需验证其数字证书
相关资源:IBM developerWroks中国中的tomcat4中使用SSLjavaeye中的Acegi X.509双向认证
tomcat4中使用SSL中的异同:jdk1.4中已经包含JSSE
AcegiX.509双向认证中的异同:tomcat6配置文件多了SSLEnabled="true"属性
1.生成CA证书目前不使用第三方权威机构的CA来认证,自己充当CA的角色

1.创建私钥 :C:\OpenSSL\apps>openssl genrsa -out root/root-key.pem 1024
2.创建证书请求 :C:\OpenSSL\apps>openssl req -new -out root/root-req.csr -key root/root-key.pem
3.自签署证书 :C:\OpenSSL\apps>openssl x509 -req -in root/root-req.csr -out root/root-cert.pem -signkey
root/root-key.pem -days 3650
4.将证书导出成浏览器支持的.p12格式 :C:\OpenSSL\apps>openssl pkcs12 -export -clcerts -in root/root-cert.pem -inkey
root/root-key.pem -out root/root.p12

2.生成server证书
1.创建私钥 :C:\OpenSSL\apps>openssl genrsa -out server/server-key.pem 1024
2.创建证书请求 :C:\OpenSSL\apps>openssl req -new -out server/server-req.csr -key server/server-key.pem
3.自签署证书 :C:\OpenSSL\apps>openssl x509 -req -in server/server-req.csr -out server/server-cert.pem -signkey
server/server-key.pem -CA root/root-cert.pem -CAkey root/root-key.pem -CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :C:\OpenSSL\apps>openssl pkcs12 -export -clcerts -in server/server-cert.pem -inkey
server/server-key.pem -out server/server.p12

3.生成client证书
1.创建私钥 :C:\OpenSSL\apps>openssl genrsa -out client/client-key.pem 1024
2.创建证书请求 :C:\OpenSSL\apps>openssl req -new -out client/client-req.csr -key client/client-key.pem
3.自签署证书 :C:\OpenSSL\apps>openssl x509 -req -in client/client-req.csr -out client/client-cert.pem -signkey
client/client-key.pem -CA root/root-cert.pem -CAkey root/root-key.pem -CAcreateserial -days 3650
4.将证书导出成浏览器支持的.p12格式 :C:\OpenSSL\apps>openssl pkcs12 -export -clcerts -in client/client-cert.pem -inkey
client/client-key.pem -out client/client.p12

4.根据root证书生成jks文件
C:\OpenSSL\apps\root>keytool -import -v -trustcacerts -storepass password -alias root -file root-cert.pem
-keystore root.jks

5.配置tomcat ssl,修改conf/server.xmltomcat6中多了SSLEnabled="true"属性
keystorefile, truststorefile设置为你正确的相关路径

xml 代码

  1. <connector secure="true" scheme="https" protocol="HTTP/1.1" port="8443"
  2. sslenabled="true" maxhttpheadersize="8192" maxthreads="150"
  3. minsparethreads="25" maxsparethreads="75" enablelookups="false"
  4. disableuploadtimeout="true" acceptcount="100" sslprotocol="TLS"
  5. clientauth="true" keystorefile="d:/path/bin/x509/server.p12"
  6. keystoretype="PKCS12" keystorepass="123456" truststorefile="d:/path/bin/x509/root.jks"
  7. truststoretype="JKS" truststorepass="123456"/>

6.将root.p12,client.p12分别导入到IE中去(打开IE->;Internet选项->内容->证书)

root.p12导入至受信任的根证书颁发机构,client.p12导入至个人
7.访问你的应用http://ip:8443,如果配置正确的话会出现请求你数字证书的对话框
8.在jsp中取得符合x.509格式的证书

java 代码

  1. <%      
  2. //获得certificate chain   
  3.          X509Certificate[] ca=(X509Certificate[])request.getAttribute("javax.servlet.request.X509Certificate");     
  4. if(ca==null)     
  5.        {     
  6.          out.println("No cert info!");     
  7.        } else {     
  8.          String   serial=ca[0].getSerialNumber().toString();     
  9.          String DN=ca[0].getSubjectDN().toString();       
  10.        }     
  11.        %> 

文章来源:http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!792.entry