Search in sources :

Example 26 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class DelegatingSubjectTest method testExecuteRunnable.

@Test
public void testExecuteRunnable() {
    String username = "jsmith";
    SecurityManager securityManager = createNiceMock(SecurityManager.class);
    PrincipalCollection identity = new SimplePrincipalCollection(username, "testRealm");
    final Subject sourceSubject = new DelegatingSubject(identity, true, null, null, securityManager);
    assertNull(ThreadContext.getSubject());
    assertNull(ThreadContext.getSecurityManager());
    Runnable runnable = new Runnable() {

        public void run() {
            Subject callingSubject = SecurityUtils.getSubject();
            assertNotNull(callingSubject);
            assertNotNull(SecurityUtils.getSecurityManager());
            assertEquals(callingSubject, sourceSubject);
        }
    };
    sourceSubject.execute(runnable);
    assertNull(ThreadContext.getSubject());
    assertNull(ThreadContext.getSecurityManager());
}
Also used : DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) Test(org.junit.Test)

Example 27 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class DelegatingSubjectTest method testExecuteCallable.

@Test
public void testExecuteCallable() {
    String username = "jsmith";
    SecurityManager securityManager = createNiceMock(SecurityManager.class);
    PrincipalCollection identity = new SimplePrincipalCollection(username, "testRealm");
    final Subject sourceSubject = new DelegatingSubject(identity, true, null, null, securityManager);
    assertNull(ThreadContext.getSubject());
    assertNull(ThreadContext.getSecurityManager());
    Callable<String> callable = new Callable<String>() {

        public String call() throws Exception {
            Subject callingSubject = SecurityUtils.getSubject();
            assertNotNull(callingSubject);
            assertNotNull(SecurityUtils.getSecurityManager());
            assertEquals(callingSubject, sourceSubject);
            return "Hello " + callingSubject.getPrincipal();
        }
    };
    String response = sourceSubject.execute(callable);
    assertNotNull(response);
    assertEquals("Hello " + username, response);
    assertNull(ThreadContext.getSubject());
    assertNull(ThreadContext.getSecurityManager());
}
Also used : DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 28 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class AbstractShiroTest method tearDownShiro.

@AfterClass
public static void tearDownShiro() {
    doClearSubject();
    try {
        SecurityManager securityManager = getSecurityManager();
        LifecycleUtils.destroy(securityManager);
    } catch (UnavailableSecurityManagerException e) {
    // we don't care about this when cleaning up the test environment
    // (for example, maybe the subclass is a unit test and it didn't
    // need a SecurityManager instance because it was using only mock Subject instances)
    }
    setSecurityManager(null);
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) UnavailableSecurityManagerException(org.apache.shiro.UnavailableSecurityManagerException) AfterClass(org.junit.AfterClass)

Example 29 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class DefaultWebSubjectFactory method createSubject.

public Subject createSubject(SubjectContext context) {
    if (!(context instanceof WebSubjectContext)) {
        return super.createSubject(context);
    }
    WebSubjectContext wsc = (WebSubjectContext) context;
    SecurityManager securityManager = wsc.resolveSecurityManager();
    Session session = wsc.resolveSession();
    boolean sessionEnabled = wsc.isSessionCreationEnabled();
    PrincipalCollection principals = wsc.resolvePrincipals();
    boolean authenticated = wsc.resolveAuthenticated();
    String host = wsc.resolveHost();
    ServletRequest request = wsc.resolveServletRequest();
    ServletResponse response = wsc.resolveServletResponse();
    return new WebDelegatingSubject(principals, authenticated, host, session, sessionEnabled, request, response, securityManager);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) SecurityManager(org.apache.shiro.mgt.SecurityManager) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) WebSubjectContext(org.apache.shiro.web.subject.WebSubjectContext) Session(org.apache.shiro.session.Session) WebDelegatingSubject(org.apache.shiro.web.subject.support.WebDelegatingSubject)

Example 30 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class ShiroWebModuleTest method basicInstantiation.

@Test
public void basicInstantiation() {
    final ShiroModuleTest.MockRealm mockRealm = createMock(ShiroModuleTest.MockRealm.class);
    ServletContext servletContext = createMock(ServletContext.class);
    Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {

        @Override
        protected void configureShiroWeb() {
            bindRealm().to(ShiroModuleTest.MockRealm.class);
            expose(SessionManager.class);
        }

        @Provides
        public ShiroModuleTest.MockRealm createRealm() {
            return mockRealm;
        }
    });
    // we're not getting a WebSecurityManager here b/c it's not exposed.  There didn't seem to be a good reason to
    // expose it outside of the Shiro module.
    SecurityManager securityManager = injector.getInstance(SecurityManager.class);
    assertNotNull(securityManager);
    assertTrue(securityManager instanceof WebSecurityManager);
    SessionManager sessionManager = injector.getInstance(SessionManager.class);
    assertNotNull(sessionManager);
    assertTrue(sessionManager instanceof ServletContainerSessionManager);
    assertTrue(((DefaultWebSecurityManager) securityManager).getSessionManager() instanceof ServletContainerSessionManager);
}
Also used : WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) DefaultWebSecurityManager(org.apache.shiro.web.mgt.DefaultWebSecurityManager) WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultWebSecurityManager(org.apache.shiro.web.mgt.DefaultWebSecurityManager) DefaultWebSessionManager(org.apache.shiro.web.session.mgt.DefaultWebSessionManager) ServletContainerSessionManager(org.apache.shiro.web.session.mgt.ServletContainerSessionManager) SessionManager(org.apache.shiro.session.mgt.SessionManager) DefaultWebSecurityManager(org.apache.shiro.web.mgt.DefaultWebSecurityManager) Provides(com.google.inject.Provides) ServletContainerSessionManager(org.apache.shiro.web.session.mgt.ServletContainerSessionManager) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext) ShiroModuleTest(org.apache.shiro.guice.ShiroModuleTest) Test(org.junit.Test) ShiroModuleTest(org.apache.shiro.guice.ShiroModuleTest)

Aggregations

SecurityManager (org.apache.shiro.mgt.SecurityManager)36 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)13 IniSecurityManagerFactory (org.apache.shiro.config.IniSecurityManagerFactory)11 Test (org.junit.Test)11 Injector (com.google.inject.Injector)8 Provides (com.google.inject.Provides)6 Subject (org.apache.shiro.subject.Subject)6 Session (org.apache.shiro.session.Session)5 WebSecurityManager (org.apache.shiro.web.mgt.WebSecurityManager)5 Realm (org.apache.shiro.realm.Realm)4 IniRealm (org.apache.shiro.realm.text.IniRealm)4 DefaultWebSecurityManager (org.apache.shiro.web.mgt.DefaultWebSecurityManager)4 UnavailableSecurityManagerException (org.apache.shiro.UnavailableSecurityManagerException)3 Ini (org.apache.shiro.config.Ini)3 DelegatingSubject (org.apache.shiro.subject.support.DelegatingSubject)3 AfterClass (org.junit.AfterClass)3 Callable (java.util.concurrent.Callable)2 ServletContext (javax.servlet.ServletContext)2 ConfigurationException (org.apache.shiro.config.ConfigurationException)2 ShiroModuleTest (org.apache.shiro.guice.ShiroModuleTest)2