自己使用keytool生成证书 和 购买 商业SSL证书,然后并配置Tomcat 的 https

一、SSL证书简介

要想使用https, 首先,我们需要有SSL证书,证书可以通过两个渠道获得:

  • 1.自己生成

虽然安全性不是那么高,但胜在成本低.

目前证书有以下常用文件格式:JKS(.keystore),微软(.pfx),PEM(.key + .crt)。其中,tomcat使用JKS格式,nginx使用PEM格式.

  • 2.公开可信认证机构

例如CA,但是申请一般是收费的。

二、自己生成

JDK自带了一个生成证书 keytool ,目录在 /bin 下面

1、生成证书

输入:(如果你没有配环境变量需要cd到bin目录执行,否则做不到keytool命令)

keytool -genkey -v -alias HaC -keyalg RSA -validity 3650 -keystore ~/cert/HaC.keystore
  • alias: 别名 这里起名HaC
  • keyalg: 证书算法,RSA
  • validity:证书有效时间,10年
  • keystore:证书生成的目标路径和文件名,替换成你自己的路径即可,我定义的是~/Lee/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".

2、其他操作,可以不用理会

导出证书:

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 

3、配置Tomcat

我本地使用的tomcat版本是tomcat-8.0.53

1、修改端口

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” 。

2、自动跳转

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/

image-20201012095954123

提示证书存在问题。然后点击 继续浏览,就可以使用HTTPS访问了。

给浏览器安装证书:

img

浏览器 一样提示 不安全的连接。没办法了,浏览器无法信任自定义的证书。

三、使用商用证书

商用的SSL实在是太贵了,比如说赛门铁克、亚信,个人一般都难以承受,let’s encrypt 是一个免费的SSL组织,申请后有3个月的期限,到期可以续杯。

2.1、申请证书

腾讯云 可以免费申请 1年的免费证书,我这里使用腾讯云为例子:

img

申请完毕,点击下载 , 解压看到这个压缩包有几种服务器的不同类型证书。我们是Tomcat的服务器,就把Tomcat的jks证书上传到服务器。

img

2.2、tomcat配置https

替换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"/>
2.3、设置http自动跳转到https

把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

现在就不会提示证书不安全了。完美!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醋酸菌HaC

请我喝杯奶茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值