use of org.apache.shiro.session.Session in project shiro by apache.
the class DefaultWebSecurityManagerTest method testGetSubjectByRequestSessionId.
@Test
public void testGetSubjectByRequestSessionId() {
shiroSessionModeInit();
HttpServletRequest mockRequest = createNiceMock(HttpServletRequest.class);
HttpServletResponse mockResponse = createNiceMock(HttpServletResponse.class);
replay(mockRequest);
replay(mockResponse);
Subject subject = newSubject(mockRequest, mockResponse);
Session session = subject.getSession();
Serializable sessionId = session.getId();
assertNotNull(sessionId);
verify(mockRequest);
verify(mockResponse);
mockRequest = createNiceMock(HttpServletRequest.class);
mockResponse = createNiceMock(HttpServletResponse.class);
// now simulate the cookie going with the request and the Subject should be acquired based on that:
Cookie[] cookies = new Cookie[] { new Cookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME, sessionId.toString()) };
expect(mockRequest.getCookies()).andReturn(cookies).anyTimes();
expect(mockRequest.getParameter(isA(String.class))).andReturn(null).anyTimes();
replay(mockRequest);
replay(mockResponse);
subject = newSubject(mockRequest, mockResponse);
session = subject.getSession(false);
assertNotNull(session);
assertEquals(sessionId, session.getId());
verify(mockRequest);
verify(mockResponse);
}
use of org.apache.shiro.session.Session 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.session.Session in project shiro by apache.
the class WebUtils method getSavedRequest.
public static SavedRequest getSavedRequest(ServletRequest request) {
SavedRequest savedRequest = null;
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession(false);
if (session != null) {
savedRequest = (SavedRequest) session.getAttribute(SAVED_REQUEST_KEY);
}
return savedRequest;
}
use of org.apache.shiro.session.Session in project shiro by apache.
the class ShiroSessionScopeTest method testScope.
@Test
public void testScope() throws Exception {
Subject subject = createMock(Subject.class);
try {
ThreadContext.bind(subject);
final Key<SomeClass> key = Key.get(SomeClass.class);
Provider<SomeClass> mockProvider = createMock(Provider.class);
Session session = createMock(Session.class);
SomeClass retuned = new SomeClass();
expect(subject.getSession()).andReturn(session);
expect(session.getAttribute(key)).andReturn(null);
expect(mockProvider.get()).andReturn(retuned);
expect(subject.getSession()).andReturn(session);
expect(session.getAttribute(key)).andReturn(retuned);
replay(subject, mockProvider, session);
ShiroSessionScope underTest = new ShiroSessionScope();
// first time the session doesn't contain it, we expect the provider to be invoked
assertSame(retuned, underTest.scope(key, mockProvider).get());
// second time the session does contain it, we expect the provider to not be invoked
assertSame(retuned, underTest.scope(key, mockProvider).get());
verify(subject, mockProvider, session);
} finally {
ThreadContext.unbindSubject();
}
}
use of org.apache.shiro.session.Session in project shiro by apache.
the class IndexController method buildModel.
protected Model buildModel(Model model) {
Subject subject = SecurityUtils.getSubject();
boolean hasRole1 = subject.hasRole("role1");
boolean hasRole2 = subject.hasRole("role2");
model.addAttribute("hasRole1", hasRole1);
model.addAttribute("hasRole2", hasRole2);
Session session = subject.getSession();
Map<Object, Object> sessionAttributes = new LinkedHashMap<Object, Object>();
for (Object key : session.getAttributeKeys()) {
sessionAttributes.put(key, session.getAttribute(key));
}
model.addAttribute("sessionAttributes", sessionAttributes);
model.addAttribute("subjectSession", subject.getSession());
return model;
}
Aggregations