Example usage for org.apache.shiro.session.mgt SimpleSession setAttribute

List of usage examples for org.apache.shiro.session.mgt SimpleSession setAttribute

Introduction

In this page you can find the example usage for org.apache.shiro.session.mgt SimpleSession setAttribute.

Prototype

public void setAttribute(Object key, Object value) 

Source Link

Usage

From source file:com.funtl.framework.apache.shiro.session.JedisSessionDAO.java

License:Apache License

/**
 * ??//w  w  w.  j av  a2s. c o  m
 *
 * @param includeLeave  ??3?
 * @param principal     ???
 * @param filterSession ????
 * @return
 */
@Override
public Collection<Session> getActiveSessions(boolean includeLeave, Object principal, Session filterSession) {
    Set<Session> sessions = Sets.newHashSet();

    Jedis jedis = null;
    try {
        jedis = JedisUtils.getResource();
        Map<String, String> map = jedis.hgetAll(sessionKeyPrefix);
        for (Map.Entry<String, String> e : map.entrySet()) {
            if (StringUtils.isNotBlank(e.getKey()) && StringUtils.isNotBlank(e.getValue())) {

                String[] ss = StringUtils.split(e.getValue(), "|");
                if (ss != null && ss.length == 3) {// jedis.exists(sessionKeyPrefix + e.getKey())){
                    // Session session = (Session)JedisUtils.toObject(jedis.get(JedisUtils.getBytesKey(sessionKeyPrefix + e.getKey())));
                    SimpleSession session = new SimpleSession();
                    session.setId(e.getKey());
                    session.setAttribute("principalId", ss[0]);
                    session.setTimeout(Long.valueOf(ss[1]));
                    session.setLastAccessTime(new Date(Long.valueOf(ss[2])));
                    try {
                        // ?SESSION
                        session.validate();

                        boolean isActiveSession = false;
                        // ????3?
                        if (includeLeave || DateUtils.pastMinutes(session.getLastAccessTime()) <= 3) {
                            isActiveSession = true;
                        }
                        // ??
                        if (principal != null) {
                            PrincipalCollection pc = (PrincipalCollection) session
                                    .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
                            if (principal.toString().equals(
                                    pc != null ? pc.getPrimaryPrincipal().toString() : StringUtils.EMPTY)) {
                                isActiveSession = true;
                            }
                        }
                        // SESSION
                        if (filterSession != null && filterSession.getId().equals(session.getId())) {
                            isActiveSession = false;
                        }
                        if (isActiveSession) {
                            sessions.add(session);
                        }

                    }
                    // SESSION?
                    catch (Exception e2) {
                        jedis.hdel(sessionKeyPrefix, e.getKey());
                    }
                }
                // SESSION??
                else {
                    jedis.hdel(sessionKeyPrefix, e.getKey());
                }
            }
            // SESSIONValue
            else if (StringUtils.isNotBlank(e.getKey())) {
                jedis.hdel(sessionKeyPrefix, e.getKey());
            }
        }
        logger.info("getActiveSessions size: {} ", sessions.size());
    } catch (Exception e) {
        logger.error("getActiveSessions", e);
    } finally {
        JedisUtils.returnResource(jedis);
    }
    return sessions;
}

From source file:com.ikanow.aleph2.security.db.SessionDb.java

License:Apache License

protected Object deserialize(JsonNode sessionOb) {
    SimpleSession s = null;
    try {//w  w w  .  jav  a2  s.c o m
        if (sessionOb != null) {
            s = new SimpleSession();
            s.setId(sessionOb.get("_id").asText());
            s.setLastAccessTime(new Date(sessionOb.get("last_access_time").asLong()));
            s.setStartTimestamp(new Date(sessionOb.get("start_time_stamp").asLong()));
            s.setTimeout(sessionOb.get("timeout").asLong());
            s.setHost(sessionOb.get("host").asText());
            JsonNode attributesOb = sessionOb.get("attributes");
            for (Iterator<Entry<String, JsonNode>> it = attributesOb.fields(); it.hasNext();) {
                Entry<String, JsonNode> e = it.next();
                s.setAttribute(deescapeMongoCharacters(e.getKey()),
                        SerializableUtils.deserialize(e.getValue().asText()));
            }
        }
    } catch (Exception e) {
        logger.error("Caught Exception deserializing :" + sessionOb, e);
    }
    return s;
}

From source file:net.eggcanfly.spring.shiro.support.RedisSessionDao.java

License:Apache License

