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