这是因为response.sendRedirect();
SSL 卸载服务器实际上会导致 https URL 的问题
在 sendRedirect 的情况下转换为 http URL。让我们尝试一下
了解这一切是如何发生的。当 https URL 到达
中间 SSL 卸载服务器,请求被解码(以卸载
实际目标将完成的解码任务
服务器(否则)转换为 http 请求,SSL Offloader 现在发送
解码到实际目标服务器的http请求(它可能会去
也通过少数其他中间服务器用于其他目的)。
现在,目标服务器不知道它最初是否是 https
请求与否,它仅将其视为 http 请求。这是
servlet 在目标服务器上调用 sendRedirect 的原因
结果转化为重定向资源的 http URL(具有相对的
URL)并且相同的http URL被发送回客户端浏览器。
请记住,此 URL 不会出现任何 SSL 卸载服务器
因为它是一个 http 请求而不是 https 请求。如果重定向
资源是绝对 https URL,然后也是中间 SSL
Offloader 可能会先将该 https 请求转换为 http 请求
它到达客户端浏览器。
该问题的解决方案是在 HttpServletResponseWrapper 中实现 Filter 并覆盖 sendRedirect。
Filter.
public class AbsoluteSendRedirectFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException { }
public void destroy() { }
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//continue the request
chain.doFilter(request, new SendRedirectOverloadedResponse(request, response));
}
}
HttpServletResponseWrapper
public class SendRedirectOverloadedResponse extends HttpServletResponseWrapper {
private HttpServletRequest m_request;
private String prefix = null;
public SendRedirectOverloadedResponse(HttpServletRequest inRequest,
HttpServletResponse response) {
super(response);
m_request = inRequest;
prefix = getPrefix(inRequest);
}
public void sendRedirect(String location) throws IOException {
String finalurl = null;
if (isUrlAbsolute(location)) {
finalurl = location;
} else {
finalurl = fixForScheme(prefix + location);
}
super.sendRedirect(finalurl);
}
public boolean isUrlAbsolute(String url) {
return url.toLowerCase().startsWith("http");
}
public String fixForScheme(String url) {
//alter the url here if you were to change the scheme return url;
}
public String getPrefix(HttpServletRequest request) {
StringBuffer str = request.getRequestURL();
String url = str.toString();
String uri = request.getRequestURI();
int offset = url.indexOf(uri);
String prefix = url.substring(0,offset);
}
}
资源来自这里:
http://www.hoitikwong.com/2013/03/the-mystery-case-of-https-becoming-http.html and
http://geekexplains.blogspot.in/2008/06/https-becoming-http-in-case-of.html