xp tomcat https服务器搭建

自建CA证书。利用openssl和keytool实现tomcat下自签名证书。

环境:Winxp + tomcat + jdk

1. 准备工作:
下载openssl
Tomcat
jdk 

2. 使用openssl生成根证书
(建立一张证书需要三步, 1是生成系统私钥, 2生成待签名证书, 3是生成x509证书, 用CA私钥进行自签名.)
1. 生成根证书的私钥,生成文件:cakey.pem
Openssl genrsa –out cakey.pem 1024
注解:genrsa: 生成CA私钥
-out: 生成的私钥的保存路径和名字
1024: 密钥位数
2. 根据私钥生成证书申请 生成文件careq.csr
Openssl req –new –out careq.csr –key cakey.pem –config openssl.cnf

G:\ca04>openssl req -new -out careq.csr -key cakey.pem -config openssl.cnf
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:guangdong
Locality Name (eg, city) []:shenzhen
Organization Name (eg, company) [Internet Widgits Pty Ltd]:royal
Organizational Unit Name (eg, section) []:PMS
Common Name (eg, YOUR name) []:localhost
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:royal

注解:
csr: certificate signing request
req: 生成待签名证书的选项
-key: 采用的CA私钥的路径, 这里使用的是3.1中产生的私钥
3. 利用根证书申请生成自签名根证书,生成cacert.pem
Openssl x509 –in careq.csr –out cacert.pem –signkey cakey.pem –days 3650

G:\ca04>openssl x509 -req -in careq.csr -out cacert.pem -signkey cakey.pem -days
3650
Loading 'screen' into random state - done
Signature ok
subject=/C=CN/ST=guangdong/L=shenzhen/O=royal/OU=PMS/CN=localhost/emailAddress=l
[email protected]
Getting Private key
表示自签名根证书成功。

注解:
x509: 生成x509的CA根证书
-in: 待签名证书的路径
-out: 生成的CA根证书的路径
-signkey: 采用签名的私钥的路径
-days: 有效时间(天)

3. 生成tomcat证书库文件和证书申请
1. 生成证书库文件 tomcatkey.jks
利用JDK自带的工具生成。
Keytool –genkey –alias tomcat_server –keyalg RSA –keysize 1024 –validity “1825” –keypass 123456 –keystore tomcatkey.jks –storepass 123456
解释:-alias:证书库文件中私钥的别名。
   -keypass证书库文件私钥的密钥。
    -keystore证书库文件存储路径
   -storepass证书文件的密钥。

G:\ca04>keytool -genkey  -alias tomcat_server -keyalg RSA -keysize 1024 -validit
y "1825" -keypass 123456 -keystore tomcatkey.jks -storepass 123456
您的名字与姓氏是什么?
  [Unknown]:  192.168.0.37
您的组织单位名称是什么?
  [Unknown]:  PMS
您的组织名称是什么?
  [Unknown]:  royal
您所在的城市或区域名称是什么?
  [Unknown]:  shenzhen
您所在的州或省份名称是什么?
  [Unknown]:  guangdong
该单位的两字母国家代码是什么
  [Unknown]:  CN
CN=192.168.0.37, OU=PMS, O=royal, L=shenzhen, ST=guangdong, C=CN 正确吗?
  [否]:  y

注:红色字体地方:名称和姓氏一定要填入服务器的域名或IP地址,否则部署之后会提示证书地址不匹配的错误。其它的写一致就可以了。
2. 根据证书库文件生成证书申请表 生成文件certreq.cer
Keytool –certreq –alias tomcat_server –signalg “MD5withRSA” –file certreq.cer –keypass 123456 –keystore tomcatkey.jks –storepass 123456

4. 制作服务器证书并导入证书库
1. 用根证书签名证书申请生成根证书 生成文件 tomcatcert.pem
Openssl ca –keyfile cakey.pem –cert cacert.pem –in certreq.cer –out tomcatcert.pem –config openssl.cnf –polisy policy_anything
提示中输入 Y接受就可以了,
2. 由于原有的格式都是pem格式的,在导入证书库之前(即导入jks之件)需要转换成cer格式,生成文件cacert.cer,tomcatcert.cer
Openssl x509 –in cacert.pem –out cacert.cer
Openssl x509 –in tomcatcert.pem –out tomcatcert.cer
3. 将转换成功的cer文件导入证书库中。
首先导入根证书。
Keytool –keystore tomcatkey.jks –import –alias RootCA –file cacert.cer
Keytool –keystore tomcatkey.jks –import –alias tomcat_server –file tomcatcert.cer

5. Tomcat配置
Server.xml修改
增加:
    <Connector protocol="HTTP/1.1"
port="443" minSpareThreads="5" maxSpareThreads="75"
enableLookups="true" disableUploadTimeout="true"
acceptCount="100"  maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="conf\ca04\tomcatkey.jks" keystorePass="123456"/>

去掉:
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

增加双向访问
在web.xml里增加:
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint> 
<!--Authorization setting for SSL --> 
<web-resource-collection> 
<web-resource-name>SSL</web-resource-name> 
<url-pattern>/*</url-pattern> 
</web-resource-collection> 
<user-data-constraint> 
<transport-guarantee>CONFIDENTIAL</transport-guarantee> 
</user-data-constraint> 
</security-constraint>

  <url-patten>表明对所有的请求都采用SSL协议,写上这段代码后,tomcat可以自动实现http到https的转换 https://192.168.0.37/YXTSystem/

IE浏览器配置
第一次打开的时候会提示证书错误,这时只要点击查看证书,点击安装证书,下一步:选择受信任的根证书颁发机构,再下一步完成。刷新就不会有提示了。
如果没有提示安装证书,进行证书安装,在证书――〉受信任的根证书颁发机会――〉导入,将cacert.cer 和tomcatcert.cer ,就成功了。

总结:
服务器文件或是生成tomcat证文件,中的名字和姓氏或common name(eg.yourname)一定要与服务器的的访问地址一致,不然会导致失败。
将根证书和客户端证书导致到浏览器中,要转换成p12格式的。
命令是:openssl pkcs12 -export -clcerts -in client/client-cert.pem -inkey client/client-key.pem -out client/client.p12

Tomcat里面配置server产生的server.p12
Trutsstore.jks 是由根证书签名成功的ca来生成的
命令:keytool -keystore truststore/truststore.jks -keypass keypass-storepass storepass-alias my_ca -import -trustcacerts -file ca\ca-cert.pem(根证书的成功签名)

浏览器要导入的证书有:
受信任的根证书颁发机构:根证书的ca.p12,cacert.cer(根证书申请)和tomcatcert.cer(服务器证书)

个人:导入client.p12 客户端的

猜你喜欢

转载自xdy2008.iteye.com/blog/1714670