Tomcat访问项目由http改为https设置

1. 先生成证书文件

  # 先切换到证书文件路径下:
  $ cd /app/tomcat-8.5.39/
  
  # 生成证书文件:
  $ keytool -genkey -alias tomcat -keyalg RSA -keypass AAyxl123 -storepass AAyxl123 
      -keystore tomcat-https-server.keystore -validity 36500

  # 证书赋执行权限:
  $ chmod u+x tomcat-https-server.keystore

生成证书文件说明:
证书密码:AAyxl123
证书文件:tomcat-https-server.keystore, 在当前tomcat目录下
证书有效期:36500 即 100年

在这里插入图片描述
代码:

[app@chezhi tomcat-8.5.39]$ pwd
/app/tomcat-8.5.39
[app@chezhi tomcat-8.5.39]$ keytool -genkey -alias tomcat -keyalg RSA -keypass AAyxl123 -storepass AAyxl123 -keystore tomcat-https-server.keystore -validity 36500
What is your first and last name?
  [Unknown]: chezhi
What is the name of your organizational unit?
  [Unknown]: xlwzj
What is the name of your organization?
  [Unknown]: xlwzj
What is the name of your City or Locality?
  [Unknown]: CHANGSHA	
What is the name of your State or Province?
  [Unknown]: HUNAN
What is the two-letter country code for this unit?
  [Unknown]: CN 
Is CN=chezhi, OU=xlwzj, O=xlwzj, L=CHANGSHA, ST=HUNAN, C=CN correct?
  [no]: Y
[app@chezhi tomcat-8.5.39]$ ls -ls tomcat-https-server.keystore 
4 -rw-rw-r--. 1 app app 2224 Jan 11 11:04 tomcat-https-server.keystore
[app@chezhi tomcat-8.5.39]$

提问填写说明:

“您的名字与姓氏是什么?”这是必填项,是证书的拥有者,我输入的是 “chezhi”
“你的组织单位名称是什么?”
“您的组织名称是什么?”
“您所在城市或区域名称是什么?”
“您所在的州或者省份名称是什么?”
“该单位的两字母国 家代码是什么?”
可以按照需要填写也可以不填写直接回车,在系统询问“正确吗?”时,对照输入信息,如果符合要求则使用键盘输入字母“y”,否则输入“n”重新填写上面的信息。

2. 修改tomcat配置文件
修改 tomcat/conf/server.xml

  # 1. 修改 8080端口配置:端口指向由原先的8443改成 443,因为http默认端口是80,所以我把 8080 改成了80 ,非必改,有需要则改
  <Connector port="80" protocol="HTTP/1.1"
	       connectionTimeout="20000" redirectPort="443" />

 # 2. 修改8443端口配置,特别重要:
 # 把注释打开,8443端口改成 443, 因为https默认端口是443,
 <Connector port="443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" 
   	scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" 
   	keystoreFile="/app/tomcat-8.5.39/server.keystore"
    keystorePass="AAyxl123"/>

 # 3. 修改8009端口配置:
 # 把8009端口指向8443改成443
 <Connector port="8009" protocol="AJP/1.3" URIEncoding="utf-8" 
	redirectPort="443" />
	

说明:

keystoreFile: 为之前在tomcat目录下生成的证书文件
keystorePass:为之前生成的证书密码
scheme:改成为 “https”

修改截图:
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

由于第一次证书配置路径写成了 “/app/apache-tomcat-8.5.39/server.keystore” 报错, 改成 “/app/tomcat-8.5.39/server.keystore” 就好了,注意别向我一样配错了

2. 修改 tomcat/conf/web.xml
拖到文档末尾的前面添加如下内容:

vim web.xml ,技巧:vim 编辑器下同时按住 shift + g 键直接跳到文档末尾

   <login-config>
		<!-- Authorization setting for SSL -->
		<auth-method>CLIENT-CERT</auth-method>
		<realm-name>Client Cert Users-only Area</realm-name>
	</login-config>
	<security-constraint>
		<!-- Authorization setting for SSL -->
		<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>

3.重启tomcat服务

tomcat 重启中。。。

4.浏览器访问测试
在地址栏里面分别输入访问地址测试:

http://192.168.3.128/xlw_web/
https://192.168.3.128/xlw_web/
在这里插入图片描述
无论你在地址栏里面输入的是 http 还是 https, 都会跳转到我们设置的 https上进行访问。
好啦,访问测试没问题的。

勇于探索你就会发现原来世界就是这的神奇。。。

                                          2020-01-11    写于长沙
发布了28 篇原创文章 · 获赞 2 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/u012733521/article/details/103935096