Parameterize SpringSessionBackedSessionRegistry

Fixes gh-781
This commit is contained in:
Vedran Pavic
2017-01-07 17:25:45 +01:00
parent 16abdc5e5c
commit a03cb02536
4 changed files with 116 additions and 70 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,20 +33,23 @@ import org.springframework.session.security.SpringSessionBackedSessionRegistry;
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
FindByIndexNameSessionRepository<ExpiringSession> sessionRepository;
private FindByIndexNameSessionRepository<ExpiringSession> sessionRepository;
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
// other config goes here...
.sessionManagement()
.maximumSessions(2)
.sessionRegistry(sessionRegistry());
// @formatter:on
}
@Bean
SpringSessionBackedSessionRegistry sessionRegistry() {
return new SpringSessionBackedSessionRegistry(this.sessionRepository);
return new SpringSessionBackedSessionRegistry<ExpiringSession>(
this.sessionRepository);
}
}
// end::class[]

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,27 +23,43 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
/**
* Ensures that calling {@link #expireNow()} propagates to Spring Session,
* since this session information contains only derived data and is not the authoritative source.
* Ensures that calling {@link #expireNow()} propagates to Spring Session, since this
* session information contains only derived data and is not the authoritative source.
*
* @param <S> the {@link ExpiringSession} type.
* @author Joris Kuipers
* @author Vedran Pavic
* @since 1.3
*/
class SpringSessionBackedSessionInformation extends SessionInformation {
class SpringSessionBackedSessionInformation<S extends ExpiringSession>
extends SessionInformation {
static final String EXPIRED_ATTR = SpringSessionBackedSessionInformation.class.getName() + ".EXPIRED";
static final String EXPIRED_ATTR = SpringSessionBackedSessionInformation.class
.getName() + ".EXPIRED";
private static final Log logger = LogFactory.getLog(SpringSessionBackedSessionInformation.class);
private static final Log logger = LogFactory
.getLog(SpringSessionBackedSessionInformation.class);
private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";
private final SessionRepository<S> sessionRepository;
SpringSessionBackedSessionInformation(S session,
SessionRepository<S> sessionRepository) {
super(resolvePrincipal(session), session.getId(),
new Date(session.getLastAccessedTime()));
this.sessionRepository = sessionRepository;
if (Boolean.TRUE.equals(session.getAttribute(EXPIRED_ATTR))) {
super.expireNow();
}
}
/**
* Tries to determine the principal's name from the given Session.
*
@@ -51,7 +67,8 @@ class SpringSessionBackedSessionInformation extends SessionInformation {
* @return the principal's name, or empty String if it couldn't be determined
*/
private static String resolvePrincipal(Session session) {
String principalName = session.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
String principalName = session
.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
if (principalName != null) {
return principalName;
}
@@ -62,30 +79,22 @@ class SpringSessionBackedSessionInformation extends SessionInformation {
return "";
}
private final SessionRepository<ExpiringSession> sessionRepository;
SpringSessionBackedSessionInformation(ExpiringSession session, SessionRepository<ExpiringSession> sessionRepository) {
super(resolvePrincipal(session), session.getId(), new Date(session.getLastAccessedTime()));
this.sessionRepository = sessionRepository;
if (Boolean.TRUE.equals(session.getAttribute(EXPIRED_ATTR))) {
super.expireNow();
}
}
@Override
public void expireNow() {
if (logger.isDebugEnabled()) {
logger.debug("Expiring session " + getSessionId() + " for user '" + getPrincipal() +
"', presumably because maximum allowed concurrent sessions was exceeded");
logger.debug("Expiring session " + getSessionId() + " for user '"
+ getPrincipal() + "', presumably because maximum allowed concurrent "
+ "sessions was exceeded");
}
super.expireNow();
ExpiringSession session = this.sessionRepository.getSession(getSessionId());
S session = this.sessionRepository.getSession(getSessionId());
if (session != null) {
session.setAttribute(EXPIRED_ATTR, Boolean.TRUE);
this.sessionRepository.save(session);
}
else {
logger.info("Could not find Session with id " + getSessionId() + " to mark as expired");
logger.info("Could not find Session with id " + getSessionId()
+ " to mark as expired");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
package org.springframework.session.security;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -25,54 +24,64 @@ import java.util.List;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.util.Assert;
/**
* A {@link SessionRegistry} that retrieves session information from Spring Session, rather than maintaining it itself.
* This allows concurrent session management with Spring Security in a clustered environment.
* A {@link SessionRegistry} that retrieves session information from Spring Session,
* rather than maintaining it itself. This allows concurrent session management with
* Spring Security in a clustered environment.
* <p>
* Relies on being able to derive the same String-based representation of the principal given to
* {@link #getAllSessions(Object, boolean)} as used by Spring Session in order to look up the user's sessions.
* Relies on being able to derive the same String-based representation of the principal
* given to {@link #getAllSessions(Object, boolean)} as used by Spring Session in order to
* look up the user's sessions.
* <p>
* Does not support {@link #getAllPrincipals()}, since that information is not available.
*
* @param <S> the {@link ExpiringSession} type.
* @author Joris Kuipers
* @author Vedran Pavic
* @since 1.3
*/
public class SpringSessionBackedSessionRegistry implements SessionRegistry {
public class SpringSessionBackedSessionRegistry<S extends ExpiringSession>
implements SessionRegistry {
private final FindByIndexNameSessionRepository<ExpiringSession> sessionRepository;
private final FindByIndexNameSessionRepository<S> sessionRepository;
public SpringSessionBackedSessionRegistry(FindByIndexNameSessionRepository<ExpiringSession> sessionRepository) {
public SpringSessionBackedSessionRegistry(
FindByIndexNameSessionRepository<S> sessionRepository) {
Assert.notNull(sessionRepository, "sessionRepository cannot be null");
this.sessionRepository = sessionRepository;
}
public List<Object> getAllPrincipals() {
throw new UnsupportedOperationException("SpringSessionBackedSessionRegistry does not support retrieving all principals, " +
"since Spring Session provides no way to obtain that information");
throw new UnsupportedOperationException("SpringSessionBackedSessionRegistry does "
+ "not support retrieving all principals, since Spring Session provides "
+ "no way to obtain that information");
}
public List<SessionInformation> getAllSessions(Object principal, boolean includeExpiredSessions) {
Collection<ExpiringSession> sessions =
this.sessionRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, name(principal)).values();
public List<SessionInformation> getAllSessions(Object principal,
boolean includeExpiredSessions) {
Collection<S> sessions = this.sessionRepository.findByIndexNameAndIndexValue(
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME,
name(principal)).values();
List<SessionInformation> infos = new ArrayList<SessionInformation>();
for (ExpiringSession session : sessions) {
if (includeExpiredSessions || !Boolean.TRUE.equals(session.getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR))) {
infos.add(new SpringSessionBackedSessionInformation(session, this.sessionRepository));
for (S session : sessions) {
if (includeExpiredSessions || !Boolean.TRUE.equals(session
.getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR))) {
infos.add(new SpringSessionBackedSessionInformation<S>(session,
this.sessionRepository));
}
}
return infos;
}
public SessionInformation getSessionInformation(String sessionId) {
ExpiringSession session = this.sessionRepository.getSession(sessionId);
S session = this.sessionRepository.getSession(sessionId);
if (session != null) {
return new SpringSessionBackedSessionInformation(session, this.sessionRepository);
return new SpringSessionBackedSessionInformation<S>(session,
this.sessionRepository);
}
return null;
}
@@ -99,7 +108,8 @@ public class SpringSessionBackedSessionRegistry implements SessionRegistry {
* Derives a String name for the given principal.
*
* @param principal as provided by Spring Security
* @return name of the principal, or its {@code toString()} representation if no name could be derived
* @return name of the principal, or its {@code toString()} representation if no name
* could be derived
*/
protected String name(Object principal) {
if (principal instanceof UserDetails) {
@@ -110,4 +120,5 @@ public class SpringSessionBackedSessionRegistry implements SessionRegistry {
}
return principal.toString();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@ import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
@@ -44,27 +43,36 @@ import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;
import static org.mockito.BDDMockito.when;
/**
* Tests for {@link SpringSessionBackedSessionRegistry}.
*/
@RunWith(MockitoJUnitRunner.class)
public class SpringSessionBackedSessionRegistryTest {
static final String SESSION_ID = "sessionId";
static final String SESSION_ID2 = "otherSessionId";
static final String USER_NAME = "userName";
static final User PRINCIPAL = new User(USER_NAME, "password", Collections.<GrantedAuthority>emptyList());
static final Date NOW = new Date();
private static final String SESSION_ID = "sessionId";
private static final String SESSION_ID2 = "otherSessionId";
private static final String USER_NAME = "userName";
private static final User PRINCIPAL = new User(USER_NAME, "password",
Collections.<GrantedAuthority>emptyList());
private static final Date NOW = new Date();
@Mock
FindByIndexNameSessionRepository<ExpiringSession> sessionRepository;
private FindByIndexNameSessionRepository<ExpiringSession> sessionRepository;
@InjectMocks
SpringSessionBackedSessionRegistry sessionRegistry;
private SpringSessionBackedSessionRegistry<ExpiringSession> sessionRegistry;
@Test
public void sessionInformationForExistingSession() {
ExpiringSession session = createSession(SESSION_ID, USER_NAME, NOW.getTime());
when(this.sessionRepository.getSession(SESSION_ID)).thenReturn(session);
SessionInformation sessionInfo = this.sessionRegistry.getSessionInformation(SESSION_ID);
SessionInformation sessionInfo = this.sessionRegistry
.getSessionInformation(SESSION_ID);
assertThat(sessionInfo.getSessionId()).isEqualTo(SESSION_ID);
assertThat(sessionInfo.getLastRequest()).isEqualTo(NOW);
@@ -75,10 +83,12 @@ public class SpringSessionBackedSessionRegistryTest {
@Test
public void sessionInformationForExpiredSession() {
ExpiringSession session = createSession(SESSION_ID, USER_NAME, NOW.getTime());
session.setAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR, Boolean.TRUE);
session.setAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR,
Boolean.TRUE);
when(this.sessionRepository.getSession(SESSION_ID)).thenReturn(session);
SessionInformation sessionInfo = this.sessionRegistry.getSessionInformation(SESSION_ID);
SessionInformation sessionInfo = this.sessionRegistry
.getSessionInformation(SESSION_ID);
assertThat(sessionInfo.getSessionId()).isEqualTo(SESSION_ID);
assertThat(sessionInfo.getLastRequest()).isEqualTo(NOW);
@@ -88,25 +98,30 @@ public class SpringSessionBackedSessionRegistryTest {
@Test
public void noSessionInformationForMissingSession() {
assertThat(this.sessionRegistry.getSessionInformation("nonExistingSessionId")).isNull();
assertThat(this.sessionRegistry.getSessionInformation("nonExistingSessionId"))
.isNull();
}
@Test
public void getAllSessions() {
setUpSessions();
List<SessionInformation> allSessionInfos = this.sessionRegistry.getAllSessions(PRINCIPAL, true);
List<SessionInformation> allSessionInfos = this.sessionRegistry
.getAllSessions(PRINCIPAL, true);
assertThat(allSessionInfos).extracting("sessionId").containsExactly(SESSION_ID, SESSION_ID2);
assertThat(allSessionInfos).extracting("sessionId").containsExactly(SESSION_ID,
SESSION_ID2);
}
@Test
public void getNonExpiredSessions() {
setUpSessions();
List<SessionInformation> nonExpiredSessionInfos = this.sessionRegistry.getAllSessions(PRINCIPAL, false);
List<SessionInformation> nonExpiredSessionInfos = this.sessionRegistry
.getAllSessions(PRINCIPAL, false);
assertThat(nonExpiredSessionInfos).extracting("sessionId").containsExactly(SESSION_ID2);
assertThat(nonExpiredSessionInfos).extracting("sessionId")
.containsExactly(SESSION_ID2);
}
@Test
@@ -114,18 +129,23 @@ public class SpringSessionBackedSessionRegistryTest {
ExpiringSession session = createSession(SESSION_ID, USER_NAME, NOW.getTime());
when(this.sessionRepository.getSession(SESSION_ID)).thenReturn(session);
SessionInformation sessionInfo = this.sessionRegistry.getSessionInformation(SESSION_ID);
SessionInformation sessionInfo = this.sessionRegistry
.getSessionInformation(SESSION_ID);
assertThat(sessionInfo.isExpired()).isFalse();
sessionInfo.expireNow();
assertThat(sessionInfo.isExpired()).isTrue();
ArgumentCaptor<ExpiringSession> captor = ArgumentCaptor.forClass(ExpiringSession.class);
ArgumentCaptor<ExpiringSession> captor = ArgumentCaptor
.forClass(ExpiringSession.class);
verify(this.sessionRepository).save(captor.capture());
assertThat(captor.getValue().getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR)).isEqualTo(Boolean.TRUE);
assertThat(captor.getValue()
.getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR))
.isEqualTo(Boolean.TRUE);
}
private ExpiringSession createSession(String sessionId, String userName, Long lastAccessed) {
private ExpiringSession createSession(String sessionId, String userName,
Long lastAccessed) {
MapSession session = new MapSession(sessionId);
session.setLastAccessedTime(lastAccessed);
Authentication authentication = mock(Authentication.class);
@@ -138,12 +158,15 @@ public class SpringSessionBackedSessionRegistryTest {
private void setUpSessions() {
ExpiringSession session1 = createSession(SESSION_ID, USER_NAME, NOW.getTime());
session1.setAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR, Boolean.TRUE);
session1.setAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR,
Boolean.TRUE);
ExpiringSession session2 = createSession(SESSION_ID2, USER_NAME, NOW.getTime());
Map<String, ExpiringSession> sessions = new LinkedHashMap<String, ExpiringSession>();
sessions.put(session1.getId(), session1);
sessions.put(session2.getId(), session2);
when(this.sessionRepository.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, USER_NAME)).thenReturn(sessions);
when(this.sessionRepository.findByIndexNameAndIndexValue(
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, USER_NAME))
.thenReturn(sessions);
}
}