protected Session deserializeSession(Serializable sessionId) {

    BoundHashOperations<String, String, Object> hashOperations = redisTemplate
            .boundHashOps(SESSION_KEY + sessionId);

    SimpleSession session = new SimpleSession();

    try {/* w  w  w  .ja va2 s . c  om*/

        session.setHost((String) hashOperations.get("host"));
        session.setId(sessionId);
        session.setLastAccessTime(
                hashOperations.get("lastAccessTime") == null ? new Date(System.currentTimeMillis())
                        : (Date) hashOperations.get("lastAccessTime"));
        session.setStartTimestamp((Date) hashOperations.get("startTimestamp"));
        session.setStopTimestamp((Date) hashOperations.get("stopTimestamp"));
        session.setTimeout((long) hashOperations.get("timeout"));
        Set<String> hashKeys = hashOperations.keys();
        for (Iterator<String> iterator = hashKeys.iterator(); iterator.hasNext();) {
            String hashKey = iterator.next();
            session.setAttribute(hashKey, hashOperations.get(hashKey));
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);

    }

    logger.debug("read session " + sessionId + ", session is " + session);

    return session.isValid() ? session : null;
}

From source file:org.killbill.billing.util.security.shiro.dao.TestJDBCSessionDao.java

License:Apache License

private SimpleSession createSession() {
    final SimpleSession simpleSession = new SimpleSession();
    simpleSession.setStartTimestamp(new Date(System.currentTimeMillis() - 5000));
    simpleSession.setLastAccessTime(new Date(System.currentTimeMillis()));
    simpleSession.setTimeout(493934L);/* w  w w . j  a  va2  s.com*/
    simpleSession.setHost(UUID.randomUUID().toString());
    simpleSession.setAttribute(UUID.randomUUID().toString(), Short.MIN_VALUE);
    simpleSession.setAttribute(UUID.randomUUID().toString(), Integer.MIN_VALUE);
    simpleSession.setAttribute(UUID.randomUUID().toString(), Long.MIN_VALUE);
    simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    // Test with Serializable objects
    simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID());
    simpleSession.setAttribute(UUID.randomUUID().toString(), new Date(1242));
    return simpleSession;
}

From source file:org.killbill.billing.util.security.shiro.dao.TestSessionModelDao.java

License:Apache License

@Test(groups = "fast")
public void testRoundTrip() throws Exception {
    final SimpleSession simpleSession = new SimpleSession();
    simpleSession.setStartTimestamp(new Date(System.currentTimeMillis() - 5000));
    simpleSession.setLastAccessTime(new Date(System.currentTimeMillis()));
    simpleSession.setTimeout(493934L);/*from  w w w. j  av  a  2  s.co m*/
    simpleSession.setHost(UUID.randomUUID().toString());
    simpleSession.setAttribute(UUID.randomUUID(), Short.MIN_VALUE);
    simpleSession.setAttribute(UUID.randomUUID(), Integer.MIN_VALUE);
    simpleSession.setAttribute(UUID.randomUUID(), Long.MIN_VALUE);
    simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    // Test with Serializable objects
    simpleSession.setAttribute(UUID.randomUUID().toString(), UUID.randomUUID());
    simpleSession.setAttribute(UUID.randomUUID().toString(), new Date(1242));

    final SessionModelDao sessionModelDao = new SessionModelDao(simpleSession);
    Assert.assertEquals(sessionModelDao.getTimeout(), simpleSession.getTimeout());
    Assert.assertEquals(sessionModelDao.getHost(), simpleSession.getHost());
    Assert.assertTrue(sessionModelDao.getSessionData().length > 0);

    final Session retrievedSession = sessionModelDao.toSimpleSession();
    Assert.assertEquals(retrievedSession, simpleSession);
}

From source file:uk.ac.ox.it.ords.security.services.impl.hibernate.SessionStorageServiceImplTest.java

License:Apache License

@Test
public void sessionLifecycleTest() throws Exception {

    SimpleSession session = new SimpleSession();
    session.setAttribute("test", "test");
    session.setId("1");

    SessionStorageService.Factory.getInstance().createSession("1", session);

    SimpleSession result = (SimpleSession) SessionStorageService.Factory.getInstance().readSession("1");
    assertEquals("test", result.getAttribute("test"));
    SessionStorageService.Factory.getInstance().deleteSession("1");
    assertNull(SessionStorageService.Factory.getInstance().readSession("1"));

}

From source file:uk.ac.ox.it.ords.security.services.impl.hibernate.SessionStorageServiceImplTest.java

License:Apache License

@Test
public void sessionLifecycleUpdateTest() throws Exception {

    SimpleSession session = new SimpleSession();
    session.setAttribute("test", "test");
    session.setId("1");

    SessionStorageService.Factory.getInstance().createSession("1", session);

    session.setAttribute("new", "newvalue");
    session.setAttribute("test", "newvalue");
    SessionStorageService.Factory.getInstance().updateSession("1", session);

    SimpleSession result = (SimpleSession) SessionStorageService.Factory.getInstance().readSession("1");
    assertEquals("newvalue", result.getAttribute("new"));
    assertEquals("newvalue", result.getAttribute("test"));

    SessionStorageService.Factory.getInstance().deleteSession("1");
    assertNull(SessionStorageService.Factory.getInstance().readSession("1"));
}