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

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

Introduction

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

Prototype

public Object getAttribute(Object key) 

Source Link

Usage

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

License:Apache License

/**
 * ??//ww  w . ja  v a2  s. co 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.wms.studio.controller.admin.UserAdminManagerController.java

License:Apache License

@RequestMapping("admin/userManager/userOnlineStore")
public void getOnlineUsers(Model model) {
    Iterator<Session> sessions = sessionDao.getActiveSessions().iterator();
    ArrayList<OnlineUser> ous = new ArrayList<OnlineUser>();
    while (sessions.hasNext()) {
        OnlineUser ou = new OnlineUser();
        SimpleSession session = (SimpleSession) sessions.next();
        ou.setHost(session.getHost());/*w  ww. j  a  v  a2  s . co m*/
        ou.setId(session.getId().toString());
        ou.setLastAccessTime(session.getLastAccessTime());
        ou.setStartTime(session.getStartTimestamp());
        PrincipalCollection principal = (PrincipalCollection) session
                .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        if (principal != null) {
            ShiroUser su = (ShiroUser) principal.getPrimaryPrincipal();
            ou.setUserid(su.loginName);
            ou.setUsername(su.name);
            ou.setLogin(true);
        }
        ous.add(ou);
    }
    model.addAttribute("users", ous);
    model.addAttribute("total", ous.size());
}

From source file:org.cast.go.message.web.WebSocketController.java

License:Apache License

/**
 * session?/*w ww. j  ava 2s  .  co  m*/
 * @return
 */
@MessageMapping("/online")
public void online(User user) {
    Cache cache = ehCacheManager.getCache(Common.ACTIVE_SESSIONS_CACHE_NAME);
    Collection<SimpleSession> sessions = cache.values();
    for (SimpleSession s : sessions) {
        MyRealm.ShiroUser shiroUser = (MyRealm.ShiroUser) s.getAttribute("shiroUser");
        if (user.getUsername().equals(shiroUser.getUsername())) {
            s.setLastAccessTime(new Date());
        }
    }

}

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"));
}