Search in sources :

Example 1 with DefaultSubjectDAO

use of org.apache.shiro.mgt.DefaultSubjectDAO in project neo4j by neo4j.

the class MultiRealmAuthManager method createSubjectDAO.

private SubjectDAO createSubjectDAO() {
    DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
    DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator();
    sessionStorageEvaluator.setSessionStorageEnabled(false);
    subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator);
    return subjectDAO;
}
Also used : DefaultSubjectDAO(org.apache.shiro.mgt.DefaultSubjectDAO) DefaultSessionStorageEvaluator(org.apache.shiro.mgt.DefaultSessionStorageEvaluator)

Example 2 with DefaultSubjectDAO

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

the class UserContextTest method runAs.

@Test
void runAs() {
    // Simulate what we do in the DefaultSecurityManagerProvider
    DefaultSecurityManager sm = new DefaultSecurityManager();
    SecurityUtils.setSecurityManager(sm);
    final DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
    final DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator() {

        @Override
        public boolean isSessionStorageEnabled(Subject subject) {
            // save to session if we already have a session. do not create on just for saving the subject
            return subject.getSession(false) != null;
        }
    };
    sessionStorageEvaluator.setSessionStorageEnabled(false);
    subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator);
    sm.setSubjectDAO(subjectDAO);
    final User user = new UserImpl(mock(PasswordAlgorithmFactory.class), mock(Permissions.class), ImmutableMap.of());
    when(userService.load(anyString())).thenReturn(user);
    when(userService.loadById(anyString())).thenReturn(user);
    final String USERID = "123456";
    UserContext.<Void>runAs(USERID, () -> {
        final UserContext userContext = new UserContext.Factory(userService).create();
        assertThat(userContext.getUserId()).isEqualTo(USERID);
        assertThat(userContext.getUser()).isEqualTo(user);
        return null;
    });
}
Also used : DefaultSubjectDAO(org.apache.shiro.mgt.DefaultSubjectDAO) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) User(org.graylog2.plugin.database.users.User) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) DefaultSessionStorageEvaluator(org.apache.shiro.mgt.DefaultSessionStorageEvaluator) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultSessionStorageEvaluator (org.apache.shiro.mgt.DefaultSessionStorageEvaluator)2 DefaultSubjectDAO (org.apache.shiro.mgt.DefaultSubjectDAO)2 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)1 Subject (org.apache.shiro.subject.Subject)1 User (org.graylog2.plugin.database.users.User)1 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)1 Permissions (org.graylog2.shared.security.Permissions)1 UserImpl (org.graylog2.users.UserImpl)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1