Rename HttpSessionStrategy to HttpSessionIdResolver

This commit harmonizes `HttpSessionStrategy` with Spring Framework's `WebSessionIdResolver` by renaming it to `WebSessionIdResolver`.
This commit is contained in:
Vedran Pavic
2017-10-26 07:52:19 +02:00
committed by Rob Winch
parent cd394bbe10
commit 6f05c84aa7
12 changed files with 185 additions and 201 deletions

View File

@@ -31,8 +31,8 @@ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactor
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.session.Session;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
import org.springframework.session.web.http.HttpSessionIdResolver;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -104,8 +104,8 @@ public class RestMockMvcTests {
}
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy();
public HttpSessionIdResolver httpSessionIdResolver() {
return new HeaderHttpSessionIdResolver();
}
}

View File

@@ -21,8 +21,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
import org.springframework.session.web.http.HttpSessionIdResolver;
@Import(EmbeddedRedisConfig.class)
// tag::class[]
@@ -36,8 +36,8 @@ public class HttpSessionConfig {
}
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy(); // <3>
public HttpSessionIdResolver httpSessionIdResolver() {
return new HeaderHttpSessionIdResolver(); // <3>
}
}
// end::class[]

View File

@@ -38,10 +38,10 @@ import org.springframework.session.SessionRepository;
import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDestroyedEvent;
import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices;
import org.springframework.session.web.http.CookieHttpSessionStrategy;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionIdResolver;
import org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.util.ClassUtils;
@@ -96,7 +96,7 @@ public class SpringHttpSessionConfiguration implements ApplicationContextAware {
private final Log logger = LogFactory.getLog(getClass());
private CookieHttpSessionStrategy defaultHttpSessionStrategy = new CookieHttpSessionStrategy();
private CookieHttpSessionIdResolver defaultHttpSessionIdResolver = new CookieHttpSessionIdResolver();
private boolean usesSpringSessionRememberMeServices;
@@ -104,7 +104,7 @@ public class SpringHttpSessionConfiguration implements ApplicationContextAware {
private CookieSerializer cookieSerializer;
private HttpSessionStrategy httpSessionStrategy = this.defaultHttpSessionStrategy;
private HttpSessionIdResolver httpSessionIdResolver = this.defaultHttpSessionIdResolver;
private List<HttpSessionListener> httpSessionListeners = new ArrayList<>();
@@ -112,7 +112,7 @@ public class SpringHttpSessionConfiguration implements ApplicationContextAware {
public void init() {
CookieSerializer cookieSerializer = this.cookieSerializer != null
? this.cookieSerializer : createDefaultCookieSerializer();
this.defaultHttpSessionStrategy.setCookieSerializer(cookieSerializer);
this.defaultHttpSessionIdResolver.setCookieSerializer(cookieSerializer);
}
@Bean
@@ -126,7 +126,7 @@ public class SpringHttpSessionConfiguration implements ApplicationContextAware {
SessionRepositoryFilter<S> sessionRepositoryFilter = new SessionRepositoryFilter<>(
sessionRepository);
sessionRepositoryFilter.setServletContext(this.servletContext);
sessionRepositoryFilter.setHttpSessionStrategy(this.httpSessionStrategy);
sessionRepositoryFilter.setHttpSessionIdResolver(this.httpSessionIdResolver);
return sessionRepositoryFilter;
}
@@ -153,8 +153,8 @@ public class SpringHttpSessionConfiguration implements ApplicationContextAware {
}
@Autowired(required = false)
public void setHttpSessionStrategy(HttpSessionStrategy httpSessionStrategy) {
this.httpSessionStrategy = httpSessionStrategy;
public void setHttpSessionIdResolver(HttpSessionIdResolver httpSessionIdResolver) {
this.httpSessionIdResolver = httpSessionIdResolver;
}
@Autowired(required = false)

View File

@@ -21,13 +21,12 @@ import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.session.Session;
import org.springframework.session.web.http.CookieSerializer.CookieValue;
/**
* A {@link HttpSessionStrategy} that uses a cookie to obtain the session from.
* A {@link HttpSessionIdResolver} that uses a cookie to obtain the session from.
* Specifically, this implementation will allow specifying a cookie serialization strategy
* using {@link CookieHttpSessionStrategy#setCookieSerializer(CookieSerializer)}. The
* using {@link CookieHttpSessionIdResolver#setCookieSerializer(CookieSerializer)}. The
* default is cookie name is "SESSION".
*
* When a session is created, the HTTP response will have a cookie with the specified
@@ -62,22 +61,21 @@ import org.springframework.session.web.http.CookieSerializer.CookieValue;
* @author Vedran Pavic
* @since 1.0
*/
public final class CookieHttpSessionStrategy implements HttpSessionStrategy {
public final class CookieHttpSessionIdResolver implements HttpSessionIdResolver {
private static final String WRITTEN_SESSION_ID_ATTR = CookieHttpSessionStrategy.class
private static final String WRITTEN_SESSION_ID_ATTR = CookieHttpSessionIdResolver.class
.getName().concat(".WRITTEN_SESSION_ID_ATTR");
private CookieSerializer cookieSerializer = new DefaultCookieSerializer();
@Override
public List<String> getRequestedSessionIds(HttpServletRequest request) {
public List<String> resolveSessionIds(HttpServletRequest request) {
return this.cookieSerializer.readCookieValues(request);
}
@Override
public void onNewSession(Session session, HttpServletRequest request,
HttpServletResponse response) {
String sessionId = session.getId();
public void setSessionId(HttpServletRequest request, HttpServletResponse response,
String sessionId) {
if (sessionId.equals(request.getAttribute(WRITTEN_SESSION_ID_ATTR))) {
return;
}
@@ -87,8 +85,7 @@ public final class CookieHttpSessionStrategy implements HttpSessionStrategy {
}
@Override
public void onInvalidateSession(HttpServletRequest request,
HttpServletResponse response) {
public void expireSession(HttpServletRequest request, HttpServletResponse response) {
this.cookieSerializer.writeCookieValue(new CookieValue(request, response, ""));
}

View File

@@ -22,12 +22,11 @@ import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.session.Session;
/**
* A {@link HttpSessionStrategy} that uses a header to obtain the session from.
* A {@link HttpSessionIdResolver} that uses a header to obtain the session from.
* Specifically, this implementation will allow specifying a header name using
* {@link HeaderHttpSessionStrategy#setHeaderName(String)}. The default is "X-Auth-Token".
* {@link HeaderHttpSessionIdResolver#setHeaderName(String)}. The default is
* "X-Auth-Token".
*
* When a session is created, the HTTP response will have a response header of the
* specified name and the value of the session id. For example:
@@ -58,26 +57,25 @@ import org.springframework.session.Session;
* @author Vedran Pavic
* @since 1.0
*/
public class HeaderHttpSessionStrategy implements HttpSessionStrategy {
public class HeaderHttpSessionIdResolver implements HttpSessionIdResolver {
private String headerName = "X-Auth-Token";
@Override
public List<String> getRequestedSessionIds(HttpServletRequest request) {
public List<String> resolveSessionIds(HttpServletRequest request) {
String headerValue = request.getHeader(this.headerName);
return headerValue != null ? Collections.singletonList(headerValue)
: Collections.emptyList();
}
@Override
public void onNewSession(Session session, HttpServletRequest request,
HttpServletResponse response) {
response.setHeader(this.headerName, session.getId());
public void setSessionId(HttpServletRequest request, HttpServletResponse response,
String sessionId) {
response.setHeader(this.headerName, sessionId);
}
@Override
public void onInvalidateSession(HttpServletRequest request,
HttpServletResponse response) {
public void expireSession(HttpServletRequest request, HttpServletResponse response) {
response.setHeader(this.headerName, "");
}

View File

@@ -0,0 +1,66 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.web.http;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Contract for session id resolution strategies. Allows for session id resolution through
* the request and for sending the session id or expiring the session through the
* response.
*
* @author Rob Winch
* @author Vedran Pavic
* @since 1.0
*/
public interface HttpSessionIdResolver {
/**
* Resolve the session ids associated with the provided {@link HttpServletRequest}.
* For example, the session id might come from a cookie or a request header.
* @param request the current request
* @return the session ids
*/
List<String> resolveSessionIds(HttpServletRequest request);
/**
* Send the given session id to the client. This method is invoked when a new session
* is created and should inform a client what the new session id is. For example, it
* might create a new cookie with the session id in it or set an HTTP response header
* with the value of the new session id.
* @param request the current request
* @param response the current response
* @param sessionId the session id
*/
void setSessionId(HttpServletRequest request, HttpServletResponse response,
String sessionId);
/**
* Instruct the client to end the current session. This method is invoked when a
* session is invalidated and should inform a client that the session id is no longer
* valid. For example, it might remove a cookie with the session id in it or set an
* HTTP response header with an empty value indicating to the client to no longer
* submit that session id.
* @param request the current request
* @param response the current response
*/
void expireSession(HttpServletRequest request, HttpServletResponse response);
}

View File

@@ -1,82 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.web.http;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.session.Session;
/**
* A strategy for mapping HTTP request and responses to a {@link Session}.
*
* @author Rob Winch
* @author Vedran Pavic
* @since 1.0
*/
public interface HttpSessionStrategy {
/**
* Obtains the requested session id from the provided
* {@link javax.servlet.http.HttpServletRequest}. For example, the session id might
* come from a cookie or a request header.
*
* @param request the {@link javax.servlet.http.HttpServletRequest} to obtain the
* session id from. Cannot be null.
* @return the session ids
*/
List<String> getRequestedSessionIds(HttpServletRequest request);
/**
* This method is invoked when a new session is created and should inform a client
* what the new session id is. For example, it might create a new cookie with the
* session id in it or set an HTTP response header with the value of the new session
* id.
*
* Some implementations may wish to associate additional information to the
* {@link Session} at this time. For example, they may wish to add the IP Address,
* browser headers, the username, etc to the
* {@link org.springframework.session.Session}.
*
* @param session the {@link org.springframework.session.Session} that is being sent
* to the client. Cannot be null.
* @param request the {@link javax.servlet.http.HttpServletRequest} that create the
* new {@link org.springframework.session.Session} Cannot be null.
* @param response the {@link javax.servlet.http.HttpServletResponse} that is
* associated with the {@link javax.servlet.http.HttpServletRequest} that created the
* new {@link org.springframework.session.Session} Cannot be null.
*/
void onNewSession(Session session, HttpServletRequest request,
HttpServletResponse response);
/**
* This method is invoked when a session is invalidated and should inform a client
* that the session id is no longer valid. For example, it might remove a cookie with
* the session id in it or set an HTTP response header with an empty value indicating
* to the client to no longer submit that session id.
*
* @param request the {@link javax.servlet.http.HttpServletRequest} that invalidated
* the {@link org.springframework.session.Session} Cannot be null.
* @param response the {@link javax.servlet.http.HttpServletResponse} that is
* associated with the {@link javax.servlet.http.HttpServletRequest} that invalidated
* the {@link org.springframework.session.Session} Cannot be null.
*/
void onInvalidateSession(HttpServletRequest request, HttpServletResponse response);
}

View File

@@ -44,19 +44,19 @@ import org.springframework.session.SessionRepository;
* {@link org.springframework.session.Session} returned by the
* {@link org.springframework.session.SessionRepository}.
*
* The {@link SessionRepositoryFilter} uses a {@link HttpSessionStrategy} (default
* {@link CookieHttpSessionStrategy} to bridge logic between an
* The {@link SessionRepositoryFilter} uses a {@link HttpSessionIdResolver} (default
* {@link CookieHttpSessionIdResolver} to bridge logic between an
* {@link javax.servlet.http.HttpSession} and the
* {@link org.springframework.session.Session} abstraction. Specifically:
*
* <ul>
* <li>The session id is looked up using
* {@link HttpSessionStrategy#getRequestedSessionIds(javax.servlet.http.HttpServletRequest)}
* {@link HttpSessionIdResolver#resolveSessionIds(javax.servlet.http.HttpServletRequest)}
* . The default is to look in a cookie named SESSION.</li>
* <li>The session id of newly created {@link org.springframework.session.Session} is sent
* to the client using
* <li>The client is notified that the session id is no longer valid with
* {@link HttpSessionStrategy#onInvalidateSession(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}
* {@link HttpSessionIdResolver#expireSession(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}
* </li>
* </ul>
*
@@ -103,7 +103,7 @@ public class SessionRepositoryFilter<S extends Session> extends OncePerRequestFi
private ServletContext servletContext;
private HttpSessionStrategy httpSessionStrategy = new CookieHttpSessionStrategy();
private HttpSessionIdResolver httpSessionIdResolver = new CookieHttpSessionIdResolver();
/**
* Creates a new instance.
@@ -118,16 +118,17 @@ public class SessionRepositoryFilter<S extends Session> extends OncePerRequestFi
}
/**
* Sets the {@link HttpSessionStrategy} to be used. The default is a
* {@link CookieHttpSessionStrategy}.
* Sets the {@link HttpSessionIdResolver} to be used. The default is a
* {@link CookieHttpSessionIdResolver}.
*
* @param httpSessionStrategy the {@link HttpSessionStrategy} to use. Cannot be null.
* @param httpSessionIdResolver the {@link HttpSessionIdResolver} to use. Cannot be
* null.
*/
public void setHttpSessionStrategy(HttpSessionStrategy httpSessionStrategy) {
if (httpSessionStrategy == null) {
throw new IllegalArgumentException("httpSessionStrategy cannot be null");
public void setHttpSessionIdResolver(HttpSessionIdResolver httpSessionIdResolver) {
if (httpSessionIdResolver == null) {
throw new IllegalArgumentException("httpSessionIdResolver cannot be null");
}
this.httpSessionStrategy = httpSessionStrategy;
this.httpSessionIdResolver = httpSessionIdResolver;
}
@Override
@@ -211,24 +212,25 @@ public class SessionRepositoryFilter<S extends Session> extends OncePerRequestFi
}
/**
* Uses the HttpSessionStrategy to write the session id to the response and
* persist the Session.
* Uses the {@link HttpSessionIdResolver} to write the session id to the response
* and persist the Session.
*/
private void commitSession() {
HttpSessionWrapper wrappedSession = getCurrentSession();
if (wrappedSession == null) {
if (isInvalidateClientSession()) {
SessionRepositoryFilter.this.httpSessionStrategy
.onInvalidateSession(this, this.response);
SessionRepositoryFilter.this.httpSessionIdResolver.expireSession(this,
this.response);
}
}
else {
S session = wrappedSession.getSession();
SessionRepositoryFilter.this.sessionRepository.save(session);
String sessionId = session.getId();
if (!isRequestedSessionIdValid()
|| !session.getId().equals(getRequestedSessionId())) {
SessionRepositoryFilter.this.httpSessionStrategy.onNewSession(session,
this, this.response);
|| !sessionId.equals(getRequestedSessionId())) {
SessionRepositoryFilter.this.httpSessionIdResolver.setSessionId(this,
this.response, sessionId);
}
}
}
@@ -351,8 +353,8 @@ public class SessionRepositoryFilter<S extends Session> extends OncePerRequestFi
@Override
public String getRequestedSessionId() {
return SessionRepositoryFilter.this.httpSessionStrategy
.getRequestedSessionIds(this).stream()
return SessionRepositoryFilter.this.httpSessionIdResolver
.resolveSessionIds(this).stream()
.filter(sessionId -> SessionRepositoryFilter.this.sessionRepository
.findById(sessionId) != null)
.findFirst().orElse(null);

View File

@@ -35,7 +35,7 @@ import org.springframework.mock.web.MockServletContext;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.SessionRepository;
import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices;
import org.springframework.session.web.http.CookieHttpSessionStrategy;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter;
import org.springframework.session.web.http.SessionRepositoryFilter;
@@ -93,11 +93,11 @@ public class SpringHttpSessionConfigurationTests {
SessionRepositoryFilter sessionRepositoryFilter = this.context
.getBean(SessionRepositoryFilter.class);
assertThat(sessionRepositoryFilter).isNotNull();
CookieHttpSessionStrategy httpSessionStrategy = (CookieHttpSessionStrategy) ReflectionTestUtils
.getField(sessionRepositoryFilter, "httpSessionStrategy");
assertThat(httpSessionStrategy).isNotNull();
CookieHttpSessionIdResolver httpSessionIdResolver = (CookieHttpSessionIdResolver) ReflectionTestUtils
.getField(sessionRepositoryFilter, "httpSessionIdResolver");
assertThat(httpSessionIdResolver).isNotNull();
DefaultCookieSerializer cookieSerializer = (DefaultCookieSerializer) ReflectionTestUtils
.getField(httpSessionStrategy, "cookieSerializer");
.getField(httpSessionIdResolver, "cookieSerializer");
assertThat(cookieSerializer).isNotNull();
assertThat(ReflectionTestUtils.getField(cookieSerializer, "cookieName"))
.isEqualTo("test-name");
@@ -116,11 +116,11 @@ public class SpringHttpSessionConfigurationTests {
SessionRepositoryFilter sessionRepositoryFilter = this.context
.getBean(SessionRepositoryFilter.class);
assertThat(sessionRepositoryFilter).isNotNull();
CookieHttpSessionStrategy httpSessionStrategy = (CookieHttpSessionStrategy) ReflectionTestUtils
.getField(sessionRepositoryFilter, "httpSessionStrategy");
assertThat(httpSessionStrategy).isNotNull();
CookieHttpSessionIdResolver httpSessionIdResolver = (CookieHttpSessionIdResolver) ReflectionTestUtils
.getField(sessionRepositoryFilter, "httpSessionIdResolver");
assertThat(httpSessionIdResolver).isNotNull();
DefaultCookieSerializer cookieSerializer = (DefaultCookieSerializer) ReflectionTestUtils
.getField(httpSessionStrategy, "cookieSerializer");
.getField(httpSessionIdResolver, "cookieSerializer");
assertThat(cookieSerializer).isNotNull();
assertThat(ReflectionTestUtils.getField(cookieSerializer,
"rememberMeRequestAttribute")).isEqualTo(

View File

@@ -32,14 +32,14 @@ import org.springframework.session.Session;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CookieHttpSessionStrategy}.
* Tests for {@link CookieHttpSessionIdResolver}.
*/
public class CookieHttpSessionStrategyTests {
public class CookieHttpSessionIdResolverTests {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private CookieHttpSessionStrategy strategy;
private CookieHttpSessionIdResolver strategy;
private String cookieName;
private Session session;
@@ -49,18 +49,18 @@ public class CookieHttpSessionStrategyTests {
this.session = new MapSession();
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.strategy = new CookieHttpSessionStrategy();
this.strategy = new CookieHttpSessionIdResolver();
}
@Test
public void getRequestedSessionIdNull() throws Exception {
assertThat(this.strategy.getRequestedSessionIds(this.request)).isEmpty();
assertThat(this.strategy.resolveSessionIds(this.request)).isEmpty();
}
@Test
public void getRequestedSessionIdNotNull() throws Exception {
setSessionCookie(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
assertThat(this.strategy.resolveSessionIds(this.request))
.isEqualTo(Collections.singletonList(this.session.getId()));
}
@@ -68,20 +68,20 @@ public class CookieHttpSessionStrategyTests {
public void getRequestedSessionIdNotNullCustomCookieName() throws Exception {
setCookieName("CUSTOM");
setSessionCookie(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
assertThat(this.strategy.resolveSessionIds(this.request))
.isEqualTo(Collections.singletonList(this.session.getId()));
}
@Test
public void onNewSession() throws Exception {
this.strategy.onNewSession(this.session, this.request, this.response);
this.strategy.setSessionId(this.request, this.response, this.session.getId());
assertThat(getSessionId()).isEqualTo(this.session.getId());
}
@Test
public void onNewSessionTwiceSameId() throws Exception {
this.strategy.onNewSession(this.session, this.request, this.response);
this.strategy.onNewSession(this.session, this.request, this.response);
this.strategy.setSessionId(this.request, this.response, this.session.getId());
this.strategy.setSessionId(this.request, this.response, this.session.getId());
assertThat(this.response.getCookies()).hasSize(1);
}
@@ -90,8 +90,8 @@ public class CookieHttpSessionStrategyTests {
public void onNewSessionTwiceNewId() throws Exception {
Session newSession = new MapSession();
this.strategy.onNewSession(this.session, this.request, this.response);
this.strategy.onNewSession(newSession, this.request, this.response);
this.strategy.setSessionId(this.request, this.response, this.session.getId());
this.strategy.setSessionId(this.request, this.response, newSession.getId());
Cookie[] cookies = this.response.getCookies();
assertThat(cookies).hasSize(2);
@@ -103,7 +103,7 @@ public class CookieHttpSessionStrategyTests {
@Test
public void onNewSessionCookiePath() throws Exception {
this.request.setContextPath("/somethingunique");
this.strategy.onNewSession(this.session, this.request, this.response);
this.strategy.setSessionId(this.request, this.response, this.session.getId());
Cookie sessionCookie = this.response.getCookie(this.cookieName);
assertThat(sessionCookie.getPath())
@@ -113,20 +113,20 @@ public class CookieHttpSessionStrategyTests {
@Test
public void onNewSessionCustomCookieName() throws Exception {
setCookieName("CUSTOM");
this.strategy.onNewSession(this.session, this.request, this.response);
this.strategy.setSessionId(this.request, this.response, this.session.getId());
assertThat(getSessionId()).isEqualTo(this.session.getId());
}
@Test
public void onDeleteSession() throws Exception {
this.strategy.onInvalidateSession(this.request, this.response);
this.strategy.expireSession(this.request, this.response);
assertThat(getSessionId()).isEmpty();
}
@Test
public void onDeleteSessionCookiePath() throws Exception {
this.request.setContextPath("/somethingunique");
this.strategy.onInvalidateSession(this.request, this.response);
this.strategy.expireSession(this.request, this.response);
Cookie sessionCookie = this.response.getCookie(this.cookieName);
assertThat(sessionCookie.getPath())
@@ -136,7 +136,7 @@ public class CookieHttpSessionStrategyTests {
@Test
public void onDeleteSessionCustomCookieName() throws Exception {
setCookieName("CUSTOM");
this.strategy.onInvalidateSession(this.request, this.response);
this.strategy.expireSession(this.request, this.response);
assertThat(getSessionId()).isEmpty();
}

View File

@@ -29,14 +29,14 @@ import org.springframework.session.Session;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link HeaderHttpSessionStrategy}.
* Tests for {@link HeaderHttpSessionIdResolver}.
*/
public class HeaderSessionStrategyTests {
public class HeaderHttpSessionIdResolverTests {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private HeaderHttpSessionStrategy strategy;
private HeaderHttpSessionIdResolver strategy;
private String headerName;
private Session session;
@@ -46,18 +46,18 @@ public class HeaderSessionStrategyTests {
this.session = new MapSession();
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.strategy = new HeaderHttpSessionStrategy();
this.strategy = new HeaderHttpSessionIdResolver();
}
@Test
public void getRequestedSessionIdNull() throws Exception {
assertThat(this.strategy.getRequestedSessionIds(this.request)).isEmpty();
assertThat(this.strategy.resolveSessionIds(this.request)).isEmpty();
}
@Test
public void getRequestedSessionIdNotNull() throws Exception {
setSessionId(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
assertThat(this.strategy.resolveSessionIds(this.request))
.isEqualTo(Collections.singletonList(this.session.getId()));
}
@@ -65,45 +65,48 @@ public class HeaderSessionStrategyTests {
public void getRequestedSessionIdNotNullCustomHeaderName() throws Exception {
setHeaderName("CUSTOM");
setSessionId(this.session.getId());
assertThat(this.strategy.getRequestedSessionIds(this.request))
assertThat(this.strategy.resolveSessionIds(this.request))
.isEqualTo(Collections.singletonList(this.session.getId()));
}
@Test
public void onNewSession() throws Exception {
this.strategy.onNewSession(this.session, this.request, this.response);
assertThat(getSessionId()).isEqualTo(this.session.getId());
String sessionId = this.session.getId();
this.strategy.setSessionId(this.request, this.response, sessionId);
assertThat(getSessionId()).isEqualTo(sessionId);
}
// the header is set as apposed to added
@Test
public void onNewSessionMulti() throws Exception {
this.strategy.onNewSession(this.session, this.request, this.response);
this.strategy.onNewSession(this.session, this.request, this.response);
String sessionId = this.session.getId();
this.strategy.setSessionId(this.request, this.response, sessionId);
this.strategy.setSessionId(this.request, this.response, sessionId);
assertThat(this.response.getHeaders(this.headerName).size()).isEqualTo(1);
assertThat(this.response.getHeaders(this.headerName))
.containsOnly(this.session.getId());
.containsOnly(sessionId);
}
@Test
public void onNewSessionCustomHeaderName() throws Exception {
setHeaderName("CUSTOM");
this.strategy.onNewSession(this.session, this.request, this.response);
assertThat(getSessionId()).isEqualTo(this.session.getId());
String sessionId = this.session.getId();
this.strategy.setSessionId(this.request, this.response, sessionId);
assertThat(getSessionId()).isEqualTo(sessionId);
}
@Test
public void onDeleteSession() throws Exception {
this.strategy.onInvalidateSession(this.request, this.response);
this.strategy.expireSession(this.request, this.response);
assertThat(getSessionId()).isEmpty();
}
// the header is set as apposed to added
@Test
public void onDeleteSessionMulti() throws Exception {
this.strategy.onInvalidateSession(this.request, this.response);
this.strategy.onInvalidateSession(this.request, this.response);
this.strategy.expireSession(this.request, this.response);
this.strategy.expireSession(this.request, this.response);
assertThat(this.response.getHeaders(this.headerName).size()).isEqualTo(1);
assertThat(getSessionId()).isEmpty();
@@ -112,7 +115,7 @@ public class HeaderSessionStrategyTests {
@Test
public void onDeleteSessionCustomHeaderName() throws Exception {
setHeaderName("CUSTOM");
this.strategy.onInvalidateSession(this.request, this.response);
this.strategy.expireSession(this.request, this.response);
assertThat(getSessionId()).isEmpty();
}

View File

@@ -77,7 +77,7 @@ import static org.mockito.Mockito.verifyZeroInteractions;
public class SessionRepositoryFilterTests {
@Mock
private HttpSessionStrategy strategy;
private HttpSessionIdResolver strategy;
private Map<String, Session> sessions;
@@ -1164,7 +1164,7 @@ public class SessionRepositoryFilterTests {
});
}
// --- HttpSessionStrategy
// --- HttpSessionIdResolver
@Test
public void doFilterAdapterGetRequestedSessionId() throws Exception {
@@ -1172,10 +1172,10 @@ public class SessionRepositoryFilterTests {
new MapSessionRepository(new ConcurrentHashMap<>()));
this.filter = new SessionRepositoryFilter<>(sessionRepository);
this.filter.setHttpSessionStrategy(this.strategy);
final String expectedId = "HttpSessionStrategy-requested-id";
this.filter.setHttpSessionIdResolver(this.strategy);
final String expectedId = "HttpSessionIdResolver-requested-id";
given(this.strategy.getRequestedSessionIds(any(HttpServletRequest.class)))
given(this.strategy.resolveSessionIds(any(HttpServletRequest.class)))
.willReturn(Collections.singletonList(expectedId));
given(sessionRepository.findById(anyString()))
.willReturn(new MapSession(expectedId));
@@ -1192,7 +1192,7 @@ public class SessionRepositoryFilterTests {
@Test
public void doFilterAdapterOnNewSession() throws Exception {
this.filter.setHttpSessionStrategy(this.strategy);
this.filter.setHttpSessionIdResolver(this.strategy);
doFilter(new DoInFilter() {
@Override
@@ -1204,13 +1204,13 @@ public class SessionRepositoryFilterTests {
HttpServletRequest request = (HttpServletRequest) this.chain.getRequest();
Session session = this.sessionRepository.findById(request.getSession().getId());
verify(this.strategy).onNewSession(eq(session), any(HttpServletRequest.class),
any(HttpServletResponse.class));
verify(this.strategy).setSessionId(any(HttpServletRequest.class),
any(HttpServletResponse.class), eq(session.getId()));
}
@Test
public void doFilterAdapterOnInvalidate() throws Exception {
this.filter.setHttpSessionStrategy(this.strategy);
this.filter.setHttpSessionIdResolver(this.strategy);
doFilter(new DoInFilter() {
@Override
@@ -1222,7 +1222,7 @@ public class SessionRepositoryFilterTests {
HttpServletRequest request = (HttpServletRequest) this.chain.getRequest();
String id = request.getSession().getId();
given(this.strategy.getRequestedSessionIds(any(HttpServletRequest.class)))
given(this.strategy.resolveSessionIds(any(HttpServletRequest.class)))
.willReturn(Collections.singletonList(id));
setupRequest();
@@ -1234,7 +1234,7 @@ public class SessionRepositoryFilterTests {
}
});
verify(this.strategy).onInvalidateSession(any(HttpServletRequest.class),
verify(this.strategy).expireSession(any(HttpServletRequest.class),
any(HttpServletResponse.class));
}
@@ -1242,7 +1242,7 @@ public class SessionRepositoryFilterTests {
@Test
public void doFilterRequestSessionNoRequestSessionDoesNotInvalidate()
throws Exception {
this.filter.setHttpSessionStrategy(this.strategy);
this.filter.setHttpSessionIdResolver(this.strategy);
doFilter(new DoInFilter() {
@Override
@@ -1254,7 +1254,7 @@ public class SessionRepositoryFilterTests {
HttpServletRequest request = (HttpServletRequest) this.chain.getRequest();
String id = request.getSession().getId();
given(this.strategy.getRequestedSessionIds(any(HttpServletRequest.class)))
given(this.strategy.resolveSessionIds(any(HttpServletRequest.class)))
.willReturn(Collections.singletonList(id));
doFilter(new DoInFilter() {
@@ -1264,7 +1264,7 @@ public class SessionRepositoryFilterTests {
}
});
verify(this.strategy, never()).onInvalidateSession(any(HttpServletRequest.class),
verify(this.strategy, never()).expireSession(any(HttpServletRequest.class),
any(HttpServletResponse.class));
}
@@ -1350,8 +1350,8 @@ public class SessionRepositoryFilterTests {
}
@Test(expected = IllegalArgumentException.class)
public void setHttpSessionStrategyNull() {
this.filter.setHttpSessionStrategy(null);
public void setHttpSessionIdResolverNull() {
this.filter.setHttpSessionIdResolver(null);
}
// --- helper methods