Tomcat 配置https方式

版权声明: https://blog.csdn.net/qq_32157851/article/details/80830204
1.进入jdk的安装目录的bin目录下

2.输入以下指令生成证书文件
keytool -v -genkey -alias tomcat -keyalg RSA -keystore d:/tomcat.keystore -validity 36500
付:
d:tomcat.keystore是将生成的证书文件tomcat.keystore放在d盘中
-validity 36500 代表证书的使用有效期,36500表示100年,默认为90天

3.输入证书的密码
此密码需要在后续的server.xml文件中使用到,请牢记。这里使用123456作为密码

4.根据提示输入所需内容
A、"Enter keystore password ":此处输入大雨6个字符的字符串
B、"What is you first and last name?" 必填项,必须是TOMCAT部署主机的域名或者IP[如:gbcom.com 或者 10.1.25.251],就是你将来要在浏览器中输入的访问地址
C、"What is the name of your organizational unit?"、"What is the name of your organization?"、"What is the name of your City or Locality?"、"What is the name of your State or Province?"、"What is the two-letter country code for this unit?"可以按照需要填写也可以不填写直接回车,在系统询问"correct?"时,对照输入信息,如果符合要求则使用键盘输入字母"y",否则输入“n”重新填写上面的信息

5.输入完成后会出现确认提示
此时输入y,并回车。此时创建完成keystore。
进入到D盘根目录下可以看到已经生成的tomcat.keystore

6.进入到tomcat的conf目录下,对server.xml文件进行修改

7.编辑
将<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
段代码的注释去掉,并修改为以下形式:

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" keystoreFile="D:/tomcat.keystore" keystorePass="123456" sslProtocol="TLS" />

红色部分代码为需要新加入的部分,其中keystoreFile表示生成的证书的路径;keystorePass就是第三步中所定义的密码123456
编译完成后保存并关闭server.xml文件

8.Tomcat启动成功后,使用https://127.0.0.1:8443进行访问测试。成功打开表示配置成功

9.应用程序HTTP自动跳转HTTPS,所有访问会被强制成HTTPS方式(可选)
在应用程序的web.xml文件中加入以下代码:

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

猜你喜欢

转载自blog.csdn.net/qq_32157851/article/details/80830204