Example usage for org.apache.shiro.session.mgt DefaultSessionKey DefaultSessionKey

List of usage examples for org.apache.shiro.session.mgt DefaultSessionKey DefaultSessionKey

Introduction

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

Prototype

public DefaultSessionKey(Serializable sessionId) 

Source Link

Usage

From source file:com.caricah.iotracah.bootstrap.security.IOTSecurityManager.java

License:Apache License

protected IOTClient resolveContextSession(SubjectContext context) throws InvalidSessionException {

    Serializable sessionId = context.getSessionId();

    if (sessionId != null) {
        SessionKey key = new DefaultSessionKey(sessionId);
        return (IOTClient) getSession(key);
    }/*  w  w w.  j av a2s  .  c o m*/

    return null;

}

From source file:com.caricah.iotracah.bootstrap.security.realm.state.IOTClient.java

License:Apache License

public void stop() {
    if (getStopTimestamp() == null) {
        setStopTimestamp(Timestamp.from(Instant.now()));
    }//from   w  w w .j a  v a2  s.  co m

    setIsActive(false);

    touch();

    getSessionManager().stop(new DefaultSessionKey(getId()));

}

From source file:com.caricah.iotracah.core.modules.Datastore.java

License:Apache License

public Observable<IOTClient> getSession(IotClientKey sessionId) {

    return Observable.create(observer -> {

        try {/*from  www . ja v a 2  s  . com*/

            DefaultSessionKey sessionKey = new DefaultSessionKey(sessionId);

            IOTClient session = (IOTClient) SecurityUtils.getSecurityManager().getSession(sessionKey);
            if (Objects.isNull(session)) {
                observer.onError(new DoesNotExistException("No session with the id exists."));
            } else {
                observer.onNext(session);
                observer.onCompleted();
            }

        } catch (Exception e) {
            observer.onError(e);
        }

    });
}

From source file:com.wms.studio.filter.KickoutSessionControlFilter.java

License:Apache License

@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
    Subject subject = getSubject(request, response);
    if (!subject.isAuthenticated() && !subject.isRemembered()) {
        // ??/*from  w  w w  .j av a 2 s  .  c o m*/
        return true;
    }
    Session session = subject.getSession();

    ShiroUser user = (ShiroUser) subject.getPrincipal();
    Serializable sessionId = session.getId();

    Deque<Serializable> deque = cache.get(user.loginName);
    if (deque == null) {
        deque = new ArrayDeque<Serializable>();
        cache.put(user.loginName, deque);
    }

    // sessionId
    if (!deque.contains(sessionId) && session.getAttribute("kickout") == null) {
        deque.push(sessionId);
        cache.put(user.loginName, deque);
    }

    // sessionId?
    if (deque.size() > maxSession) {
        // 
        Iterator<Serializable> loginUsers = deque.iterator();
        while (loginUsers.hasNext()) {
            Serializable ss = loginUsers.next();
            try {
                sessionManager.getSession(new DefaultSessionKey(ss));
            } catch (SessionException e) {
                deque.remove(ss);
                cache.put(user.loginName, deque);
            }
        }
        // ??
        while (deque.size() > maxSession) {
            Serializable kickoutSessionId = null;
            if (kickoutAfter) { // ?
                kickoutSessionId = deque.removeFirst();
            } else { // ??
                kickoutSessionId = deque.removeLast();
            }
            try {
                Session kickoutSession = sessionManager.getSession(new DefaultSessionKey(kickoutSessionId));
                if (kickoutSession != null) {
                    // ?kickout
                    kickoutSession.setAttribute(Constant.USER_STATUS_KEY, true);
                }
            } catch (Exception e) {// ignore exception
            }
        }
        cache.put(user.loginName, deque);
    }

    // ????
    if (session.getAttribute(Constant.USER_STATUS_KEY) != null) {
        // ?
        try {
            subject.logout();
        } catch (Exception e) { // ignore
        }
        String loginUrl = getLoginUrl() + (getLoginUrl().contains("?") ? "&" : "?") + KICKOUT_SIGN;
        WebUtils.issueRedirect(request, response, loginUrl);
        return false;
    }
    return true;
}

From source file:com.zht.common.shiro.filter.MaxSessionControlEhcaheFilter.java

License:Apache License

@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
    Subject subject = getSubject(request, response);
    if (!subject.isAuthenticated() && !subject.isRemembered()) {
        // ??//from  ww  w .j  ava 2 s.  c o  m
        return true;
    }
    Session session = subject.getSession();
    String username = (String) subject.getPrincipal();
    Serializable sessionId = session.getId();
    //TODO ?
    Deque<Serializable> deque = cache.get(username);
    if (deque == null) {
        deque = new LinkedList<Serializable>();
        cache.put(username, deque);
    }
    // sessionId  
    if (!deque.contains(sessionId) && session.getAttribute("kickout") == null) {
        deque.push(sessionId);
    }
    while (deque.size() > maxSession) {
        Serializable kickoutSessionId = null;
        if (kickoutAfter) { //?
            kickoutSessionId = deque.removeFirst();
        } else { //??
            kickoutSessionId = deque.removeLast();
        }
        try {
            Session kickoutSession = sessionManager.getSession(new DefaultSessionKey(kickoutSessionId));
            if (kickoutSession != null) {
                //? kickout 
                kickoutSession.setAttribute("kickout", true);
            }
        } catch (Exception e) {//ignore exception
        }
    }
    //  ????
    if (session.getAttribute("kickout") != null) {
        //?
        try {
            subject.logout();
        } catch (Exception e) { //ignore
        }
        saveRequest(request);
        WebUtils.issueRedirect(request, response, kickoutUrl);
        return false;
    }
    return true;
}

From source file:org.apache.activemq.shiro.session.mgt.DisabledSessionManagerTest.java

License:Apache License

@Test
public void testGetSession() {
    assertNull(mgr.getSession(null));
    assertNull(mgr.getSession(new DefaultSessionKey("foo")));
}

From source file:org.lazulite.boot.autoconfigure.osaam.shiro.web.session.mgt.OnlineWebSessionManager.java

License:Apache License

/**
 * ?session? session/*from  ww  w  .  j  av  a2s . c  o m*/
 */
@Override
public void validateSessions() {
    if (log.isInfoEnabled()) {
        log.info("invalidation sessions...");
    }

    int invalidCount = 0;

    int timeout = (int) getGlobalSessionTimeout();
    Date expiredDate = DateUtils.addMilliseconds(new Date(), 0 - timeout);
    PageRequest pageRequest = new PageRequest(0, 100);
    Page<UserOnline> page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest);

    //??
    while (page.hasContent()) {
        List<Long> needOfflineIdList = Lists.newArrayList();
        for (UserOnline userOnline : page.getContent()) {
            try {
                SessionKey key = new DefaultSessionKey(userOnline.getId());
                Session session = retrieveSession(key);
                //cache db
                if (session != null) {
                    session.setAttribute(ShiroConstants.ONLY_CLEAR_CACHE, true);
                }
                validate(session, key);
            } catch (InvalidSessionException e) {
                if (log.isDebugEnabled()) {
                    boolean expired = (e instanceof ExpiredSessionException);
                    String msg = "Invalidated session with id [" + userOnline.getId() + "]"
                            + (expired ? " (expired)" : " (stopped)");
                    log.debug(msg);
                }
                invalidCount++;
                needOfflineIdList.add(userOnline.getId());
            }

        }
        if (needOfflineIdList.size() > 0) {
            try {
                userOnlineService.batchOffline(needOfflineIdList);
            } catch (Exception e) {
                log.error("batch delete db session error.", e);
            }
        }
        pageRequest = new PageRequest(0, pageRequest.getPageSize());
        page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest);
    }

    if (log.isInfoEnabled()) {
        String msg = "Finished invalidation session.";
        if (invalidCount > 0) {
            msg += "  [" + invalidCount + "] sessions were stopped.";
        } else {
            msg += "  No sessions were stopped.";
        }
        log.info(msg);
    }

}

From source file:org.obiba.agate.web.rest.security.SessionResource.java

License:Open Source License

@Nullable
Session getSession(String sessionId) {
    if (sessionId != null) {
        SessionKey key = new DefaultSessionKey(sessionId);
        try {/*ww  w.j  a  v a  2s .  com*/
            return SecurityUtils.getSecurityManager().getSession(key);
        } catch (SessionException e) {
            // Means that the session does not exist or has expired.
        }
    }
    return null;
}

From source file:org.obiba.opal.core.runtime.security.AbstractHttpAuthenticatingRealm.java

License:Open Source License

@Nullable
protected Session getSession(String sessionId) {
    if (sessionId != null) {
        SessionManager manager = getSessionManager();
        if (manager != null) {
            SessionKey key = new DefaultSessionKey(sessionId);
            try {
                return manager.getSession(key);
            } catch (SessionException e) {
                // Means that the session does not exist or has expired.
            }//  w  w  w  .j a  va2 s. co m
        }
    }
    return null;
}

From source file:org.obiba.opal.server.httpd.security.AbstractSecurityComponent.java

License:Open Source License

protected boolean isValidSessionId(String sessionId) {
    if (sessionId != null) {
        SessionKey key = new DefaultSessionKey(sessionId);
        try {/*w  w w . j  a  v  a  2  s .  c  o  m*/
            return securityManager.getSessionManager().getSession(key) != null;
        } catch (SessionException e) {
            // Means that the session does not exist or has expired.
        }
    }
    return false;
}