---
title: "Https跳到http时session信息丢失的分析及解决方案"
author: "java.guru"
date: "2008-01-20T22:03:44Z"
tags: []
slug: "https-http-session"
draft: false
lastmod: "2008-01-20T22:03:44Z"
id: 157897
url: "https://java-guru.iteye.com/blog/157897"
---

我们在YMU([website monitoring](http://YouMonitor.Us "website monitoring"))项目开发过程中发现一个关于登录功能的奇怪的问题。

当按一般流程使用登录功能时是没问题的，即：点击官网 (http://YouMonitor.Us)的login链接，然后跳转到https://YouMonitor.Us/login.shtml，输入正确的用户名和密码后，则能正确转入功能页面(http协议)。

而如果跳过第一步，直接在浏览器中输https://YouMonitor.Us/login.shtml，则不能正确转入功能页面。

 

# **原因分析**

经调试发现是由于session造成的。当用户名和密码通过验证后，YMU会在session中保存登录用户名。

 

在第二种方式下，用户名被保存在https下创建的session中，而不能被传递到http协议，这样当以http协议跳转到功能页面时，发现session中没有用户名，系统就会认为没有登录，就出问题了。

再深入分析session的传递机制，其中一种方式是通过JSESSIONID这个cookie在浏览器和web server之间进行传递的。而为了增强安全性，从tomcat 4.0开始，在https协议下生成的cookie不会被传递到http协议。  
这就是登录问题产生的根本原因。

 

# 解决方案

前一段时间在网上找到一种解决方法，该方法修改tomcat的源代码，以解除cookie传递的限制。该方法的缺点是每次tomcat升级都要重新修改并编译，很不方便。

还有没有更方便的方法呢？上周公司一同事在[网上](http://forum.java.sun.com/thread.jspa?threadID=197150&amp%3BmessageID=2255222)又找到一个更加简洁和方面的方法。其主要思路是建立一个filter, 对所有的Servlet Request做处理，如果session是新的https session，则创建一个JSESSIONID cookie，并设置到Servlet Response中，这样就突破了tomcat的限制，把https下的session传递到http协议下。

根据文章说明试了一下，果然成功了！这下解决了系统的一个BUG，不错啊！

# 附：源代码

 

## 1. 先建立Wrapper类，用于处理所有的Serverlet Request:

public class MyRequestWrapper extends HttpServletRequestWrapper{  
private HttpServletResponse response = null;

public MyRequestWrapper(HttpServletRequest request) {  
super(request);  
}

public void setResponse(HttpServletResponse response) { this.response = response;}

public HttpSession getSession(){  
HttpSession session = super.getSession();  
processSessionCookie(session);  
return session;  
}

public HttpSession getSession(boolean create){  
HttpSession session = super.getSession(create);  
processSessionCookie(session);  
return session;  
}

private void processSessionCookie(HttpSession session){  
if (null == response || null == session) {  
// No response or session object attached, skip the pre processing  
return;  
}

// cookieOverWritten - 用于过滤多个Set-Cookie头的标志  
Object cookieOverWritten = getAttribute("COOKIE\_OVERWRITTEN\_FLAG");  
if (null == cookieOverWritten && isSecure() && isRequestedSessionIdFromCookie() && session.isNew()) {  
// 当是https协议，且新session时，创建JSESSIONID cookie以欺骗浏览器

Cookie cookie = new Cookie("JSESSIONID", session.getId());  
cookie.setMaxAge(-1); // 有效时间为浏览器打开或超时  
String contextPath = getContextPath();  
if ((contextPath != null) && (contextPath.length() &gt; 0)) {  
cookie.setPath(contextPath);  
}  
else {  
cookie.setPath("/");  
}

response.addCookie(cookie); // 增加一个Set-Cookie头到response  
setAttribute("COOKIE\_OVERWRITTEN\_FLAG", "true");// 过滤多个Set-Cookie头的标志  
}  
}

}

## 2. 再把上述Wrapper类与Filter建立关联：

public final class TestFilter implements Filter{  
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  
throws IOException, ServletException {  
MyRequestWrapper myrequest = new MyRequestWrapper(request);  
myrequest.setResponse(response);  
chain.doFilter(myrequest, response);  
}  
}