https 协议 -- tomcat配置

本文介绍如何利用JDK自带的keytool命令创建证书,并将其配置到Tomcat服务器上实现HTTPS加密通信。文章详细说明了创建证书所需的参数及其含义,并提供了配置Tomcat server.xml文件的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用jdk自带的keytool命令创建一个证书
keytool -genkeypair -alias tomcat -keyalg "RSA" -keystore "coffee.keystore"  

功能:
        创建一个别名为tomcat的证书,该证书存放在名为coffee.keystore的密钥库中,若coffee.keystore密钥库不存在则创建。
        参数说明:
        -genkeypair:生成一对非对称密钥;
        -alias:指定密钥对的别名,该别名是公开的;
        -keyalg:指定加密算法,本例中的采用通用的RAS加密算法;
        -keystore:密钥库的路径及名称,不指定的话,默认在操作系统的用户目录下生成一个".keystore"的文件

我的win10 默认会在 C:\Users\coffee\.keystore目录下生成,

指定了以后就会在当前目录下生产一个keystore文件 ‪C:\Users\coffee\Desktop\coffee.keystore

然后配置tomcat的server.xml

	<Connector
			  port="8443" protocol="HTTP/1.1" SSLEnabled="true"
              maxThreads="150" scheme="https" secure="true"
              clientAuth="false" sslProtocol="TLS"
              keystoreFile="C:/Users/coffee/Desktop/coffee.keystore" keystorePass="coffee"/>

——————————————————————————

需要注意三个问题

一、alias的密码和密钥库的口令必须一致,否则在tomcat中会报错

Tomcat : java.io.IOException: Keystore was tampered with, or password was incorrect

The password in Tomcat’s “<Connector>” is NOT matched with the password you assigned to your keystore.

File : $Tomcat\conf\server.xml

//...
 <!-- Define a SSL HTTP/1.1 Connector on port 8443
        This connector uses the JSSE configuration, when using APR, the
        connector should be using the OpenSSL style configuration
        described in the APR documentation -->

<Connector port="8443" //...
	keystorePass="password here must same with your keystore's password" />

 //...
To fix this, make sure both are using same password, and passwords are case sensitive as well :)
二、keytool设置的alias不是"tomcat" 回导致启动tomcat失败
12-Nov-2016 13:06:18.187 WARNING [main] org.apache.tomcat.util.net.openssl.OpenSSLContext.init Error initializ ing SSL context  
java.lang.NullPointerException
        at org.apache.tomcat.util.net.openssl.OpenSSLContext.init(OpenSSLContext.java:276)
        at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:100)
        at org.apache.tomcat.util.net.AbstractJsseEndpoint.initialiseSsl(AbstractJsseEndpoint.java:80)
        at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:245)
这个错误是由于keytool设置的alias不是"tomcat"导致的

三、你的名字和姓氏

必须输入域名 localhost

并且 访问浏览器的时候 需要访问https://localhost:8443 而不是http



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值