tomcat系列-06-HTTPS支持-商用证书-openssl操作

9 篇文章 0 订阅
7 篇文章 0 订阅

前言

上一篇,我们介绍了tomcat启用https支持,只是是在私有CA下启用的。
私有CA除了平时自己练习或者内网使用外,想不出其他用途了。

现在我们介绍下,tomcat使用真正的商用证书来支持https。

1 环境准备

  • 域名
    • 本人这里是在阿里云买的域名
    • 只要你想买的域名不是 google.com或在baidu.com这种白金次的话,一般都不贵吧……
    • 域名备不备案在这里无所谓了
    • 将域名解析到你自己的服务器
  • 公网服务器
    • 本人这里使用的是阿里云主机
    • 得搞个公网主机,不然域名解析到哪里去呢?
  • https证书

2 申请证书

这部分没啥问题的道友,直接跳过吧……

为方便,以下操作都在 /soft/tomcat7-80/ssl目录执行

2.1 生成应用程序私钥

(umask;openssl genrsa -out tomcat.key 2048)
# 此处在子shell中执行,并将遮罩码设置为077,
# 以便生成的文件tomcat.key的mod==600

2.2 提交CSR到CA(startssl.com)

https://startssl.com/ 有两种方式来提交CSR:

  • 自己生成CSR
  • 通过IE浏览器

此处可以同keytool或者openssl来自己生成CSR。
本人这里选择自己用openssl工具生成(使用keytool的话也差不多的过程):

# 生成证书请求保存至tomcat.csr
[root@hylexus ssl]# openssl req -new -key tomcat.key -out tomcat.csr
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) [CN]:
State or Province Name (full name) [ShangHai]:
Locality Name (eg, city) [ShangHai]:
Organization Name (eg, company) [KKBC]:
Organizational Unit Name (eg, section) [Tech]:
Common Name (eg, your name or your server's hostname) []:hylexus.top
Email Address []:hylexus@163.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@hylexus ssl]# 

将自己的CSR内容粘贴到https://startssl.com/提供的输入框中即可:

startSSL提交csr

2.3 下载CA颁发的证书

申请结果:

申请结果

点击下载即可,本人这里hylexus.top.zip的压缩包,内容如下:

[root@hylexus https]# tree
.
├── ApacheServer.zip # apache/httpd的证书
├── IISServer.zip # IIS的证书
├── NginxServer.zip # Nginx的证书
└── OtherServer.zip # 其他服务器的证书,这里我们就使用这个了

3 配置tomcat支持HTTPS

这里我们使用从CA下载的OtherServer.zip文件中2_hylexus.top.crt文件为例,配置tomcat。
并将复制至:${catalina.home}/ssl/tomcat.crt

cp 2_hylexus.top.crt /soft/tomcat7-80/ssl/tomcat.crt
cp 1_Intermediate.crt /soft/tomcat7-80/ssl/ca.cert

# 此时的ssl目录
[root@hylexus ssl]# tree
.
├── ca.cert # CA自己的证书
├── tomcat.crt # 刚刚复制的CA颁发的证书
├── tomcat.csr # 证书申请请求文件,现在可以删了
└── tomcat.key # 应用程序私钥

3.1 基于APR-Connector

如何启用APR,可以参考本人另一篇文章:http://blog.csdn.net/hylexus/article/details/53137721

<Connector
     protocol="org.apache.coyote.http11.Http11AprProtocol"
     port="8443" maxThreads="200"
     scheme="https" secure="true" SSLEnabled="true"
     SSLCertificateFile="${catalina.base}/ssl/tomcat.crt"
     SSLCertificateKeyFile="${catalina.base}/ssl/tomcat.key"
     SSLVerifyClient="optional" SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"/>

3.2 基于NIO、AIO-Connector

由于上面本人使用openssl工具生成的私钥,而基于NIO和BIO的Connector配置中HTTPS的支持必须依赖于java的标准keystore文件。
所以,现在有个麻烦的事就是将现成的私钥和证书导入到某个keystore中。
查了好久资料,终于找到方法了………………(keytool并没有直接提供这个功能^)

keytool的使用示例可以参考:http://blog.csdn.net/hylexus/article/details/53145973

  • 1) 将私钥和证书导入keystore

借助于openssl工具实现转换,注意记住以下操作要求你输入的密码哦:

# ks.p12这中格式的输出文件可以当做一个keystore来用
openssl pkcs12 -export -name tmp -in tomcat.crt -inkey tomcat.key -out ks.p12

# 从ks.p12这个非标准的keystore导入到新的keystore:ks
keytool -importkeystore -destkeystore ks -srckeystore ks.p12 -srcstoretype pkcs12 -alias tmp

此时的ssl目录

[root@hylexus ssl]# tree
.
├── ks # 刚刚转换生成的keystore
├── ks.p12 # 临时的非标准的keystore,现在可以删了
├── tomcat.crt # 当前配置下可删除
├── tomcat.csr # 当前配置下可删除
└── tomcat.key # 当前配置下可删除


# 以上标注了"当前配置下可删除"的三个文件,
# 在非APR类型的Connector下这个文件是没啥用处的
# 因为私钥和证书都在keystore文件ks中了
  • 2) 配置server.xml

NIO-Connector配置

<Connector
       protocol="org.apache.coyote.http11.Http11NioProtocol"
       port="8443" maxThreads="200"
       scheme="https" secure="true" SSLEnabled="true"
       keystoreFile="${catalina.home}/ssl/ks" keystorePass="123456"
       clientAuth="false" sslProtocol="TLS"/>

BIO-Connector配置

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
           connectionTimeout="20000" SSLEnabled="true"
           maxThreads="150" scheme="https" secure="true"
           clientAuth="false" sslProtocol="TLS"
           keystoreFile="${catalina.home}/ssl/ks" keystorePass="123456"
           />

4 效果预览

效果预览

再也没有那个恶心的警告了^_^

参考文章

https://www.oschina.net/translate/switch-to-https-now-for-free?cmp

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值