Search in sources :

Example 1 with SessionListener

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

the class AbstractValidatingSessionManagerTest method testNoMemoryLeakOnInvalidSessions.

/**
 * Tests that no memory leak exists on invalid sessions: expired or stopped
 * Verifies <a href="https://issues.apache.org/jira/browse/SHIRO-399">SHIRO-399</a>.
 */
@Test
public void testNoMemoryLeakOnInvalidSessions() throws Exception {
    SessionListener sessionListener = new SessionListener() {

        public void onStart(Session session) {
            session.setAttribute("I love", "Romania");
        }

        public void onStop(Session session) {
            tryToCleanSession(session);
        }

        public void onExpiration(Session session) {
            tryToCleanSession(session);
        }

        private void tryToCleanSession(Session session) {
            Collection<Object> keys = session.getAttributeKeys();
            for (Object key : keys) {
                session.removeAttribute(key);
            }
        }
    };
    DefaultSessionManager sessionManager = new DefaultSessionManager();
    sessionManager.setSessionListeners(Arrays.asList(sessionListener));
    Session session = sessionManager.start(null);
    assertEquals(1, sessionManager.getActiveSessions().size());
    session.setTimeout(0L);
    // last access timestamp needs to be older than the current timestamp when validating, so ensure a delay:
    Thread.sleep(1);
    sessionManager.validateSessions();
    assertEquals(0, sessionManager.getActiveSessions().size());
}
Also used : SessionListener(org.apache.shiro.session.SessionListener) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 2 with SessionListener

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

the class AbstractValidatingSessionManagerTest method testValidateSessions.

/**
 * Tests that both SessionListeners are called and that invalid sessions are deleted by default.
 * Verifies <a href="https://issues.apache.org/jira/browse/SHIRO-199">SHIRO-199</a>.
 */
@Test
public void testValidateSessions() {
    final SimpleSession validSession = new SimpleSession();
    validSession.setId(1);
    final SimpleSession invalidSession = new SimpleSession();
    // set to a time in the past:
    Calendar cal = Calendar.getInstance();
    Long expiredTimeout = AbstractSessionManager.DEFAULT_GLOBAL_SESSION_TIMEOUT + 1;
    cal.add(Calendar.MILLISECOND, -(expiredTimeout.intValue()));
    Date past = cal.getTime();
    invalidSession.setStartTimestamp(past);
    invalidSession.setLastAccessTime(past);
    invalidSession.setId(2);
    final AtomicInteger expirationCount = new AtomicInteger();
    SessionListener sessionListener = new SessionListenerAdapter() {

        @Override
        public void onExpiration(Session session) {
            expirationCount.incrementAndGet();
        }
    };
    AbstractValidatingSessionManager sessionManager = new AbstractValidatingSessionManager() {

        @Override
        protected Session retrieveSession(SessionKey key) throws UnknownSessionException {
            throw new UnsupportedOperationException("Should not be called in this test.");
        }

        @Override
        protected Session doCreateSession(SessionContext initData) throws AuthorizationException {
            throw new UnsupportedOperationException("Should not be called in this test.");
        }

        @Override
        protected Collection<Session> getActiveSessions() {
            Collection<Session> sessions = new ArrayList<Session>(2);
            sessions.add(validSession);
            sessions.add(invalidSession);
            return sessions;
        }
    };
    sessionManager.setSessionListeners(Arrays.asList(sessionListener));
    sessionManager.validateSessions();
    assertEquals(1, expirationCount.intValue());
}
Also used : SessionListenerAdapter(org.apache.shiro.session.SessionListenerAdapter) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Date(java.util.Date) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SessionListener(org.apache.shiro.session.SessionListener) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 3 with SessionListener

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

the class DefaultSessionManagerTest method testSessionListenerStopNotification.

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

        public void onStop(Session session) {
            stopped[0] = true;
        }
    };
    sm.getSessionListeners().add(listener);
    Session session = sm.start(null);
    sm.stop(new DefaultSessionKey(session.getId()));
    assertTrue(stopped[0]);
}
Also used : SessionListenerAdapter(org.apache.shiro.session.SessionListenerAdapter) SessionListener(org.apache.shiro.session.SessionListener) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 4 with SessionListener

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

the class DefaultSessionManagerTest method testSessionListenerStopNotificationWithReadAttribute.

// asserts fix for SHIRO-388:
// Ensures that a session attribute can be accessed in the listener without
// causing a stack overflow exception.
@Test
public void testSessionListenerStopNotificationWithReadAttribute() {
    final boolean[] stopped = new boolean[1];
    final String[] value = new String[1];
    SessionListener listener = new SessionListenerAdapter() {

        public void onStop(Session session) {
            stopped[0] = true;
            value[0] = (String) session.getAttribute("foo");
        }
    };
    sm.getSessionListeners().add(listener);
    Session session = sm.start(null);
    session.setAttribute("foo", "bar");
    sm.stop(new DefaultSessionKey(session.getId()));
    assertTrue(stopped[0]);
    assertEquals("bar", value[0]);
}
Also used : SessionListenerAdapter(org.apache.shiro.session.SessionListenerAdapter) SessionListener(org.apache.shiro.session.SessionListener) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 5 with SessionListener

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

the class DefaultSessionManagerTest method testSessionListenerStartNotification.

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

        public void onStart(Session session) {
            started[0] = true;
        }
    };
    sm.getSessionListeners().add(listener);
    sm.start(null);
    assertTrue(started[0]);
}
Also used : 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