Order SessionRepositoryCustomizer before other customizers

Update `JdbcSessionConfiguration` so the `SessionRepositoryCustomizer`
used to map properties is always applied before other customizers.

See gh-33514
This commit is contained in:
Michael Weidmann 2022-12-12 17:01:41 +01:00 committed by Phillip Webb
parent 8015f283b2
commit 19ce32dc34
2 changed files with 32 additions and 0 deletions

View File

@ -31,6 +31,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.SessionRepositoryCustomizer;
@ -64,6 +66,7 @@ class JdbcSessionConfiguration {
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepositoryCustomizer(
SessionProperties sessionProperties, JdbcSessionProperties jdbcSessionProperties,
ServerProperties serverProperties) {

View File

@ -49,6 +49,7 @@ import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
import org.springframework.session.jdbc.PostgreSqlJdbcIndexedSessionRepositoryCustomizer;
import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
import static org.assertj.core.api.Assertions.assertThat;
@ -243,6 +244,24 @@ class SessionAutoConfigurationJdbcTests extends AbstractSessionAutoConfiguration
.hasBean("customInitializer"));
}
@Test
void whenTheUserDefinesTheirOwnJdbcIndexedSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() {
String expectedCreateSessionAttributeQuery = """
INSERT INTO SPRING_SESSION_ATTRIBUTES (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES)
VALUES (?, ?, ?)
ON CONFLICT (SESSION_PRIMARY_ID, ATTRIBUTE_NAME)
DO UPDATE SET ATTRIBUTE_BYTES = EXCLUDED.ATTRIBUTE_BYTES
""";
this.contextRunner.withUserConfiguration(CustomJdbcIndexedSessionRepositoryCustomizerConfiguration.class)
.withConfiguration(AutoConfigurations.of(JdbcSessionConfiguration.class)).run((context) -> {
JdbcIndexedSessionRepository repository = validateSessionRepository(context,
JdbcIndexedSessionRepository.class);
assertThat(repository).hasFieldOrPropertyWithValue("createSessionAttributeQuery",
expectedCreateSessionAttributeQuery);
});
}
@Configuration
static class SessionDataSourceConfiguration {
@ -289,4 +308,14 @@ class SessionAutoConfigurationJdbcTests extends AbstractSessionAutoConfiguration
}
@Configuration
static class CustomJdbcIndexedSessionRepositoryCustomizerConfiguration {
@Bean
PostgreSqlJdbcIndexedSessionRepositoryCustomizer postgreSqlJdbcIndexedSessionRepositoryCustomizer() {
return new PostgreSqlJdbcIndexedSessionRepositoryCustomizer();
}
}
}