暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

tomcat配置https自签名证书(keytool生成)

IT运维大爆炸 2023-02-09
1548

1、简介

相较于http、https提供了身份验证与加密通信方法,于是,出于安全性考虑,越来越多的网站开始使用https协议。

2、环境准备

为了成功配置https,你需要具备以下环境:

  • java jdk

  • tomcat

3、SSL证书简介

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

  • 公开可信认证机构:例如CA,但是申请一般是收费的,一般几百到几千一年。

  • 自己生成:虽然安全性不是那么高,但胜在成本低。

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

虽然两种方式博主都已经实现过,但是这里只讲JKS格式

4、JKS格式证书生成

1.打开你的终端或者命令行或者其他黑窗口,输入:

[root@10-60-249-255 ~]# keytool -genkeypair -alias "server" -keyalg "RSA" -validity "365" -keystore "/app/webapp/tomcat/https/server.keystore"
  • alias: 别名 这里起名server。

  • keyalg: 证书算法,RSA。

  • validity:证书有效时间,1年。

  • keystore:证书生成的目标路径和文件名,替换成你自己的路径即可,我定义的是/app/webapp/tomcat/https/server.keystore。

2.回车,然后会让你输入一些信息,其中秘钥库口令和秘要口令最好输入同一个,并且记下这个口令,其他的随便填即可

[root@10-60-249-255 ~]# keytool -genkeypair -alias "server" -keyalg "RSA" -validity "365" -keystore "/app/webapp/tomcat/https/server.keystore"
Enter keystore password:  
Re-enter new password:
What is your first and last name?
[Unknown]:  10.13.22.102
What is the name of your organizational unit?
[Unknown]: ai
What is the name of your organization?
[Unknown]: ai
What is the name of your City or Locality?
[Unknown]: gz
What is the name of your State or Province?
[Unknown]: gd
What is the two-letter country code for this unit?
[Unknown]: cn
Is CN=10.13.22.102, OU=ai, O=ai, L=gz, ST=gd, C=cn correct?
[no]:  yes

Enter key password for <server>
      (RETURN if same as keystore password):  
Re-enter new password:

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 app/webapp/tomcat/https/server.keystore -destkeystore app/webapp/tomcat/https/server.keystore -deststoretype pkcs12".

<!--在你刚才的目标路径里拿到生成好的server.keystore-->

5、tomcat配置https

把keystore证书上传到你的tomcat服务器上(如果你的tomcat在本地,那么不移动也可以),并记下证书所在路径。

cd到tomcat的conf目录下,打开server.xml文件,搜索https找到下面这项:

先去掉注释,然后将keystoreFile和keystorePass处替换成你自己的证书路径和生成证书时的口令即可。

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
              maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
              clientAuth="false" sslProtocol="TLS" keystoreFile="你的keystore路径" keystorePass="生成证书时的口令" />
  • port: https的端口,默认8443。

  • clientAuth:设置是否双向验证,默认为false,设置为true代表双向验证keystoreFile。

  • keystoreFile: keystore证书的路径。

  • keystorePass: 生成keystore时的口令。

启动tomcat,然后就可以使用https和8443端口访问你的服务了!  

欢迎大家扫码关注:

本公众号只写原创,不接广告、不接广告、不接广告。下期小伙伴想学习什么技术,可以私信发我吆。

文章转载自IT运维大爆炸,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论