SessionReplace做mecache分布式同步

public class ReplaceSessionRequestWrapper extends HttpServletRequestWrapper {

//~ Static fields/initializers =============================================

private static final Log log = LogFactory.getLog(ReplaceSessionRequestWrapper.class);

private static final String SESSION_COOKIE_NAME = "JSESSIONID";

private static final String SESSION_URL_PARAM   = "jsessionid";

//~ Instance fields ========================================================

 

private CachedStoreHttpSession session = null;

 

private HttpServletRequest request = null;

private HttpServletResponse response = null;

private ServletContext context = null;

private String requestedSessionId = null;

private boolean sessionIdFromCookie = false;

private boolean sessionIdFromUri = false;

 

// ~ Constructors ===========================================================

 

public ReplaceSessionRequestWrapper(HttpServletRequest request, HttpServletResponse response, ServletContext context) {

super(request);

 

this.request = request;

this.response = response;

this.context = context;

}

 

// ~ Methods ================================================================

 

/*

* (non-Javadoc)

* @see javax.servlet.http.HttpServletRequestWrapper#getSession()

*/

public HttpSession getSession() {

return getSession(true);

}

 

/*

* (non-Javadoc)

* @see javax.servlet.http.HttpServletRequestWrapper#getSession(boolean)

*/

public HttpSession getSession(boolean create) {

if (session != null && session.getId() != null && session.isValid()) {

return session;

}

 

session = null;

 

SessionManager manager = SessionManagerFactory.getSessionManager();

 

String id = getRequestedSessionId();

 

if (id == null) { // SESSION ID IS NOT SPECIFIED

if (!create) {

return null;

}

return createNewSession(manager);

}

Session cachedSession = manager.getSession(id);

if (cachedSession == null || !(cachedSession instanceof CachedStoreHttpSession)) {

if (!create) {

return null;

}

return createNewSession(manager);

}

CachedStoreHttpSession cachedStoreHttpSession = (CachedStoreHttpSession) cachedSession;

cachedStoreHttpSession.setServletContext(context); // SERVLET CONTEXT is not stored

session = cachedStoreHttpSession;

return session;

}

private HttpSession createNewSession(SessionManager manager) {

String id = new RandomGUID().toString();

while (manager.getSession(id) != null) {

id = new RandomGUID().toString();

}

CachedStoreHttpSession cachedSession = new CachedStoreHttpSession(id, context);

cachedSession.setNew(true);

cachedSession.setMaxInactiveInterval(manager.getMaxInactiveInterval());

manager.saveSession(cachedSession);

session = cachedSession;

// Creating a new session cookie based on that session

Cookie cookie = new Cookie(SESSION_COOKIE_NAME, session.getId());

cookie.setPath(CookieUtils.getCookiePath(request));

cookie.setMaxAge(-1);

if (isSecure()) {

            cookie.setSecure(true);

        }

response.addCookie(cookie);

if (log.isDebugEnabled()) {

log.debug("New Session[" + cachedSession.getId() + "] created");

}

return session;

}

/*

* @see javax.servlet.http.HttpServletRequestWrapper#getRequestedSessionId()

*/

@Override

public String getRequestedSessionId() {

if (requestedSessionId == null) {

requestedSessionId = super.getRequestedSessionId();

if (requestedSessionId != null) {

SessionManager manager = SessionManagerFactory.getSessionManager();

if (manager.getSession(requestedSessionId) == null) {

requestedSessionId = null;

} else {

sessionIdFromCookie = super.isRequestedSessionIdFromCookie();

sessionIdFromUri = super.isRequestedSessionIdFromURL();

}

}

if (requestedSessionId == null) {

requestedSessionId = parseSessionIdFromCookie();

}

if (requestedSessionId == null) {

requestedSessionId = parseSessionIdFromUri();

}

if (requestedSessionId == null) {

requestedSessionId = getParameter(SESSION_URL_PARAM);

}

}

return requestedSessionId;

}

/*

* @see javax.servlet.http.HttpServletRequestWrapper#isRequestedSessionIdFromCookie()

*/

@Override

public boolean isRequestedSessionIdFromCookie() {

return sessionIdFromCookie;

}

/*

* @see javax.servlet.http.HttpServletRequestWrapper#isRequestedSessionIdFromURL()

*/

@Override

public boolean isRequestedSessionIdFromURL() {

return sessionIdFromUri;

}

/*

* @see javax.servlet.http.HttpServletRequestWrapper#isRequestedSessionIdFromUrl()

*/

@Override

public boolean isRequestedSessionIdFromUrl() {

return sessionIdFromUri;

}

private String parseSessionIdFromCookie() {

String id = null;

SessionManager manager = SessionManagerFactory.getSessionManager();

Cookie[] cookies = request.getCookies();

        if (cookies != null && cookies.length > 0) {

            for (int i = 0; i < cookies.length; i++) {

                if (SESSION_COOKIE_NAME.equalsIgnoreCase(cookies[i].getName())) {

                    if (id != null) {

                        // Multiple jsessionid cookies. Probably due to

                        // multiple paths and/or domains. Pick the first

                        // known session or the last defined cookie.

                        if (manager.getSession(id) != null)

                         break;

                    }

 

                    id = cookies[i].getValue();

                }

            }

        }

 

        if (id != null) {

         sessionIdFromCookie = true;

        }

 

        return id;

}

private String parseSessionIdFromUri() {

String id = null;

String uri = request.getRequestURI();

 

        int semi = uri.lastIndexOf(';');

        if (semi >= 0) {

            String pathParams = uri.substring(semi + 1);

 

            // check if there is a url encoded session param.

            String param = SESSION_URL_PARAM;

            if (param != null && pathParams != null && pathParams.startsWith(param) && pathParams.length() > param.length() + 1) {

                id = pathParams.substring(param.length() + 1);

            }

        }

 

        if (id != null) {

         sessionIdFromUri = true;

        }

 

        return id;

}

}

 

