读书人

Tomcat配备使用SSL双向认证(使用open

发布时间: 2012-07-15 20:20:06 作者: rapoo

Tomcat配置使用SSL双向认证(使用openssl生成证书)

?

双向认证:

?

    客户端向服务器发送消息,首先把消息用客户端证书加密然后连同时把客户端证书一起发送到服务器端,服务器接到消息后用首先用客户端证书把消息解密,然后用服务器私钥把消息加密,把服务器证书和消息一起发送到客户端,客户端用发来的服务器证书对消息进行解密,然后用服务器的证书对消息加密,然后在用客户端的证书对消息在进行一次加密,连同加密消息和客户端证书一起发送到服务器端,到服务器端首先用客户端传来的证书对消息进行解密,确保消息是这个客户发来的,然后用服务器端的私钥对消息在进行解密这个便得到了明文数据。

?

单向认证:

?

    客户端向服务器发送消息,服务器接到消息后,用服务器端的密钥库中的私钥对数据进行加密,然后把加密后的数据和服务器端的公钥一起发送到客户端,客户端用服务器发送来的公钥对数据解密,然后在用传到客户端的服务器公钥对数据加密传给服务器端,服务器用私钥对数据进行解密,

这就完成了客户端和服务器之间通信的安全问题,但是单向认证没有验证客户端的合法性。

?

?

==========================

openssl在windows上的安装

?

?

从此处下载openssl for windows

http://gnuwin32.sourceforge.net/packages/openssl.htm

解压,并设置PATH环境变量指向其bin文件夹

下载openssl的配置文件http://www.securityfocus.com/data/tools/openssl.conf

并将其拷到一个文件夹下,以便用命令行指定,这里是c:/ssl/下

否则运行时会报Unable to load config info from /usr/local/ssl/openssl.cnf错误

=============================

?

?

以下安装配置环境为linux,tomcat-5.5.30

?

?

一、建立目录

?

?

?

?

tomcat6.0的配置:

?

?

tomcat7.0的配置:

jsse模式

?

?APR模式

?

?

?

?

?

七、测试(linux下)

openssl s_client -connect localhost:8443 -cert /home/ssl/client/client-cert.pem -key /home/ssl/client/client-key.pem -tls1 -CAfile /home/ssl/ca/ca-cert.pem -state -showcerts

?

GET /index.jsp HTTP/1.0

?

?

八、导入证书

服务端导入server.P12 和ca.p12证书

客户端导入将ca.p12,client.p12证书

IE中(打开IE->;Internet选项->内容->证书)

?

ca.p12导入至受信任的根证书颁发机构,client.p12导入至个人

Firefox中(工具-选项-高级-加密-查看证书-您的证书)

将ca.p12和client.p12均导入这里

?

注意:ca,server,client的证书的common name(ca=ca,server=localhost,client=dong)一定不能重复,否则ssl不成功

?

?

?

九、tomcat应用程序使用浏览器证书认证

?

在server/webapps/manager/WEB-INF/web.xml中,将BASIC认证改为证书认证

?

?

访问http://localhost:8443即可验证ssl是否成功

访问http://localhost:8443/manager/html可验证应用程序利用client证书验证是否成功

?

?

?

?

?

Used keytool to self-author a server certificate for DEMO

?

Microsoft Windows XP [Version 5.1.2600](C) Copyright 1985-2001 Microsoft Corp.C:\Documents and Settings\ukari>cd \program*The filename, directory name, or volume label syntax is incorrect.C:\Program Files>cd javaC:\Program Files\Java>cd jdk*C:\Program Files\Java\jdk1.5.0_11>cd binC:\Program Files\Java\jdk1.5.0_11\bin>keytool -genkey -alias tomcat -keypass changeit -keyalg RSAEnter keystore password:  changeitWhat is your first and last name?  [Unknown]:  compAWhat is the name of your organizational unit?  [Unknown]:  Information SystemsWhat is the name of your organization?  [Unknown]:  Pacific Disaster CenterWhat is the name of your City or Locality?  [Unknown]:  KiheiWhat is the name of your State or Province?  [Unknown]:  HIWhat is the two-letter country code for this unit?  [Unknown]:  USIs CN=localhost, OU=Information Systems, O=Pacific Disaster Center, L=Kihei, ST=HI, C=US correct?  [no]:  yesC:\Program Files\Java\jdk1.5.0_11\bin>keytool -export -alias tomcat -keypass changeit -file server.crtEnter keystore password: changeitCertificate stored in file <server.crt>C:\Program Files\Java\jdk1.5.0_11\bin>keytool -import -file server.crt -keypass changeit -keystore ..\jre\lib\security\cacertsEnter keystore password: changeitOwner: CN=localhost, OU=Information Systems, O=Pacific Disaster Center, L=Kihei, ST=HI, C=USIssuer: CN=localhost, OU=Information Systems, O=Pacific Disaster Center, L=Kihei, ST=HI, C=USSerial number: 462030d8Valid from: Fri Apr 13 15:39:36 HST 2007 until: Thu Jul 12 15:39:36 HST 2007Certificate fingerprints:MD5: CC:3B:FB:FB:AE:12:AD:FB:3E:D 5:98:CB:2E:3B:0A:ADSHA1: A1:16:80:68:39:C7:58:EA:2F:48:59:AA:1D:73:5F:56:78:CE:A4:CETrust this certificate? [no]: yesCertificate was added to keystoreC:\Program Files\Java\jdk1.5.0_11\bin>

?

?

?

如果 下面这行出现错误:

keytool -import -file server.crt -keypass changeit -keystore ..\jre\lib\security\cacerts?

那么查看是否已存在 “..\jre\lib\security\cacerts” 这个文件,存在的话,备份删除,重试就OK了

?

读书人网 >Web前端

热点推荐