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());
}
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());
}
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);
}
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);
}
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);
}
Aggregations