Tomcat全局/局部HTTP自动跳转HTTPS协议、项目部署不加项目名自动跳转

环境:Tomcat 8.5/8.0(Linux/Windows10均测试通过)

一、【全局】1.配置Tomcat conf目录下server.xml文件

(1) 打开/app/apache-tomcat/conf/server.xml,添加:

<Connector protocol="org.apache.coyote.http11.Http11NioProtocol"
           port="443" maxThreads="200"
           scheme="https" secure="true" SSLEnabled="true"
           keystoreFile="path/to/cer" keystorePass="password" keyPass="password" keyAlias="passport"
           clientAuth="false" sslProtocol="TLS"/>

 

(2) 修改:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" URIEncoding="UTF-8" />

修改后:

<Connector port="80" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="443" URIEncoding="UTF-8" />

(3) 修改:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

修改后:

<Connector port="8009" protocol="AJP/1.3" redirectPort="443" />

2. 配置conf/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>

此时在浏览器输入localhost即可实现自动跳转为https:localhost,同时出现证书信任提示(可能!)

 

二、【局部】

1. server.xml配置和全局中相同

2. conf/web.xml配置

<security-constraint>
	<web-resource-collection >
		<web-resource-name >SSL</web-resource-name>
		<url-pattern>/MyWebapp</url-pattern>
	</web-resource-collection>
	<user-data-constraint>
		<transport-guarantee>CONFIDENTIAL</transport-guarantee>
	</user-data-constraint>
</security-constraint>

注:MyWebapp为部署的项目名

3. webapps/MyWebapp/WEB-INF/web.xml配置

添加:

<security-constraint>
	<web-resource-collection>
		<web-resource-name>SSL</web-resource-name>
		<url-pattern>/jsp/*</url-pattern>
	</web-resource-collection>
	<user-data-constraint>
		<transport-guarantee>CONFIDENTIAL</transport-guarantee>
	</user-data-constraint>
</security-constraint>

注:实现/jsp目录下所有路径跳转

 

三、项目部署不加项目名自动跳转

打开Tomcat中/webapps/ROOT/index.jsp文件

删除index.jsp全部内容;添加:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script language="javascript" type="text/javascript">             
	window.location.href="/MyWebapp"; <!--跳转页面-->
</script>
“好像没跳诶!”
</head>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值