Introduce SessionIdGenerationStrategy
Closes gh-11
This commit is contained in:
committed by
Marcus Hert Da Coregio
parent
2e1275333a
commit
d547b33962
@@ -34,8 +34,10 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.session.FindByIndexNameSessionRepository;
|
||||
import org.springframework.session.MapSession;
|
||||
import org.springframework.session.SessionIdGenerationStrategy;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -209,4 +211,52 @@ public class MongoIndexedSessionRepositoryTest {
|
||||
assertThat(sessionsMap).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSessionWhenSessionIdGenerationStrategyThenUses() {
|
||||
this.repository.setSessionIdGenerationStrategy(new FixedSessionIdGenerationStrategy("123"));
|
||||
MongoSession session = this.repository.createSession();
|
||||
assertThat(session.getId()).isEqualTo("123");
|
||||
assertThat(session.changeSessionId()).isEqualTo("123");
|
||||
}
|
||||
|
||||
@Test
|
||||
void setSessionIdGenerationStrategyWhenNullThenThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setSessionIdGenerationStrategy(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByIdWhenChangeSessionIdThenUsesSessionIdGenerationStrategy() {
|
||||
this.repository.setSessionIdGenerationStrategy(new FixedSessionIdGenerationStrategy("456"));
|
||||
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById("123", Document.class,
|
||||
MongoIndexedSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(sessionDocument);
|
||||
|
||||
MongoSession session = new MongoSession("123");
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
|
||||
MongoSession retrievedSession = this.repository.findById("123");
|
||||
assertThat(retrievedSession.getId()).isEqualTo("123");
|
||||
String newSessionId = retrievedSession.changeSessionId();
|
||||
assertThat(newSessionId).isEqualTo("456");
|
||||
}
|
||||
|
||||
static class FixedSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
|
||||
|
||||
private final String id;
|
||||
|
||||
FixedSessionIdGenerationStrategy(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generate() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.springframework.session.MapSession;
|
||||
import org.springframework.session.events.SessionDeletedEvent;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.BDDMockito.any;
|
||||
import static org.mockito.BDDMockito.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@@ -210,4 +211,43 @@ public class ReactiveMongoSessionRepositoryTest {
|
||||
verify(this.converter, times(1)).ensureIndexes(indexOperations);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSessionWhenSessionIdGenerationStrategyThenUses() {
|
||||
this.repository.setSessionIdGenerationStrategy(() -> "test");
|
||||
|
||||
this.repository.createSession().as(StepVerifier::create).assertNext((mongoSession) -> {
|
||||
assertThat(mongoSession.getId()).isEqualTo("test");
|
||||
assertThat(mongoSession.changeSessionId()).isEqualTo("test");
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setSessionIdGenerationStrategyWhenNullThenThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.repository.setSessionIdGenerationStrategy(null))
|
||||
.withMessage("sessionIdGenerationStrategy cannot be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByIdWhenChangeSessionIdThenUsesSessionIdGenerationStrategy() {
|
||||
this.repository.setSessionIdGenerationStrategy(() -> "test");
|
||||
|
||||
String sessionId = UUID.randomUUID().toString();
|
||||
Document sessionDocument = new Document();
|
||||
|
||||
given(this.mongoOperations.findById(sessionId, Document.class,
|
||||
ReactiveMongoSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
|
||||
|
||||
MongoSession session = new MongoSession(sessionId);
|
||||
|
||||
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
|
||||
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
|
||||
|
||||
this.repository.findById(sessionId).as(StepVerifier::create).assertNext((mongoSession) -> {
|
||||
String oldId = mongoSession.getId();
|
||||
String newId = mongoSession.changeSessionId();
|
||||
assertThat(oldId).isEqualTo(sessionId);
|
||||
assertThat(newId).isEqualTo("test");
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.session.IndexResolver;
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.SessionIdGenerationStrategy;
|
||||
import org.springframework.session.UuidSessionIdGenerationStrategy;
|
||||
import org.springframework.session.config.SessionRepositoryCustomizer;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
import org.springframework.session.data.mongo.JacksonMongoSessionConverter;
|
||||
@@ -200,6 +202,22 @@ public class MongoHttpSessionConfigurationTest {
|
||||
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerWhenSessionIdGenerationStrategyBeanThenUses() {
|
||||
registerAndRefresh(SessionIdGenerationStrategyConfiguration.class);
|
||||
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
|
||||
.isInstanceOf(TestSessionIdGenerationStrategy.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerWhenNoSessionIdGenerationStrategyBeanThenDefault() {
|
||||
registerAndRefresh(DefaultConfiguration.class);
|
||||
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
|
||||
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
|
||||
.isInstanceOf(UuidSessionIdGenerationStrategy.class);
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||
|
||||
this.context.register(annotatedClasses);
|
||||
@@ -350,4 +368,25 @@ public class MongoHttpSessionConfigurationTest {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableMongoHttpSession
|
||||
@Import(MongoConfiguration.class)
|
||||
static class SessionIdGenerationStrategyConfiguration {
|
||||
|
||||
@Bean
|
||||
SessionIdGenerationStrategy sessionIdGenerationStrategy() {
|
||||
return new TestSessionIdGenerationStrategy();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
|
||||
|
||||
@Override
|
||||
public String generate() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ import org.springframework.data.mongodb.core.index.IndexOperations;
|
||||
import org.springframework.session.IndexResolver;
|
||||
import org.springframework.session.ReactiveSessionRepository;
|
||||
import org.springframework.session.Session;
|
||||
import org.springframework.session.SessionIdGenerationStrategy;
|
||||
import org.springframework.session.UuidSessionIdGenerationStrategy;
|
||||
import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
|
||||
import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;
|
||||
import org.springframework.session.data.mongo.AbstractMongoSessionConverter;
|
||||
@@ -222,6 +224,28 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerWhenSessionIdGenerationStrategyBeanThenUses() {
|
||||
registerAndRefresh(GoodConfig.class, SessionIdGenerationStrategyConfiguration.class);
|
||||
ReactiveMongoSessionRepository sessionRepository = this.context.getBean(ReactiveMongoSessionRepository.class);
|
||||
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
|
||||
.isInstanceOf(TestSessionIdGenerationStrategy.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerWhenNoSessionIdGenerationStrategyBeanThenDefault() {
|
||||
registerAndRefresh(GoodConfig.class);
|
||||
ReactiveMongoSessionRepository sessionRepository = this.context.getBean(ReactiveMongoSessionRepository.class);
|
||||
assertThat(sessionRepository).extracting("sessionIdGenerationStrategy")
|
||||
.isInstanceOf(UuidSessionIdGenerationStrategy.class);
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(annotatedClasses);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reflectively extract the {@link AbstractMongoSessionConverter} from the
|
||||
* {@link ReactiveMongoSessionRepository}. This is to avoid expanding the surface area
|
||||
@@ -411,4 +435,23 @@ public class ReactiveMongoWebSessionConfigurationTest {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class SessionIdGenerationStrategyConfiguration {
|
||||
|
||||
@Bean
|
||||
SessionIdGenerationStrategy sessionIdGenerationStrategy() {
|
||||
return new TestSessionIdGenerationStrategy();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestSessionIdGenerationStrategy implements SessionIdGenerationStrategy {
|
||||
|
||||
@Override
|
||||
public String generate() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user