Search in sources :

Example 6 with SessionListener

use of org.apache.shiro.session.SessionListener in project tesla by linking12.

the class AuthzConfig method sessionManager.

@Bean
public SessionManager sessionManager() {
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    Collection<SessionListener> listeners = new ArrayList<SessionListener>();
    listeners.add(new TeslaSessionListener());
    sessionManager.setSessionListeners(listeners);
    sessionManager.setSessionDAO(sessionDAO());
    return sessionManager;
}
Also used : ArrayList(java.util.ArrayList) DefaultWebSessionManager(org.apache.shiro.web.session.mgt.DefaultWebSessionManager) SessionListener(org.apache.shiro.session.SessionListener) InitializingBean(org.springframework.beans.factory.InitializingBean) ShiroFilterFactoryBean(org.apache.shiro.spring.web.ShiroFilterFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 7 with SessionListener

use of org.apache.shiro.session.SessionListener in project wechat by dllwh.

the class ShiroConfig method sessionManager.

/**
 * @方法描述 : session管理器
 * @return
 */
// @Bean("sessionManager")
public DefaultWebSessionManager sessionManager() {
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    // session的失效时长,默认是30 分钟((1800000)),该值以毫秒为时间单位
    sessionManager.setGlobalSessionTimeout(TIMEOUT);
    // 是否在会话过期后会调用SessionDAO的delete方法删除会话,默认true
    sessionManager.setDeleteInvalidSessions(true);
    // 间隔多少时间检查,不配置是60分钟
    sessionManager.setSessionValidationScheduler(sessionValidationScheduler());
    // 是否开启session验证检测
    sessionManager.setSessionValidationSchedulerEnabled(true);
    // 定时清理失效session , 清理用户直接关闭浏览器造成的孤立会话 :默认每小时检测一次
    sessionManager.setSessionValidationInterval(INTERVAL);
    // 是否启用/禁用,默认是启用的;如果禁用后将不会设置Session Id Cookie,即默认使用了Servlet容器的JSESSIONID
    sessionManager.setSessionIdCookieEnabled(true);
    sessionManager.setSessionIdCookie(sessionIdCookie());
    // sessionManager.setSessionIdUrlRewritingEnabled(false);
    // sessionManager.setSessionDAO(sessionDAO);
    // session 监听
    Collection<SessionListener> sessionListeners = Lists.newArrayList();
    sessionListeners.add(new CustomSessionListener());
    sessionManager.setSessionListeners(sessionListeners);
    return sessionManager;
}
Also used : DefaultWebSessionManager(org.apache.shiro.web.session.mgt.DefaultWebSessionManager) SessionListener(org.apache.shiro.session.SessionListener) CustomSessionListener(com.cdeledu.core.shiro.listener.CustomSessionListener) CustomSessionListener(com.cdeledu.core.shiro.listener.CustomSessionListener)

Example 8 with SessionListener

use of org.apache.shiro.session.SessionListener in project shiro by apache.

the class DefaultSessionManagerTest method testSessionListenerExpiredNotification.

@Test
public void testSessionListenerExpiredNotification() {
    final boolean[] expired = new boolean[1];
    SessionListener listener = new SessionListenerAdapter() {

        public void onExpiration(Session session) {
            expired[0] = true;
        }
    };
    sm.getSessionListeners().add(listener);
    sm.setGlobalSessionTimeout(100);
    Session session = sm.start(null);
    sleep(150);
    try {
        sm.checkValid(new DefaultSessionKey(session.getId()));
        fail("check should have thrown an exception.");
    } catch (InvalidSessionException expected) {
    // do nothing - expected.
    }
    assertTrue(expired[0]);
}
Also used : InvalidSessionException(org.apache.shiro.session.InvalidSessionException) SessionListenerAdapter(org.apache.shiro.session.SessionListenerAdapter) SessionListener(org.apache.shiro.session.SessionListener) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Aggregations

SessionListener (org.apache.shiro.session.SessionListener)8 Session (org.apache.shiro.session.Session)6 Test (org.junit.Test)6 SessionListenerAdapter (org.apache.shiro.session.SessionListenerAdapter)5 ArrayList (java.util.ArrayList)2 DefaultWebSessionManager (org.apache.shiro.web.session.mgt.DefaultWebSessionManager)2 CustomSessionListener (com.cdeledu.core.shiro.listener.CustomSessionListener)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InvalidSessionException (org.apache.shiro.session.InvalidSessionException)1 ShiroFilterFactoryBean (org.apache.shiro.spring.web.ShiroFilterFactoryBean)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 Bean (org.springframework.context.annotation.Bean)1