Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix no effect of custom SessionRepositoryCustomizer in application context #33514

Conversation

michaelweidmann
Copy link
Contributor

@michaelweidmann michaelweidmann commented Dec 12, 2022

This PR fixes the issue #33463.

With the new release of Spring Boot v3 the SpringBootJdbcHttpSessionConfiguration was replaced with the SessionRepositoryCustomizer.

@Bean
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> springBootSessionRepositoryCustomizer(
		SessionProperties sessionProperties, JdbcSessionProperties jdbcSessionProperties,
		ServerProperties serverProperties) {
	return (sessionRepository) -> {
		PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
		map.from(sessionProperties.determineTimeout(() -> serverProperties.getServlet().getSession().getTimeout()))
				.to(sessionRepository::setDefaultMaxInactiveInterval);

		map.from(jdbcSessionProperties::getTableName).to(sessionRepository::setTableName); // --> relevant line
		map.from(jdbcSessionProperties::getFlushMode).to(sessionRepository::setFlushMode);
		map.from(jdbcSessionProperties::getSaveMode).to(sessionRepository::setSaveMode);
		map.from(jdbcSessionProperties::getCleanupCron).to(sessionRepository::setCleanupCron);
	};

}

This change led to the linked issue because in the Spring Session project the line in JdbcHttpSessionConfiguration now has more than one sessionRepositoryCustomizers and they are executed in the wrong order.

@Bean
public JdbcIndexedSessionRepository sessionRepository() {
	// ...
	JdbcIndexedSessionRepository sessionRepository = new JdbcIndexedSessionRepository(jdbcTemplate,
			this.transactionOperations);
	// ...
	this.sessionRepositoryCustomizers
			.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(sessionRepository));
	return sessionRepository;
}

By default the first few session repository customizer are the custom defined one's (e.g. a PostgreSqlJdbcIndexedSessionRepositoryCustomizer, it updates a query). After that the springBootSessionRepositoryCustomizer is executed which sets the table name and in return updates all queries to their respective defaults (see here).

public void setTableName(String tableName) {
	Assert.hasText(tableName, "Table name must not be empty");
	this.tableName = tableName.trim();
	prepareQueries(); // --> this line updates all queries to their defaults
}

To fix this the order of the springBootSessionRepositoryCustomizer bean is set to highest precedence so that the bean is the first one in the list.

This fix should be identical to the implementation in Spring Boot 2.7 because in the "old" implementation the logic of springBootSessionRepositoryCustomizer is executed before other customizer's.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 12, 2022
@philwebb philwebb added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 18, 2023
@philwebb philwebb added this to the 3.0.x milestone Jan 18, 2023
philwebb pushed a commit that referenced this pull request Jan 19, 2023
Update `JdbcSessionConfiguration` so the `SessionRepositoryCustomizer`
used to map properties is always applied before other customizers.

See gh-33514
@philwebb philwebb closed this in 7349051 Jan 19, 2023
@philwebb philwebb modified the milestones: 3.0.x, 3.0.2 Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants