Search in sources :

Example 31 with SecurityManager

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

the class ShiroWebModuleTest method testBindWebSecurityManager.

@Test
public void testBindWebSecurityManager() throws Exception {
    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(WebSecurityManager.class);
        }

        @Provides
        public ShiroModuleTest.MockRealm createRealm() {
            return mockRealm;
        }

        @Override
        protected void bindWebSecurityManager(AnnotatedBindingBuilder<? super WebSecurityManager> bind) {
            bind.to(MyDefaultWebSecurityManager.class).asEagerSingleton();
        }
    });
    SecurityManager securityManager = injector.getInstance(SecurityManager.class);
    assertNotNull(securityManager);
    assertTrue(securityManager instanceof MyDefaultWebSecurityManager);
    WebSecurityManager webSecurityManager = injector.getInstance(WebSecurityManager.class);
    assertNotNull(webSecurityManager);
    assertTrue(webSecurityManager instanceof MyDefaultWebSecurityManager);
    // SHIRO-435: Check both keys SecurityManager and WebSecurityManager are bound to the same instance
    assertTrue(securityManager == webSecurityManager);
}
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) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext) ShiroModuleTest(org.apache.shiro.guice.ShiroModuleTest) Provides(com.google.inject.Provides) Test(org.junit.Test) ShiroModuleTest(org.apache.shiro.guice.ShiroModuleTest)

Example 32 with SecurityManager

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

the class ShiroModuleTest method testBindSecurityManager.

@Test
public void testBindSecurityManager() {
    final MockRealm mockRealm = createMock(MockRealm.class);
    Injector injector = Guice.createInjector(new ShiroModule() {

        @Override
        protected void configureShiro() {
            bindRealm().to(MockRealm.class);
        }

        @Provides
        public MockRealm createRealm() {
            return mockRealm;
        }

        @Override
        protected void bindSecurityManager(AnnotatedBindingBuilder<? super SecurityManager> bind) {
            bind.to(MyDefaultSecurityManager.class);
        }
    });
    SecurityManager securityManager = injector.getInstance(SecurityManager.class);
    assertNotNull(securityManager);
    assertTrue(securityManager instanceof MyDefaultSecurityManager);
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) Test(org.junit.Test)

Example 33 with SecurityManager

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

the class ShiroModuleTest method testBindSessionManager.

@Test
public void testBindSessionManager() {
    final MockRealm mockRealm = createMock(MockRealm.class);
    Injector injector = Guice.createInjector(new ShiroModule() {

        @Override
        protected void configureShiro() {
            bindRealm().to(MockRealm.class);
        }

        @Provides
        public MockRealm createRealm() {
            return mockRealm;
        }

        @Override
        protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) {
            bind.to(MyDefaultSessionManager.class);
        }
    });
    DefaultSecurityManager securityManager = (DefaultSecurityManager) injector.getInstance(SecurityManager.class);
    assertNotNull(securityManager);
    assertNotNull(securityManager.getSessionManager());
    assertTrue(securityManager.getSessionManager() instanceof MyDefaultSessionManager);
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Injector(com.google.inject.Injector) DefaultSessionManager(org.apache.shiro.session.mgt.DefaultSessionManager) SessionManager(org.apache.shiro.session.mgt.SessionManager) Provides(com.google.inject.Provides) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Test(org.junit.Test)

Example 34 with SecurityManager

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

the class ShiroModuleTest method basicInstantiation.

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

        @Override
        protected void configureShiro() {
            bindRealm().to(MockRealm.class);
        }

        @Provides
        public MockRealm createRealm() {
            return mockRealm;
        }
    });
    SecurityManager securityManager = injector.getInstance(SecurityManager.class);
    assertNotNull(securityManager);
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) Test(org.junit.Test)

Example 35 with SecurityManager

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

the class ShiroModuleTest method testConfigure.

@Test
public void testConfigure() {
    final MockRealm mockRealm = createMock(MockRealm.class);
    AuthenticationToken authToken = createMock(AuthenticationToken.class);
    AuthenticationInfo info = new SimpleAuthenticationInfo("mockUser", "password", "mockRealm");
    expect(mockRealm.supports(authToken)).andReturn(true);
    expect(mockRealm.getAuthenticationInfo(authToken)).andReturn(info);
    replay(mockRealm);
    Injector injector = Guice.createInjector(new ShiroModule() {

        @Override
        protected void configureShiro() {
            bindRealm().to(MockRealm.class);
        }

        @Provides
        public MockRealm createRealm() {
            return mockRealm;
        }
    });
    SecurityManager securityManager = injector.getInstance(SecurityManager.class);
    assertNotNull(securityManager);
    SecurityUtils.setSecurityManager(securityManager);
    final Subject subject = new Subject.Builder(securityManager).buildSubject();
    securityManager.login(subject, authToken);
    verify(mockRealm);
}
Also used : AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) AuthenticationInfo(org.apache.shiro.authc.AuthenticationInfo) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) Subject(org.apache.shiro.subject.Subject) Test(org.junit.Test)

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