环境: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>