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) {
}
}
分享到:
相关推荐
Memcached是一个高性能的分布式的内存对象缓存系统.它的缓存是一种分布式的,也就是可以允许不同主机上的多个用户同时访问这个缓存系统,这种方法不仅解决了共享内存只能是单机的弊端,同时也解决了数据库检索的压力...
MemcacheDemo是一个示例项目,主要用于演示和学习如何在应用程序中使用Memcached,这是一个高性能、分布式内存对象缓存系统。Memcached被广泛用于减轻数据库负载,通过存储经常访问的数据到内存中,加快了数据的读取...
### LNMP环境搭建详解 #### 一、LNMP架构简介 ...此架构充分利用了各组件的优点,提供了高性能、高稳定性的Web服务。 - **Linux**:作为操作系统,提供了稳定可靠的运行环境。 - **Nginx**:作为Web服务器,擅长处理...
Mecache(通常写作memcached)是一款高性能、分布式内存对象缓存系统,广泛应用于Web应用中以减轻数据库负载。它通过将数据存储在内存中,从而实现快速访问,提高网站性能。Memcached支持多种编程语言的客户端接口,...
SuperCache实现专利的数据块级别的缓存技术,除提高系统性能之外,还能独自进行 NT 文件级别缓存。 SuperCache使用获得专利的块级高速缓存技术来提升系统的性能,这种技术远胜于单纯使用NT文件级的高速缓存。...