`
sillycat
  • 浏览: 2552667 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Session between HTTPS and HTTP

    博客分类:
  • JAVA
 
阅读更多
Session between HTTPS and HTTP

Recently, we met a problem with sessions between HTTPS and HTTP. The step is as follow:
first page ---> put data in session ---> second page display session data -----> access HTTPS ---> third page display session data
click back space button in the third page, we came back to the second page, the session data is lost.
And we have this kind of data in the second step:
response.setHeader("Pragma", "no-cache")
response.addHeader("Cache-Control", "no-cache")
response.addHeader("Cache-Control", "no-store" )
response.addHeader("Cache-Control", "must-revalidate" )
response.setDateHeader("Expires", 0)
response.flushBuffer()

And These codes in page:
<META HTTP-EQUIV="Cache-control" CONTENT="no-cache">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">

We have SessionFixationProtectionFilter to protect we in the HTTPS steps.
The log from server side is as follow:
preparesession in controller with sessionId = 1ED16C12C04E06A7628173195C471D64
displaylogin in controller with sessionId = 1ED16C12C04E06A7628173195C471D64
SessionFixationProtectionFilter class entered here!!!!!!!!!!!!!!
06-16 13:06:12 [DEBUG] com.sillycat.easywebflow.util.SessionUtil.startNewSessionIfRequired(SessionUtil.java:34) - Invalidating session with Id '1ED16C12C04E06A7628173195C471D64' and migrating attributes.
06-16 13:06:12 [DEBUG] com.sillycat.easywebflow.util.SessionUtil.startNewSessionIfRequired(SessionUtil.java:54) - Started new session: 89C26D84D4FC67E202C31CA4C50E0CA4
GET username = null password = null sessionId = 89C26D84D4FC67E202C31CA4C50E0CA4
displaylogin in controller with sessionId = 0FDA5D828BE9EAD31454E7B34765DA3F

So, we can see, at the last step, we have a new session Id with value equal 0FDA5D828BE9EAD31454E7B34765DA3F. This is a new session id, that is why we lost all our data stored in session.

That is the reason beween HTTP and HTTPS, because the session created in HTTP can be passed to HTTPS, but HTTPS session can not be passed to HTTP after tomcat4.0.

How to fix this problem. I follow the guide from others and I can solve this problem like this, use another filter, every time, when it is newly create filter, and when it is secure, I will write the jsessionid into cookie.
package com.sillycat.easywebflow.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpsCookieWriterFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("HttpsCookieWriterFilter class entered here!!!!!!!!!!!!!!");
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
CookieRequestWrapper wrapperRequest = new CookieRequestWrapper(
httpRequest);
wrapperRequest.setResponse(httpResponse);
chain.doFilter(wrapperRequest, response);
}

public void init(FilterConfig filterConfig) throws ServletException {

}

public void destroy() {

}

}

package com.sillycat.easywebflow.filter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CookieRequestWrapper extends HttpServletRequestWrapper {

private HttpServletResponse response = null;

public CookieRequestWrapper(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) {
return;
}
// cookieOverWritten
Object cookieOverWritten = getAttribute("COOKIE_OVERWRITTEN_FLAG");
if (null == cookieOverWritten && isSecure()
&& isRequestedSessionIdFromCookie() && session.isNew()) {
System.out.println("CookieRequestWrapper class entered here!!!!!!!!!!!!!! and sessionId=" + session.getId());
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(-1);
String contextPath = getContextPath();
if ((contextPath != null) && (contextPath.length() > 0)) {
cookie.setPath(contextPath);
} else {
cookie.setPath("/");
}
response.addCookie(cookie); //
setAttribute("COOKIE_OVERWRITTEN_FLAG", "true");
}
}
}

<filter>
<filter-name>httpsCookieWriterFilter</filter-name>
<filter-class>com.sillycat.easywebflow.filter.HttpsCookieWriterFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>sessionFixationProtoctionFilter</filter-name>
<url-pattern>/user.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>httpsCookieWriterFilter</filter-name>
<url-pattern>/user.do</url-pattern>
</filter-mapping>
This will work, and it will use the jsessionid in the cookie, it will not create a new session. The log messages will be as follow:
SessionFixationProtectionFilter class entered here!!!!!!!!!!!!!!
HttpsCookieWriterFilter class entered here!!!!!!!!!!!!!!
CookieRequestWrapper class entered here!!!!!!!!!!!!!! and sessionId=A9CC7242FB755D0753B4A3D18A6B991A
GET username = null password = null sessionId = A9CC7242FB755D0753B4A3D18A6B991A
preparesession in controller with sessionId = A9CC7242FB755D0753B4A3D18A6B991A
displaylogin in controller with sessionId = A9CC7242FB755D0753B4A3D18A6B991A
SessionFixationProtectionFilter class entered here!!!!!!!!!!!!!!
06-16 17:45:41 [DEBUG] com.sillycat.easywebflow.util.SessionUtil.startNewSessionIfRequired(SessionUtil.java:34) - Invalidating session with Id 'A9CC7242FB755D0753B4A3D18A6B991A' and migrating attributes.
06-16 17:45:41 [DEBUG] com.sillycat.easywebflow.util.SessionUtil.startNewSessionIfRequired(SessionUtil.java:54) - Started new session: DA4AC9FA8777DA0DCBAC6C1D68C7A65F
HttpsCookieWriterFilter class entered here!!!!!!!!!!!!!!
CookieRequestWrapper class entered here!!!!!!!!!!!!!! and sessionId=DA4AC9FA8777DA0DCBAC6C1D68C7A65F
POST username = Karl password = kaishi sessionId = DA4AC9FA8777DA0DCBAC6C1D68C7A65F

And we can make these 2 filter classes together.
startNewSessionIfRequired(request, response);

CookieRequestWrapper wrapperRequest = new CookieRequestWrapper(
request);
wrapperRequest.setResponse(response);
chain.doFilter(wrapperRequest, response);
//chain.doFilter(request, response);

references:
http://en.wikipedia.org/wiki/HTTP_cookie
http://java-guru.iteye.com/blog/157897


分享到:
评论

相关推荐

    session,sql分页

    SELECT * FROM CTE WHERE RowNum BETWEEN 10 AND 20; ``` 这将返回第10到20行的数据。 3. **OFFSET 和 FETCH**:SQL Server 2012及更高版本引入了更直观的分页方式: ```sql SELECT * FROM table ORDER BY id ...

    securecrt 622 x64 tbe

    For SSH, Telnet, Telnet/SSL, and other protocols, SecureCRT's tabbed sessions reduce desktop clutter and make it easy to switch between sessions and organize groups of connected sessions. Extensive ...

    Bulletproof SSL and TLS,PDF , Ivan Ristic

    Differences between Protocol Versions 60 SSL 3 60 TLS 1.0 61 TLS 1.1 61 TLS 1.2 61 3. Public-Key Infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    ISSCC2017-03

    The papers presented in Session 3 of ISSCC 2017 highlight significant advancements in digital processor technology, ranging from high-performance CPUs optimized for cognitive computing and gaming to ...

    #GDC2017 Photogrammetry for Games

    Photogrammetry, the ... The session includes real-world photogrammetry examples and demonstrates full integration between commercial photogrammetry tools like Reality Capture and game engine pipelines.

    3GPP NAS Protocol for EPS

    The present document specifies the procedures used by the protocols for mobility management and session management between User Equipment (UE) and Mobility Management Entity (MME) in the Evolved ...

    node_pcap.zip

     console.log("Start of TCP session between " session.src_name " and " session.dst_name); }); tcp_tracker.on('end', function (session) {  console.log("End of TCP session between ...

    Artech House - SMS and MMS Interworking in Mobile Networks

    7.4.4 Transmission of Signaling Between a GSM and an IS-41 Network 136 7.5 Conclusion 136 Reference 136 CHAPTER 8 Connecting ASPs and ISPs with SMPP 137 8.1 Introduction 137 8.2 SMPP Sessions 137 viii...

    VoIP Demo源码演示

    RTC event handling), and the code to interface between RTC and the UI. The intent of this quasi- layering approach is to limit the direct hooks from the RTC backend to the UI and make it easier to ...

    jwts-not-safe-e-book.pdf

    In web applications, managing user sessions is crucial for maintaining state between the client and server. This involves handling user authentication and authorization effectively. The chapter ...

    Web Analytics 2.0: The Art of Online Accountability and Science of Customer Centricity

    - **Understanding Web Analytics 2.0**: This section covers the fundamental differences between Web Analytics 1.0 and 2.0, focusing on the importance of customer-centric analysis. - **The Evolution of ...

    ISO 14229-1-2013.pdf

    - **Service Primitive:** A basic unit of information exchanged between the client and server during a diagnostic session. - **Diagnostic Communication Interface (DCI):** The interface through which ...

    Cindy components v6.53 for All Delphi

    TcyCommunicate and TcyCommRoomConnector allows communication between applications running in same computer session. TcySearchFiles and TcyCopyfiles allow respectively search and copy files with pause/...

    ADOdb.Manual.chm

    We currently support MySQL, Oracle, Microsoft SQL Server, Sybase, ... You can store your session information using ADOdb for true portability and scalability. See adodb-session.php for more information.

    modsecurity handbook

    Situated between your web sites and the world, web application firewalls provide an additional security layer, monitoring everything that comes in and everything that goes out. They enable you to ...

    Cindy components for all Delphi versions

    TcyCommunicate and TcyCommRoomConnector allows communication between applications running in same computer session. TcySearchFiles and TcyCopyfiles allow respectively search and copy files with ...

    浅谈cookie和session(小结)

    Servlet容器使用这个接口来创建一个session between an HTTP client and an HTTP server。Session持久存在指定的时间周期内,跨越多个连接或页面请求。 Session可以存储用户信息,并且可以在多个页面请求之间共享...

    CAD转换EARTH

    CAD-Earth is designed to easily import/export images, objects and terrain meshes between Google Earth™ and AutoCAD® 2007-2014, and create dynamic contour lines and profiles. You can pick CAD-Earth ...

    UE(官方下载)

    The selected text compare allows you to select portions of text between 2 files and execute a compare on ONLY the se Using the SSH/telnet console A tutorial for UltraEdit/UEStudio's SSH/telent ...

Global site tag (gtag.js) - Google Analytics