之后就可以用这个包含到filter中

if (!(request instanceof ReplaceSessionRequestWrapper)) {

request = new ReplaceSessionRequestWrapper(request, response, getFilterConfig().getServletContext());

}

 

public class CachedStoreHttpSession extends CachedStoreSession implements HttpSession {

//~ Static fields/initializers =============================================

 

private static final long serialVersionUID = 5400909765496257075L;

//~ Instance fields ========================================================

 

private transient ServletContext context;

 

//~ Constructors ===========================================================

/**

* @param id

*/

public CachedStoreHttpSession(String id, ServletContext context) {

super(id);

this.context = context;

}

//~ Methods ================================================================

 

public void setServletContext(ServletContext context) {

this.context = context;

}

/* (non-Javadoc)

* @see javax.servlet.http.HttpSession#getServletContext()

*/

public ServletContext getServletContext() {

return context;

}

 

/* (non-Javadoc)

* @see javax.servlet.http.HttpSession#getSessionContext()

*/

@SuppressWarnings("deprecation")

public javax.servlet.http.HttpSessionContext getSessionContext() {

return null;

}

 

/* (non-Javadoc)

* @see javax.servlet.http.HttpSession#getValue(java.lang.String)

*/

public Object getValue(String name) {

return null;

}

 

/* (non-Javadoc)

* @see javax.servlet.http.HttpSession#getValueNames()

*/

public String[] getValueNames() {

return new String[0];

}

 

/* (non-Javadoc)

* @see javax.servlet.http.HttpSession#putValue(java.lang.String, java.lang.Object)

*/

public void putValue(String name, Object value) {

}

 

/* (non-Javadoc)

* @see javax.servlet.http.HttpSession#removeValue(java.lang.String)

*/

public void removeValue(String name) {

}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值