Skip to content

Commit 19ce32d

Browse files
michaelweidmannphilwebb
authored andcommitted
Order SessionRepositoryCustomizer before other customizers
Update `JdbcSessionConfiguration` so the `SessionRepositoryCustomizer` used to map properties is always applied before other customizers. See gh-33514
1 parent 8015f28 commit 19ce32d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.context.annotation.Conditional;
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.annotation.Import;
34+
import org.springframework.core.Ordered;
35+
import org.springframework.core.annotation.Order;
3436
import org.springframework.jdbc.core.JdbcTemplate;
3537
import org.springframework.session.SessionRepository;
3638
import org.springframework.session.config.SessionRepositoryCustomizer;
@@ -64,6 +66,7 @@ JdbcSessionDataSourceScriptDatabaseInitializer jdbcSessionDataSourceScriptDataba
6466
}
6567

6668
@Bean
69+
@Order(Ordered.HIGHEST_PRECEDENCE)
6770
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepositoryCustomizer(
6871
SessionProperties sessionProperties, JdbcSessionProperties jdbcSessionProperties,
6972
ServerProperties serverProperties) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
5050
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
5151
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
52+
import org.springframework.session.jdbc.PostgreSqlJdbcIndexedSessionRepositoryCustomizer;
5253
import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
5354

5455
import static org.assertj.core.api.Assertions.assertThat;
@@ -243,6 +244,24 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredJdbcSessi
243244
.hasBean("customInitializer"));
244245
}
245246

247+
@Test
248+
void whenTheUserDefinesTheirOwnJdbcIndexedSessionRepositoryCustomizerThenDefaultConfigurationIsOverwritten() {
249+
String expectedCreateSessionAttributeQuery = """
250+
INSERT INTO SPRING_SESSION_ATTRIBUTES (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES)
251+
VALUES (?, ?, ?)
252+
ON CONFLICT (SESSION_PRIMARY_ID, ATTRIBUTE_NAME)
253+
DO UPDATE SET ATTRIBUTE_BYTES = EXCLUDED.ATTRIBUTE_BYTES
254+
""";
255+
256+
this.contextRunner.withUserConfiguration(CustomJdbcIndexedSessionRepositoryCustomizerConfiguration.class)
257+
.withConfiguration(AutoConfigurations.of(JdbcSessionConfiguration.class)).run((context) -> {
258+
JdbcIndexedSessionRepository repository = validateSessionRepository(context,
259+
JdbcIndexedSessionRepository.class);
260+
assertThat(repository).hasFieldOrPropertyWithValue("createSessionAttributeQuery",
261+
expectedCreateSessionAttributeQuery);
262+
});
263+
}
264+
246265
@Configuration
247266
static class SessionDataSourceConfiguration {
248267

@@ -289,4 +308,14 @@ DataSourceScriptDatabaseInitializer customInitializer(DataSource dataSource) {
289308

290309
}
291310

311+
@Configuration
312+
static class CustomJdbcIndexedSessionRepositoryCustomizerConfiguration {
313+
314+
@Bean
315+
PostgreSqlJdbcIndexedSessionRepositoryCustomizer postgreSqlJdbcIndexedSessionRepositoryCustomizer() {
316+
return new PostgreSqlJdbcIndexedSessionRepositoryCustomizer();
317+
}
318+
319+
}
320+
292321
}

0 commit comments

Comments
 (0)