服务端:
tomcat.keystore 【服务端证书库,记录受信任的客户端的证书】
tomcat.private.key 【服务端证书库的 私钥,用openssl解密后,用在nginx中】
tomcat.private.key.nokey 【服务端证书库的 私钥(已解密),用在nginx中】
tomcat.cer【服务端证书,需要导入到 客户端的证书库中受信任】
tomcat.crt【服务端证书,我用于nginx中】
客户端:
client.keystore【客户端证书库,需要将服务端证书tomcat.cer导入】
client.keystore.p12【客户端证书库 PKCS12格式】
1、生成证书库
服务端证书库和客户端证书库都是证书库,没什么不一样。
下面是生成tomcat.keystore(CN 对应的一般就是IP)
keytool -genkey -keystore tomcat.keystore -alias tomcat -keyalg RSA -validity 36500 -dname "CN=1.1.1.1, OU=t, O=t, L=t, ST=t, C=t" -keypass password -storepass password
下面是生成client.keystore(CN 对应的一般就是IP)
keytool -genkey -keystore client.keystore -alias client -keyalg RSA -validity 36500 -dname "CN=0.0.0.0, OU=t, O=t, L=t, ST=t, C=t" -keypass password -storepass password
下面生成client.keystore.p12
keytool -genkey -alias client -keypass password -keyalg RSA -keysize 1024 -validity 36500 -dname "CN=0.0.0.0, OU=t, O=t, L=t, ST=t, C=t" -storetype PKCS12 -keystore client.keystore.p12 -storepass password
2、服务端证书库导出私钥(用于nginx中)
java程序跑一跑【PS 后续我会放在github上,当下有不懂的可以发邮件问我拿 t953170224@163.com】
https://github.com/zealotTL/test/tree/master/ssl
3、服务端证书私钥解密
我是利用linux上的openssl
openssl rsa -in tomcat.private.key -out tomcat.private.key.nokey
4、服务端证书导出
导出crt格式
keytool -export -alias tomcat -rfc -file tomcat.crt -keystore tomcat.keystore
导出cer格式
keytool -export -alias tomcat -rfc -file tomcat.cer -keystore tomcat.keystore
PS:两种命令之后导出文件后缀不一样,不知道两者有什么区别
5、将服务端证书cer导入到客户端证书库中
keytool -import -alias tomcat -keystore client.keystore -file tomcat.cer -trustcacerts -storepass password
服务端配置:
nginx配置
nginx配置证书后,会将https转发成http请求给tomcat,所以tomcat就不需要配置
upstream web {
server 172.0.0.1:10000 weight=1 max_fails=2 fail_timeout=6s;
server 172.0.0.2:10001 weight=1 max_fails=2 fail_timeout=6s;
}
server {
listen 8080 ssl;
ssl_certificate /home/tomcat.crt;
ssl_certificate_key /home/tomcat.private.key.nokey;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web;
}
}
tomcat配置
<Connector port="8080" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="/home/tomcat.keystore" keystorePass="password"
/>
客户端java程序请求:
需要用到 client.keystore.p12 和 client.keystore(已导入服务端证书的)
java程序跑一跑,测试是否请求成功【PS 后续我会放在github上,当下有不懂的可以发邮件问我拿 t953170224@163.com】
7955




被折叠的 条评论
为什么被折叠?



