Search in sources :

Example 36 with Session

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

the class DefaultSessionManagerTest method testSessionListenerStartNotification.

@Test
public void testSessionListenerStartNotification() {
    final boolean[] started = new boolean[1];
    SessionListener listener = new SessionListenerAdapter() {

        public void onStart(Session session) {
            started[0] = true;
        }
    };
    sm.getSessionListeners().add(listener);
    sm.start(null);
    assertTrue(started[0]);
}
Also used : SessionListenerAdapter(org.apache.shiro.session.SessionListenerAdapter) SessionListener(org.apache.shiro.session.SessionListener) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 37 with Session

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

the class DelegatingSubject method login.

public void login(AuthenticationToken token) throws AuthenticationException {
    clearRunAsIdentitiesInternal();
    Subject subject = securityManager.login(this, token);
    PrincipalCollection principals;
    String host = null;
    if (subject instanceof DelegatingSubject) {
        DelegatingSubject delegating = (DelegatingSubject) subject;
        // we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:
        principals = delegating.principals;
        host = delegating.host;
    } else {
        principals = subject.getPrincipals();
    }
    if (principals == null || principals.isEmpty()) {
        String msg = "Principals returned from securityManager.login( token ) returned a null or " + "empty value.  This value must be non null and populated with one or more elements.";
        throw new IllegalStateException(msg);
    }
    this.principals = principals;
    this.authenticated = true;
    if (token instanceof HostAuthenticationToken) {
        host = ((HostAuthenticationToken) token).getHost();
    }
    if (host != null) {
        this.host = host;
    }
    Session session = subject.getSession(false);
    if (session != null) {
        this.session = decorate(session);
    } else {
        this.session = null;
    }
}
Also used : HostAuthenticationToken(org.apache.shiro.authc.HostAuthenticationToken) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Subject(org.apache.shiro.subject.Subject) ProxiedSession(org.apache.shiro.session.ProxiedSession) Session(org.apache.shiro.session.Session)

Example 38 with Session

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

the class DelegatingSubject method pushIdentity.

private void pushIdentity(PrincipalCollection principals) throws NullPointerException {
    if (isEmpty(principals)) {
        String msg = "Specified Subject principals cannot be null or empty for 'run as' functionality.";
        throw new NullPointerException(msg);
    }
    List<PrincipalCollection> stack = getRunAsPrincipalsStack();
    if (stack == null) {
        stack = new CopyOnWriteArrayList<PrincipalCollection>();
    }
    stack.add(0, principals);
    Session session = getSession();
    session.setAttribute(RUN_AS_PRINCIPALS_SESSION_KEY, stack);
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) ProxiedSession(org.apache.shiro.session.ProxiedSession) Session(org.apache.shiro.session.Session)

Example 39 with Session

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

the class DelegatingSubject method getSession.

public Session getSession(boolean create) {
    if (log.isTraceEnabled()) {
        log.trace("attempting to get session; create = " + create + "; session is null = " + (this.session == null) + "; session has id = " + (this.session != null && session.getId() != null));
    }
    if (this.session == null && create) {
        // added in 1.2:
        if (!isSessionCreationEnabled()) {
            String msg = "Session creation has been disabled for the current subject.  This exception indicates " + "that there is either a programming error (using a session when it should never be " + "used) or that Shiro's configuration needs to be adjusted to allow Sessions to be created " + "for the current Subject.  See the " + DisabledSessionException.class.getName() + " JavaDoc " + "for more.";
            throw new DisabledSessionException(msg);
        }
        log.trace("Starting session for host {}", getHost());
        SessionContext sessionContext = createSessionContext();
        Session session = this.securityManager.start(sessionContext);
        this.session = decorate(session);
    }
    return this.session;
}
Also used : SessionContext(org.apache.shiro.session.mgt.SessionContext) DefaultSessionContext(org.apache.shiro.session.mgt.DefaultSessionContext) ProxiedSession(org.apache.shiro.session.ProxiedSession) Session(org.apache.shiro.session.Session)

Example 40 with Session

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

the class DefaultWebSecurityManagerTest method testSessionTimeout.

@Test
public void testSessionTimeout() {
    shiroSessionModeInit();
    long globalTimeout = 100;
    ((AbstractSessionManager) sm.getSessionManager()).setGlobalSessionTimeout(globalTimeout);
    HttpServletRequest mockRequest = createNiceMock(HttpServletRequest.class);
    HttpServletResponse mockResponse = createNiceMock(HttpServletResponse.class);
    expect(mockRequest.getCookies()).andReturn(null);
    expect(mockRequest.getContextPath()).andReturn("/");
    replay(mockRequest);
    Subject subject = newSubject(mockRequest, mockResponse);
    Session session = subject.getSession();
    assertEquals(session.getTimeout(), globalTimeout);
    session.setTimeout(125);
    assertEquals(session.getTimeout(), 125);
    sleep(200);
    try {
        session.getTimeout();
        fail("Session should have expired.");
    } catch (ExpiredSessionException expected) {
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ExpiredSessionException(org.apache.shiro.session.ExpiredSessionException) AbstractSessionManager(org.apache.shiro.session.mgt.AbstractSessionManager) WebSubject(org.apache.shiro.web.subject.WebSubject) Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session) ShiroHttpSession(org.apache.shiro.web.servlet.ShiroHttpSession) 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