Search in sources :

Example 6 with SimpleSession

use of org.apache.shiro.session.mgt.SimpleSession in project killbill by killbill.

the class TestJDBCSessionDao method testH2AndInvalidSessionId.

@Test(groups = "slow")
public void testH2AndInvalidSessionId() {
    final JDBCSessionDao jdbcSessionDao = new JDBCSessionDao(dbi);
    // We need to create some data to force H2 to build the query
    // (otherwise, the read path is optimized and the bug is not triggered)
    final SimpleSession session = createSession();
    jdbcSessionDao.doCreate(session);
    // Make sure this doesn't throw any exception on H2
    Assert.assertNull(jdbcSessionDao.doReadSession(UUID.randomUUID()));
}
Also used : SimpleSession(org.apache.shiro.session.mgt.SimpleSession) Test(org.testng.annotations.Test)

Example 7 with SimpleSession

use of org.apache.shiro.session.mgt.SimpleSession in project graylog2-server by Graylog2.

the class MongoDbSessionDAO method doUpdate.

@Override
protected void doUpdate(Session session) {
    final MongoDbSession dbSession = mongoDBSessionService.load(session.getId().toString());
    if (null == dbSession) {
        throw new RuntimeException("Couldn't load session <" + session.getId() + ">");
    }
    LOG.debug("Updating session {}", session);
    dbSession.setHost(session.getHost());
    dbSession.setTimeout(session.getTimeout());
    dbSession.setStartTimestamp(session.getStartTimestamp());
    dbSession.setLastAccessTime(session.getLastAccessTime());
    if (session instanceof SimpleSession) {
        final SimpleSession simpleSession = (SimpleSession) session;
        dbSession.setAttributes(simpleSession.getAttributes());
        dbSession.setExpired(simpleSession.isExpired());
    } else {
        throw new RuntimeException("Unsupported session type: " + session.getClass().getCanonicalName());
    }
    // Due to https://jira.mongodb.org/browse/SERVER-14322 upserts can fail under concurrency.
    // We need to retry the update, and stagger them a bit, so no all of the retries attempt it at the same time again.
    // Usually this should succeed the first time, though
    final Retryer<Object> retryer = RetryerBuilder.newBuilder().retryIfExceptionOfType(DuplicateKeyException.class).withWaitStrategy(WaitStrategies.randomWait(5, TimeUnit.MILLISECONDS)).withStopStrategy(StopStrategies.stopAfterAttempt(10)).build();
    try {
        retryer.call(() -> mongoDBSessionService.saveWithoutValidation(dbSession));
    } catch (ExecutionException e) {
        LOG.warn("Unexpected exception when saving session to MongoDB. Failed to update session.", e);
        throw new RuntimeException(e.getCause());
    } catch (RetryException e) {
        LOG.warn("Tried to update session 10 times, but still failed. This is likely because of https://jira.mongodb.org/browse/SERVER-14322", e);
        throw new RuntimeException(e.getCause());
    }
}
Also used : SimpleSession(org.apache.shiro.session.mgt.SimpleSession) ExecutionException(java.util.concurrent.ExecutionException) RetryException(com.github.rholder.retry.RetryException) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Example 8 with SimpleSession

use of org.apache.shiro.session.mgt.SimpleSession in project ddf by codice.

the class AbstractDownloadsStatusEventPublisherTest method addSecurity.

private void addSecurity() {
    org.apache.shiro.mgt.SecurityManager secManager = new DefaultSecurityManager();
    PrincipalCollection principals = new SimplePrincipalCollection(USER_ID, "testrealm");
    Subject subject = new Subject.Builder(secManager).principals(principals).session(new SimpleSession()).authenticated(true).buildSubject();
    ThreadContext.bind(secManager);
    ThreadContext.bind(subject);
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) Subject(org.apache.shiro.subject.Subject)

Example 9 with SimpleSession

use of org.apache.shiro.session.mgt.SimpleSession in project ddf by codice.

the class SubjectUtilsTest method testGetName.

@Test
public void testGetName() {
    org.apache.shiro.subject.Subject subject;
    org.apache.shiro.mgt.SecurityManager secManager = new DefaultSecurityManager();
    PrincipalCollection principals = new SimplePrincipalCollection(TEST_NAME, "testrealm");
    subject = new Subject.Builder(secManager).principals(principals).session(new SimpleSession()).authenticated(true).buildSubject();
    assertEquals(TEST_NAME, SubjectUtils.getName(subject));
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) Test(org.junit.Test)

Example 10 with SimpleSession

use of org.apache.shiro.session.mgt.SimpleSession in project ddf by codice.

the class SubjectUtilsTest method testGetDefaultName.

@Test
public void testGetDefaultName() {
    org.apache.shiro.subject.Subject subject;
    org.apache.shiro.mgt.SecurityManager secManager = new DefaultSecurityManager();
    PrincipalCollection principals = new SimplePrincipalCollection();
    subject = new Subject.Builder(secManager).principals(principals).session(new SimpleSession()).authenticated(true).buildSubject();
    assertEquals(DEFAULT_NAME, SubjectUtils.getName(subject, DEFAULT_NAME));
    assertEquals(DEFAULT_NAME, SubjectUtils.getName(null, DEFAULT_NAME));
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) Test(org.junit.Test)

Aggregations

SimpleSession (org.apache.shiro.session.mgt.SimpleSession)10 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)3 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)3 Test (org.testng.annotations.Test)3 Date (java.util.Date)2 Session (org.apache.shiro.session.Session)2 Test (org.junit.Test)2 RetryException (com.github.rholder.retry.RetryException)1 DuplicateKeyException (com.mongodb.DuplicateKeyException)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Subject (org.apache.shiro.subject.Subject)1