Centos 7 配置Tomcat跳转Https

前言:在网络安全盛行的时代下,有时业务为了安全需求要使用https协议,包括http、nginx、tomcat等,本篇简单分享一下tomcat跳转https配置。

1、环境

Centos 7.9

2、下载apache-tomcat包(根据网速选择)

# 官方地址
wget -P /usr/local https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.84/bin/apache-tomcat-8.5.84.tar.gz

# 阿里云地址
wget -P /usr/local https://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.84/bin/apache-tomcat-8.5.84.tar.gz

3、进入安装包目录解压

cd /usr/local
tar -xvf apache-tomcat-8.5.84.tar.gz

4、配置Tomcat

cd apache-tomcat-8.5.84/conf/

# 编辑server.xml,找到下面配置
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
# 默认tomcat端口为8080,可以根据需求修改,保存退出。

# 启动tomcat
cd /usr/local/apache-tomcat-8.5.84/bin
./startup.sh

# 查看启动是否成功
ps aux | grep tomcat

# 查看端口是否有监听
netstat -ntlp | grep 8080

5、浏览器测试

http://ip地址:8080

6、配置https协议访问

# 要实现https,必须有tomcat证书,可以使用java自带的keytool证书生成工具生成。实际生产环境,安全的证书需要去申请。
keytool -genkeypair -alias 'tomcat' -keyalg 'RSA' -keystore '/usr/local/apache-tomcat-8.5.84/conf/tomcat.keystore'

此时在/usr/local/apache-tomcat-8.5.84/conf下面就会生成一个tomcat.keystore的证书

# 修改配置文件server.xml
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
	maxThreads="150" scheme="https" secure="true"
	clientAuth="false" sslProtocol="TLS"
	keystoreFile="/usr/local/apache-tomcat-8.5.84/conf/tomcat.keystore"
	keystorePass="123456" />
# keystoreFile为证书路径,keystorePass为生成证书的密码,默认监听8443端口,修改完后重启tomcat。

7、测试https是否成功

http://ip地址:8443

此时页面已经为https协议

8、配置强制页面使用https协议

# 上诉配置即可使用http,也可以使用https协议,如果需要强制使用https协议,需要修改web.xml配置。
<login-config>
	<auth-method>CLIENT-CERT</auth-method>
	<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<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> 
# 将以上配置添加到conf/web.xml的后面,重启tomcat。

9、测试强制跳转https
输入http://ip地址:8080,即可自动跳转到https页面。

结束语:至此tomcat的https协议配置完成,希望能够帮助到大家,觉得有用,感谢大家推荐,感谢!

posted @ 2022-11-29 14:01  谭咏麟  阅读(203)  评论(0编辑  收藏  举报