diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java index 24bca4a40b..4b292b75c3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -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 springBootSessionRepositoryCustomizer( SessionProperties sessionProperties, JdbcSessionProperties jdbcSessionProperties, ServerProperties serverProperties) { @@ -76,7 +79,6 @@ class JdbcSessionConfiguration { map.from(jdbcSessionProperties::getSaveMode).to(sessionRepository::setSaveMode); map.from(jdbcSessionProperties::getCleanupCron).to(sessionRepository::setCleanupCron); }; - } static class OnJdbcSessionDatasourceInitializationCondition extends OnDatabaseInitializationCondition { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java index 2582325400..621dbc73ca 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 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. @@ -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,23 @@ 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 +307,14 @@ class SessionAutoConfigurationJdbcTests extends AbstractSessionAutoConfiguration } + @Configuration + static class CustomJdbcIndexedSessionRepositoryCustomizerConfiguration { + + @Bean + PostgreSqlJdbcIndexedSessionRepositoryCustomizer postgreSqlJdbcIndexedSessionRepositoryCustomizer() { + return new PostgreSqlJdbcIndexedSessionRepositoryCustomizer(); + } + + } + }