1 使用java拦截器方法实现 协议跳转(目前可以不用,使用下面js方法即可,这个是提供的另外一种方法)
//该类实现了 Filter 接口,在 web.xml需要配置拦截器
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
String contextPath = httpServletRequest.getRequestURI();
//获取需要转换为 https的请求路径,这个配置在 xml文件里
List<String> resultList = getList()
//isSecure()用来检测是否是 https 的请求
if (httpServletRequest.isSecure() && resultList.contains(contextPath)) {
//此处用来做页面重定向处理,如果路径需要转为 https,则 重定向
response.sendRedirect(........);
}
else {
chain.doFilter(request, response);
}
return;
}
<filter>
<filter-name>Redirect</filter-name>
<filter-class>com.abc.def.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Redirect</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2 js方法
if (window.location.protocol == "http:") {
if (window.location.replace) {
window.location.replace("https" + window.location.href.substr(4));
} else {
window.location.href = "https" + window.location.href.substr(4)
}
}
3 tomcat 主要配置:
<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="443"/>
<!--配置了 443端口,keystore 路径, 和密码-->
<Connector port="443" SSLEnabled="true" clientAuth="false" keystoreFile="d:/a/b/tomcat.keystore" keystorePass="222555" maxThreads="150" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="443"/>
//该类实现了 Filter 接口,在 web.xml需要配置拦截器
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
String contextPath = httpServletRequest.getRequestURI();
//获取需要转换为 https的请求路径,这个配置在 xml文件里
List<String> resultList = getList()
//isSecure()用来检测是否是 https 的请求
if (httpServletRequest.isSecure() && resultList.contains(contextPath)) {
//此处用来做页面重定向处理,如果路径需要转为 https,则 重定向
response.sendRedirect(........);
}
else {
chain.doFilter(request, response);
}
return;
}
<filter>
<filter-name>Redirect</filter-name>
<filter-class>com.abc.def.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Redirect</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2 js方法
if (window.location.protocol == "http:") {
if (window.location.replace) {
window.location.replace("https" + window.location.href.substr(4));
} else {
window.location.href = "https" + window.location.href.substr(4)
}
}
3 tomcat 主要配置:
<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="443"/>
<!--配置了 443端口,keystore 路径, 和密码-->
<Connector port="443" SSLEnabled="true" clientAuth="false" keystoreFile="d:/a/b/tomcat.keystore" keystorePass="222555" maxThreads="150" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="443"/>