Merge pull request #33514 from michaelweidmann

* pr/33514:
  Polish 'Order SessionRepositoryCustomizer before other customizers'
  Order SessionRepositoryCustomizer before other customizers

Closes gh-33514
This commit is contained in:
Phillip Webb
2023-01-19 12:49:16 -08:00
2 changed files with 33 additions and 3 deletions

View File

@@ -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<JdbcIndexedSessionRepository> 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 {

View File

@@ -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();
}
}
}