redis session manager

redis session-manager

keywords: session manager; redis session manager; redis session;

1. config listener

set JedisPool and cacheEngine, new customer CacheListener extends CacheEngineLoadListener:

CacheEngineLoadListener.java
““java
public class CacheListener extends CacheEngineLoadListener {
private static Log log = LogFactory.getLog(CacheListener.class);
protected WebApplicationContext ctx;

@Override
public void contextInitialized(ServletContextEvent event) {
    ServletContext servletContext = event.getServletContext();
    this.ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);//get spring ctx
    JedisPool jedisPool = this.ctx.getBean(JedisPool.class);//get jedisPool with spring
    log.info(jedisPool);
    servletContext.setAttribute(RedisCacheEngine.JEDIS_POOL, jedisPool);
    super.contextInitialized(event);
}

@Override
public void contextDestroyed(ServletContextEvent event) {
    super.contextDestroyed(event);
}

}
““

2. config web.xml (CacheListener & CacheSessionFilter)

Parameter Required type Default value description
maxInactiveInterval false int 28800 session expired time in second
cookieDomain false string sub domain set cookie domain
tldEnable false boolean false set cookie use top level domain
cookieSessionIdName false string SESSIONID session id name in cookie
cookieContextPath false string / cookie context path
sessionCacheKeyPrefix false string session session key prefix in redis
sessionAttributeListeners false string null javax.servlet.http.HttpSessionListeners name, split with comma

config web.xml

<listener>
    <listener-class>com.ezhe.www.listener.CacheListener</listener-class>
</listener>

<!-- session manager filter -->
<filter>
    <filter-name>CacheSessionFilter</filter-name>
    <filter-class>com.gozap.session.servlet.filter.session.CacheSessionFilter</filter-class>
    <init-param>
        <description>session expired time in second</description>
        <param-name>maxInactiveInterval</param-name>
        <param-value>3600</param-value>
    </init-param>
    <init-param>
        <description>session prefix int redis</description>
        <param-name>sessionCacheKeyPrefix</param-name>
        <param-value>session-ezhe</param-value>
    </init-param>
    <init-param>
        <description>use top level domain for sessionId </description>
        <param-name>tldEnable</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CacheSessionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

.... other filters

如果想各個 子域名之間共享session,設置 tldEnable=true 即可

session-manager.png
這裏寫圖片描述

https://github.com/blackshadowwalker/session-manager

發佈了126 篇原創文章 · 獲贊 59 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章