Add support for configuring custom IndexResolver

See: #1467
This commit is contained in:
Vedran Pavic
2019-09-26 22:18:37 +02:00
parent b357a76ce3
commit feaf8780a8
9 changed files with 144 additions and 13 deletions

View File

@@ -198,8 +198,6 @@ public class JdbcIndexedSessionRepository
private final ResultSetExtractor<List<JdbcSession>> extractor = new SessionResultSetExtractor();
private final IndexResolver<JdbcSession> indexResolver;
/**
* The name of database table used by Spring Session to store sessions.
*/
@@ -229,9 +227,11 @@ public class JdbcIndexedSessionRepository
*/
private Integer defaultMaxInactiveInterval;
private ConversionService conversionService;
private IndexResolver<Session> indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());
private LobHandler lobHandler;
private ConversionService conversionService = createDefaultConversionService();
private LobHandler lobHandler = new DefaultLobHandler();
private FlushMode flushMode = FlushMode.ON_SAVE;
@@ -248,9 +248,6 @@ public class JdbcIndexedSessionRepository
Assert.notNull(transactionOperations, "transactionOperations must not be null");
this.jdbcOperations = jdbcOperations;
this.transactionOperations = transactionOperations;
this.indexResolver = new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>());
this.conversionService = createDefaultConversionService();
this.lobHandler = new DefaultLobHandler();
prepareQueries();
}
@@ -355,6 +352,15 @@ public class JdbcIndexedSessionRepository
this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
}
/**
* Set the {@link IndexResolver} to use.
* @param indexResolver the index resolver
*/
public void setIndexResolver(IndexResolver<Session> indexResolver) {
Assert.notNull(indexResolver, "indexResolver cannot be null");
this.indexResolver = indexResolver;
}
public void setLobHandler(LobHandler lobHandler) {
Assert.notNull(lobHandler, "LobHandler must not be null");
this.lobHandler = lobHandler;

View File

@@ -45,8 +45,10 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.MapSession;
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
@@ -95,6 +97,8 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
private TransactionOperations transactionOperations;
private IndexResolver<Session> indexResolver;
private LobHandler lobHandler;
private ConversionService springSessionConversionService;
@@ -121,6 +125,9 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds);
sessionRepository.setFlushMode(this.flushMode);
sessionRepository.setSaveMode(this.saveMode);
if (this.indexResolver != null) {
sessionRepository.setIndexResolver(this.indexResolver);
}
if (this.lobHandler != null) {
sessionRepository.setLobHandler(this.lobHandler);
}
@@ -194,6 +201,11 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
this.transactionOperations = transactionOperations;
}
@Autowired(required = false)
public void setIndexResolver(IndexResolver<Session> indexResolver) {
this.indexResolver = indexResolver;
}
@Autowired(required = false)
@Qualifier("springSessionLobHandler")
public void setLobHandler(LobHandler lobHandler) {

View File

@@ -33,7 +33,9 @@ import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.session.FlushMode;
import org.springframework.session.IndexResolver;
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
@@ -242,6 +244,17 @@ class JdbcHttpSessionConfigurationTests {
assertThat(repository).hasFieldOrPropertyWithValue("transactionOperations", transactionOperations);
}
@Test
void customIndexResolverConfiguration() {
registerAndRefresh(DataSourceConfiguration.class, CustomIndexResolverConfiguration.class);
JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class);
@SuppressWarnings("unchecked")
IndexResolver<Session> indexResolver = this.context.getBean(IndexResolver.class);
assertThat(repository).isNotNull();
assertThat(indexResolver).isNotNull();
assertThat(repository).hasFieldOrPropertyWithValue("indexResolver", indexResolver);
}
@Test
void customLobHandlerConfiguration() {
registerAndRefresh(DataSourceConfiguration.class, CustomLobHandlerConfiguration.class);
@@ -452,6 +465,17 @@ class JdbcHttpSessionConfigurationTests {
}
@EnableJdbcHttpSession
static class CustomIndexResolverConfiguration {
@Bean
@SuppressWarnings("unchecked")
public IndexResolver<Session> indexResolver() {
return mock(IndexResolver.class);
}
}
@EnableJdbcHttpSession
static class CustomLobHandlerConfiguration {