Search in sources :

Example 26 with Session

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

the class CachingSessionDAO method createActiveSessionsCache.

/**
 * Creates a cache instance used to store active sessions.  Creation is done by first
 * {@link #getCacheManager() acquiring} the {@code CacheManager}.  If the cache manager is not null, the
 * cache returned is that resulting from the following call:
 * <pre>       String name = {@link #getActiveSessionsCacheName() getActiveSessionsCacheName()};
 * cacheManager.getCache(name);</pre>
 *
 * @return a cache instance used to store active sessions, or {@code null} if the {@code CacheManager} has
 *         not been set.
 */
protected Cache<Serializable, Session> createActiveSessionsCache() {
    Cache<Serializable, Session> cache = null;
    CacheManager mgr = getCacheManager();
    if (mgr != null) {
        String name = getActiveSessionsCacheName();
        cache = mgr.getCache(name);
    }
    return cache;
}
Also used : Serializable(java.io.Serializable) CacheManager(org.apache.shiro.cache.CacheManager) ValidatingSession(org.apache.shiro.session.mgt.ValidatingSession) Session(org.apache.shiro.session.Session)

Example 27 with Session

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

the class CachingSessionDAO method uncache.

/**
 * Removes the specified Session from the cache.
 *
 * @param session the session to remove from the cache.
 */
protected void uncache(Session session) {
    if (session == null) {
        return;
    }
    Serializable id = session.getId();
    if (id == null) {
        return;
    }
    Cache<Serializable, Session> cache = getActiveSessionsCacheLazy();
    if (cache != null) {
        cache.remove(id);
    }
}
Also used : Serializable(java.io.Serializable) ValidatingSession(org.apache.shiro.session.mgt.ValidatingSession) Session(org.apache.shiro.session.Session)

Example 28 with Session

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

the class DefaultSecurityManagerTest method testAutoCreateSessionAfterInvalidation.

/**
 * Test that validates functionality for issue
 * <a href="https://issues.apache.org/jira/browse/JSEC-46">JSEC-46</a>
 */
@Test
public void testAutoCreateSessionAfterInvalidation() {
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    Serializable origSessionId = session.getId();
    String key = "foo";
    String value1 = "bar";
    session.setAttribute(key, value1);
    assertEquals(value1, session.getAttribute(key));
    // now test auto creation:
    session.setTimeout(50);
    try {
        Thread.sleep(150);
    } catch (InterruptedException e) {
    // ignored
    }
    try {
        session.setTimeout(AbstractValidatingSessionManager.DEFAULT_GLOBAL_SESSION_TIMEOUT);
        fail("Session should have expired.");
    } catch (ExpiredSessionException expected) {
    }
}
Also used : Serializable(java.io.Serializable) ExpiredSessionException(org.apache.shiro.session.ExpiredSessionException) Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 29 with Session

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

the class DefaultSecurityManagerTest method testSubjectReuseAfterLogout.

/**
 * Test that validates functionality for issue
 * <a href="https://issues.apache.org/jira/browse/JSEC-22">JSEC-22</a>
 */
@Test
public void testSubjectReuseAfterLogout() {
    Subject subject = SecurityUtils.getSubject();
    AuthenticationToken token = new UsernamePasswordToken("guest", "guest");
    subject.login(token);
    assertTrue(subject.isAuthenticated());
    assertTrue("guest".equals(subject.getPrincipal()));
    assertTrue(subject.hasRole("guest"));
    Session session = subject.getSession();
    Serializable firstSessionId = session.getId();
    session.setAttribute("key", "value");
    assertEquals(session.getAttribute("key"), "value");
    subject.logout();
    assertNull(subject.getSession(false));
    assertNull(subject.getPrincipal());
    assertNull(subject.getPrincipals());
    subject.login(new UsernamePasswordToken("lonestarr", "vespa"));
    assertTrue(subject.isAuthenticated());
    assertTrue("lonestarr".equals(subject.getPrincipal()));
    assertTrue(subject.hasRole("goodguy"));
    assertNotNull(subject.getSession());
    assertFalse(firstSessionId.equals(subject.getSession().getId()));
    subject.logout();
    assertNull(subject.getSession(false));
    assertNull(subject.getPrincipal());
    assertNull(subject.getPrincipals());
}
Also used : Serializable(java.io.Serializable) AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 30 with Session

use of org.apache.shiro.session.Session 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)

Aggregations

Session (org.apache.shiro.session.Session)93 Subject (org.apache.shiro.subject.Subject)34 Test (org.junit.Test)21 Serializable (java.io.Serializable)11 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)8 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 SecurityManager (org.apache.shiro.mgt.SecurityManager)5 SessionListener (org.apache.shiro.session.SessionListener)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 User (com.hfut.entity.User)4 Subject (ddf.security.Subject)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Date (java.util.Date)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AuthenticationException (org.apache.shiro.authc.AuthenticationException)4 InvalidSessionException (org.apache.shiro.session.InvalidSessionException)4 SessionListenerAdapter (org.apache.shiro.session.SessionListenerAdapter)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3