diff --git a/spring-session-core/src/main/java/org/springframework/session/SpringWebSessionConfiguration.java b/spring-session-core/src/main/java/org/springframework/session/SpringWebSessionConfiguration.java index a0253521..e8e7210e 100644 --- a/spring-session-core/src/main/java/org/springframework/session/SpringWebSessionConfiguration.java +++ b/spring-session-core/src/main/java/org/springframework/session/SpringWebSessionConfiguration.java @@ -17,14 +17,16 @@ package org.springframework.session; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.session.web.server.session.SpringSessionWebSessionManager; +import org.springframework.session.web.server.session.SpringSessionWebSessionStore; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; +import org.springframework.web.server.session.DefaultWebSessionManager; import org.springframework.web.server.session.WebSessionManager; /** * Wire up a {@link WebSessionManager} using a Reactive {@link ReactorSessionRepository} from the application context. * * @author Greg Turnquist + * @author Rob Winch * @since 2.0 * * @see EnableSpringWebSession @@ -39,7 +41,10 @@ public class SpringWebSessionConfiguration { * @return a configured {@link WebSessionManager} registered with a preconfigured name. */ @Bean(WebHttpHandlerBuilder.WEB_SESSION_MANAGER_BEAN_NAME) - public WebSessionManager webSessionManager(ReactorSessionRepository> repository) { - return new SpringSessionWebSessionManager(repository); + public WebSessionManager webSessionManager(ReactorSessionRepository extends Session> repository) { + SpringSessionWebSessionStore extends Session> sessionStore = new SpringSessionWebSessionStore<>(repository); + DefaultWebSessionManager manager = new DefaultWebSessionManager(); + manager.setSessionStore(sessionStore); + return manager; } } diff --git a/spring-session-core/src/main/java/org/springframework/session/web/server/session/SpringSessionWebSessionManager.java b/spring-session-core/src/main/java/org/springframework/session/web/server/session/SpringSessionWebSessionManager.java deleted file mode 100644 index 2a55905b..00000000 --- a/spring-session-core/src/main/java/org/springframework/session/web/server/session/SpringSessionWebSessionManager.java +++ /dev/null @@ -1,173 +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.server.session; - -import java.time.Clock; -import java.time.Instant; -import java.time.ZoneOffset; -import java.util.List; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import org.springframework.session.ReactorSessionRepository; -import org.springframework.session.Session; -import org.springframework.util.Assert; -import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.server.WebSession; -import org.springframework.web.server.session.CookieWebSessionIdResolver; -import org.springframework.web.server.session.WebSessionIdResolver; -import org.springframework.web.server.session.WebSessionManager; -import org.springframework.web.server.session.WebSessionStore; - -/** - * The {@link WebSessionManager} implementation backed by - * {@link ReactorSessionRepository}. - * - * @author Rob Winch - * @since 2.0 - */ -public class SpringSessionWebSessionManager implements WebSessionManager { - - private final SpringSessionWebSessionStore extends Session> sessionStore; - - private WebSessionIdResolver sessionIdResolver = new CookieWebSessionIdResolver(); - - private Clock clock = Clock.system(ZoneOffset.UTC); - - public SpringSessionWebSessionManager( - ReactorSessionRepository extends Session> sessionRepository) { - this.sessionStore = new SpringSessionWebSessionStore<>(sessionRepository); - } - - /** - * Return the configured {@link WebSessionIdResolver}. - * @return the configured {@link WebSessionIdResolver} - */ - private WebSessionIdResolver getSessionIdResolver() { - return this.sessionIdResolver; - } - - /** - * Configure the id resolution strategy. - *
- * By default an instance of {@link CookieWebSessionIdResolver}. - * @param sessionIdResolver the resolver to use - */ - public void setSessionIdResolver(WebSessionIdResolver sessionIdResolver) { - Assert.notNull(sessionIdResolver, "WebSessionIdResolver is required."); - this.sessionIdResolver = sessionIdResolver; - } - - /** - * Return the configured {@link WebSessionStore}. - * @return the configured {@link WebSessionStore} - */ - private WebSessionStore getSessionStore() { - return this.sessionStore; - } - - /** - * Return the configured clock for session {@code lastAccessTime} calculations. - * @return the configured clock for session {@code lastAccessTime} calculations - */ - private Clock getClock() { - return this.clock; - } - - /** - * Configure the {@link Clock} to use to set lastAccessTime on every created session - * and to calculate if it is expired. - *
- * This may be useful to align to different timezone or to set the clock back in a - * test, e.g. {@code Clock.offset(clock, Duration.ofMinutes(-31))} in order to - * simulate session expiration. - *
- * By default this is {@code Clock.system(ZoneOffset.UTC)}.
- * @param clock the clock to use
- */
- public void setClock(Clock clock) {
- Assert.notNull(clock, "'clock' is required.");
- this.clock = clock;
- }
-
- @Override
- public Mono This may be useful to align to different timezone or to set the clock
+ * back in a test, e.g. {@code Clock.offset(clock, Duration.ofMinutes(-31))}
+ * in order to simulate session expiration.
+ * By default this is {@code Clock.system(ZoneId.of("GMT"))}.
+ * @param clock the clock to use
+ */
+ public void setClock(Clock clock) {
+ Assert.notNull(clock, "clock cannot be null");
+ this.clock = clock;
}
- public Mono implements WebSessionStore {
+public class SpringSessionWebSessionStore implements WebSessionStore {
private final ReactorSessionRepository sessions;
- SpringSessionWebSessionStore(ReactorSessionRepository sessions) {
- Assert.notNull(sessions, "sessions cannot be null");
- this.sessions = sessions;
+ private Clock clock = Clock.system(ZoneOffset.UTC);
+
+ public SpringSessionWebSessionStore(ReactorSessionRepository reactorSessionRepository) {
+ Assert.notNull(reactorSessionRepository, "reactorSessionRepository cannot be null");
+ this.sessions = reactorSessionRepository;
}
- public Mono implements WebSessionStore
@Override
public Mono implements WebSessionStore
private AtomicReference implements WebSessionStore
@Override
public Mono implements WebSessionStore
@Override
public Mono {
-
- @Mock
- private ReactorSessionRepository sessions;
-
- @Mock
- private WebSessionIdResolver resolver;
-
- @Mock
- private ServerCodecConfigurer serverCodecConfigurer;
-
- @Mock
- private LocaleContextResolver localeContextResolver;
-
- @Mock
- private S createSession;
-
- @Mock
- private S findByIdSession;
-
- private Mono createSessionMono;
-
- private ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
-
- private SpringSessionWebSessionManager manager;
-
- @Before
- public void setup() {
- given(this.createSession.getId()).willReturn("createSession-id");
- given(this.findByIdSession.getId()).willReturn("findByIdSession-id");
- this.createSessionMono = Mono.just(this.createSession);
- given(this.sessions.createSession()).willReturn(this.createSessionMono);
- this.manager = new SpringSessionWebSessionManager(this.sessions);
- this.manager.setSessionIdResolver(this.resolver);
- }
-
- @Test
- public void getSessionWhenDefaultSessionIdResolverFoundSessionUsed() {
- String findByIdSessionId = this.findByIdSession.getId();
- this.exchange = MockServerHttpRequest.get("/")
- .cookie(new HttpCookie("SESSION", findByIdSessionId)).toExchange();
- this.manager = new SpringSessionWebSessionManager(this.sessions);
- given(this.sessions.findById(findByIdSessionId))
- .willReturn(Mono.just(this.findByIdSession));
-
- WebSession webSession = this.manager.getSession(this.exchange).block();
-
- assertThat(webSession.getId()).isEqualTo(findByIdSessionId);
- verify(this.sessions).findById(findByIdSessionId);
- }
-
- @Test
- public void getSessionWhenNewThenCreateSessionInvoked() {
- WebSession webSession = this.manager.getSession(this.exchange).block();
-
- assertThat(webSession.getId()).isEqualTo(this.createSession.getId());
- verify(this.sessions).createSession();
- }
-
- @Test
- public void getSessionWhenNewAndPutThenSetAttributeInvoked() {
- String attrName = "attrName";
- String attrValue = "attrValue";
-
- WebSession webSession = this.manager.getSession(this.exchange).block();
- webSession.getAttributes().put(attrName, attrValue);
-
- verify(this.createSession).setAttribute(attrName, attrValue);
- }
-
- @Test
- public void getSessionWhenInvalidIdThenCreateSessionInvoked() {
- String invalidId = "invalid";
- String createSessionId = this.createSession.getId();
- given(this.sessions.findById(any())).willReturn(Mono.empty());
- given(this.resolver.resolveSessionIds(this.exchange))
- .willReturn(Collections.singletonList(invalidId));
-
- WebSession webSession = this.manager.getSession(this.exchange).block();
-
- assertThat(webSession.getId()).isEqualTo(createSessionId);
- verify(this.sessions).findById(invalidId);
-
- Mono {
@Mock
private S findByIdSession;
- private Function webSessionStore;
@Before
@@ -76,7 +73,7 @@ public class SpringSessionWebSessionStoreTests {
@Test
public void createSessionWhenNoAttributesThenNotStarted() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
assertThat(createdWebSession.isStarted()).isFalse();
@@ -86,7 +83,7 @@ public class SpringSessionWebSessionStoreTests {
public void createSessionWhenAddAttributeThenStarted() {
given(this.createSession.getAttributeNames())
.willReturn(Collections.singleton("a"));
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
assertThat(createdWebSession.isStarted()).isTrue();
@@ -94,7 +91,7 @@ public class SpringSessionWebSessionStoreTests {
@Test
public void createSessionWhenGetAttributesAndSizeThenDelegatesToCreateSession() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void createSessionWhenGetAttributesAndIsEmptyThenDelegatesToCreateSession() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void createSessionWhenGetAttributesAndContainsKeyAndNotStringThenFalse() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void createSessionWhenGetAttributesAndContainsKeyAndNotFoundThenFalse() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
public void createSessionWhenGetAttributesAndContainsKeyAndFoundThenTrue() {
given(this.createSession.getAttributeNames())
.willReturn(Collections.singleton("a"));
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void createSessionWhenGetAttributesAndPutThenDelegatesToCreateSession() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void createSessionWhenGetAttributesAndPutNullThenDelegatesToCreateSession() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void createSessionWhenGetAttributesAndRemoveThenDelegatesToCreateSession() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void createSessionWhenGetAttributesAndPutAllThenDelegatesToCreateSession() {
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
public void createSessionWhenGetAttributesAndClearThenDelegatesToCreateSession() {
given(this.createSession.getAttributeNames())
.willReturn(Collections.singleton("a"));
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
public void createSessionWhenGetAttributesAndKeySetThenDelegatesToCreateSession() {
given(this.createSession.getAttributeNames())
.willReturn(Collections.singleton("a"));
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
given(this.createSession.getAttributeNames())
.willReturn(Collections.singleton("a"));
given(this.createSession.getAttribute("a")).willReturn("b");
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
.willReturn(Collections.singleton(attrName));
String attrValue = "attrValue";
given(this.createSession.getAttribute(attrName)).willReturn(attrValue);
- WebSession createdWebSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdWebSession = this.webSessionStore.createWebSession()
.block();
Map {
@Test
public void storeSessionWhenInvokedThenSessionSaved() {
given(this.sessionRepository.save(this.createSession)).willReturn(Mono.empty());
- WebSession createdSession = this.webSessionStore.createSession(this.saveOperation)
+ WebSession createdSession = this.webSessionStore.createWebSession()
.block();
this.webSessionStore.storeSession(createdSession).block();
@@ -268,7 +265,7 @@ public class SpringSessionWebSessionStoreTests {
public void retrieveSessionThenStarted() {
String id = "id";
WebSession retrievedWebSession = this.webSessionStore
- .retrieveSession(id, this.saveOperation).block();
+ .retrieveSession(id).block();
assertThat(retrievedWebSession.isStarted()).isTrue();
}
@@ -283,4 +280,8 @@ public class SpringSessionWebSessionStoreTests {
verify(this.sessionRepository).delete(sessionId);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void setClockWhenNullThenException() {
+ this.webSessionStore.setClock(null);
+ }
}