要想使用https, 首先,我们需要有SSL证书,证书可以通过两个渠道获得:
虽然安全性不是那么高,但胜在成本低.
目前证书有以下常用文件格式:JKS(.keystore),微软(.pfx),PEM(.key + .crt)。其中,tomcat使用JKS格式,nginx使用PEM格式.
例如CA,但是申请一般是收费的。
JDK自带了一个生成证书 keytool ,目录在 /bin 下面
输入:(如果你没有配环境变量需要cd到bin目录执行,否则做不到keytool命令)
keytool -genkey -v -alias HaC -keyalg RSA -validity 3650 -keystore ~/cert/HaC.keystore
然后随便输入,但是密码要记得:
[root@VM-8-8-centos ~]# keytool -genkey -v -alias HaC -keyalg RSA -validity 3650 -keystore ~/cert/HaC.keystore
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]: a
What is the name of your organizational unit?
[Unknown]: b
What is the name of your organization?
[Unknown]: c
What is the name of your City or Locality?
[Unknown]: d
What is the name of your State or Province?
[Unknown]: e
What is the two-letter country code for this unit?
[Unknown]: f
Is CN=a, OU=b, O=c, L=d, ST=e, C=f correct?
[no]: y
Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 3,650 days
for: CN=a, OU=b, O=c, L=d, ST=e, C=f
Enter key password for <HaC>
(RETURN if same as keystore password):
Re-enter new password:
[Storing /root/cert/HaC.keystore]
Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore /root/cert/HaC.keystore -destkeystore /root/cert/HaC.keystore -deststoretype pkcs12".
导出证书:
123456是我在上面输入的密码。
keytool -exportcert -rfc -alias HaC -file ~/cert/HaC.cer -keystore ~/cert/HaC.keystore -storepass 123456
[root@VM-8-8-centos apache-tomcat-8.0.53]# keytool -exportcert -rfc -alias HaC -file ~/cert/HaC.cer -keystore ~/cert/HaC.keystore -storepass 123456
Certificate stored in file </root/cert/HaC.cer>
导入到jvm的库
-storepass changeit
表示默认密码是 changeit ,changeit 是jvm的默认密码。不是上面的123456。
keytool -import -v -trustcacerts -alias HaC -file ~/cert/HaC.cer -storepass changeit -keystore /var/www/web/jdk/jdk1.8.0_261/jre/lib/security/cacerts
删除
keytool -delete -alias testkey -keystore /var/www/web/jdk/jdk1.8.0_261/jre/lib/security/cacerts -storepass changeit
查看证书
keytool -list -v -alias HaC -keystore /var/www/web/jdk/jdk1.8.0_261/jre/lib/security/cacerts -storepass changeit
我本地使用的tomcat版本是tomcat-8.0.53
tomcat的默认https端口是8443,而https的端口是443,那需要把8443改一下,如果不改,你可以通过 https://www.xxx.cn:8443 这样访问,但是就怪怪的。
找到tomcat目录下的conf/server.xml文件,修改:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
表示80端口跳转到443端口,即http的请求都转发到https
这个没有用到,也改了:
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="443" />
接着把这段注释放开:
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="/root/cert/HaC.keystore" keystorePass="123456"/>
keystoreFile是你的证书路径,keystorePass的密码是你之前输入的密码。这里的port改成 port=“443” 。
http的请求都转发到https
找到tomcat目录下的conf/web.xml文件,末尾 加入:
<!-- ====================自动跳转——start ===================== -->
<security-constraint>
<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>
<!-- ====================自动跳转——end ===================== -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
重启Tomcat,访问 https://81.71.16.134/ 或者 http://81.71.16.134/
提示证书存在问题。然后点击 继续浏览,就可以使用HTTPS访问了。
给浏览器安装证书:
浏览器 一样提示 不安全的连接。没办法了,浏览器无法信任自定义的证书。
商用的SSL实在是太贵了,比如说赛门铁克、亚信,个人一般都难以承受,let’s encrypt 是一个免费的SSL组织,申请后有3个月的期限,到期可以续杯。
腾讯云 可以免费申请 1年的免费证书,我这里使用腾讯云为例子:
申请完毕,点击下载 , 解压看到这个压缩包有几种服务器的不同类型证书。我们是Tomcat的服务器,就把Tomcat的jks证书上传到服务器。
替换server.xml的jks证书路径和密码即可:
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="/root/cert/www.baimuxym.cn.jks" keystorePass="i24vj6841f1"/>
把http的请求都转发到https
找到tomcat目录下的conf/web.xml文件,末尾 加入:
<!-- ====================自动跳转——start ===================== -->
<security-constraint>
<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>
<!-- ====================自动跳转——end ===================== -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
然后重启Tomcat。访问 https://baimuxym.cn/
现在就不会提示证书不安全了。
上面访问了域名,跳到了tomcat的首页,那要跳转到我们自定义的网站怎么办呢?
找到tomcat目录下的conf/server.xml文件,在标签之间添加上:
<Context path="" docBase="/var/www/web/HaCresume/" debug="0" reloadable="true"/>
表示Tomcat的根目录就是这个了,不再是webapps下面了。
找到tomcat目录下的conf/web.xml文件,修改首页:
<welcome-file-list>
<welcome-file>HaCresume.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
表示我的 首页 就是 /var/www/web/HaCresume/HaCresume.html
了。
然后重启Tomcat。访问 https://baimuxym.cn
现在就不会提示证书不安全了。完美!