最近要做一个登录时数字证书验证的功能,在用户登录时除了效验用户名密码,还需验证其数字证书。
相关资源:IBM developerWroks中国中的tomcat4中使用SSL,javaeye中的Acegi X.509双向认证
异同之处
与tomcat4中使用SSL中的异同:jdk1.4中已经包含JSSE。
与AcegiX.509双向认证中的异同:tomcat6配置文件多了SSLEnabled="true"属性。
实现方法
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
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
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
修改conf/server.xml,tomcat6中多了SSLEnabled="true"属性。keystorefile, truststorefile设置为你正确的相关路径
<CONNECTOR truststorepass="123456" truststoretype="JKS" truststorefile="d:/path/bin/x509/root.jks"
keystorepass="123456" keystoretype="PKCS12" keystorefile="d:/path/bin/x509/server.p12" clientauth="true"
sslprotocol="TLS" acceptcount="100" disableuploadtimeout="true" enablelookups="false" maxsparethreads="75"
minsparethreads="25" maxthreads="150" maxhttpheadersize="8192" sslenabled="true" port="8443" protocol="HTTP/1.1"
scheme="https" secure="true" />
root.p12导入至受信任的根证书颁发机构,client.p12导入至个人
<%
//获得certificate chain
X509Certificate[] ca = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
if (ca == null) {
out.println("No cert info!");
} else {
String serial = ca[0].getSerialNumber().toString();
String DN = ca[0].getSubjectDN().toString();
}
%>
目前有2条留言: