Search in sources :

Example 16 with Session

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

the class DefaultSampleManager method getValue.

/*--------------------------------------------
    |         C O N S T R U C T O R S           |
    ============================================*/
/*--------------------------------------------
    |  A C C E S S O R S / M O D I F I E R S    |
    ============================================*/
/*--------------------------------------------
    |               M E T H O D S               |
    ============================================*/
public String getValue() {
    String value = null;
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession(false);
    if (session != null) {
        value = (String) session.getAttribute(VALUE_KEY);
        if (log.isDebugEnabled()) {
            log.debug("retrieving session key [" + VALUE_KEY + "] with value [" + value + "] on session with id [" + session.getId() + "]");
        }
    }
    return value;
}
Also used : Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session)

Example 17 with Session

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

the class IndexController method referenceData.

protected Map<String, Object> referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
    Subject subject = SecurityUtils.getSubject();
    boolean hasRole1 = subject.hasRole("role1");
    boolean hasRole2 = subject.hasRole("role2");
    Map<String, Object> refData = new HashMap<String, Object>();
    refData.put("hasRole1", hasRole1);
    refData.put("hasRole2", hasRole2);
    Session session = subject.getSession();
    Map<Object, Object> sessionAttributes = new LinkedHashMap<Object, Object>();
    for (Object key : session.getAttributeKeys()) {
        sessionAttributes.put(key, session.getAttribute(key));
    }
    refData.put("sessionAttributes", sessionAttributes);
    refData.put("subjectSession", subject.getSession());
    return refData;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session) LinkedHashMap(java.util.LinkedHashMap)

Example 18 with Session

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

the class ShiroSessionScope method scope.

public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) {
    return new Provider<T>() {

        public T get() {
            Subject subject = ThreadContext.getSubject();
            if (subject == null) {
                throw new OutOfScopeException("There is no Shiro Session currently in scope.");
            }
            Session session = subject.getSession();
            T scoped = castSessionAttribute(session);
            if (scoped == null) {
                scoped = unscoped.get();
            }
            return scoped;
        }

        @SuppressWarnings({ "unchecked" })
        private T castSessionAttribute(Session session) {
            return (T) session.getAttribute(key);
        }

        @Override
        public String toString() {
            return unscoped.toString();
        }
    };
}
Also used : OutOfScopeException(com.google.inject.OutOfScopeException) Subject(org.apache.shiro.subject.Subject) Provider(com.google.inject.Provider) Session(org.apache.shiro.session.Session)

Example 19 with Session

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

the class AbstractNativeSessionManager method stop.

public void stop(SessionKey key) throws InvalidSessionException {
    Session session = lookupRequiredSession(key);
    try {
        if (log.isDebugEnabled()) {
            log.debug("Stopping session with id [" + session.getId() + "]");
        }
        session.stop();
        onStop(session, key);
        notifyStop(session);
    } finally {
        afterStopped(session);
    }
}
Also used : Session(org.apache.shiro.session.Session)

Example 20 with Session

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

the class AbstractNativeSessionManager method touch.

public void touch(SessionKey key) throws InvalidSessionException {
    Session s = lookupRequiredSession(key);
    s.touch();
    onChange(s);
}
Also used : Session(org.apache.shiro.session.Session)

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