Validate Spring Session database initializer configuration
This commit adds Spring Session JDBC configuration validation that disables database initializer in case custom table name is configured with default schema. See gh-6649
This commit is contained in:
parent
07b2fe1d67
commit
9141177f1a
|
|
@ -107,6 +107,8 @@ public class SessionProperties {
|
||||||
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
|
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
|
||||||
+ "session/jdbc/schema-@@platform@@.sql";
|
+ "session/jdbc/schema-@@platform@@.sql";
|
||||||
|
|
||||||
|
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to the SQL file to use to initialize the database schema.
|
* Path to the SQL file to use to initialize the database schema.
|
||||||
*/
|
*/
|
||||||
|
|
@ -115,7 +117,7 @@ public class SessionProperties {
|
||||||
/**
|
/**
|
||||||
* Name of database table used to store sessions.
|
* Name of database table used to store sessions.
|
||||||
*/
|
*/
|
||||||
private String tableName = "SPRING_SESSION";
|
private String tableName = DEFAULT_TABLE_NAME;
|
||||||
|
|
||||||
private final Initializer initializer = new Initializer();
|
private final Initializer initializer = new Initializer();
|
||||||
|
|
||||||
|
|
@ -139,7 +141,7 @@ public class SessionProperties {
|
||||||
return this.initializer;
|
return this.initializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Initializer {
|
public class Initializer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the required session tables on startup if necessary.
|
* Create the required session tables on startup if necessary.
|
||||||
|
|
@ -147,7 +149,11 @@ public class SessionProperties {
|
||||||
private boolean enabled = true;
|
private boolean enabled = true;
|
||||||
|
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return this.enabled;
|
boolean isDefaultTableName = DEFAULT_TABLE_NAME.equals(
|
||||||
|
Jdbc.this.getTableName());
|
||||||
|
boolean isDefaultSchema = DEFAULT_SCHEMA_LOCATION.equals(
|
||||||
|
Jdbc.this.getSchema());
|
||||||
|
return this.enabled && (isDefaultTableName || !isDefaultSchema);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEnabled(boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,8 @@ public class SessionAutoConfigurationJdbcTests
|
||||||
JdbcOperationsSessionRepository.class);
|
JdbcOperationsSessionRepository.class);
|
||||||
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
||||||
.isEqualTo("SPRING_SESSION");
|
.isEqualTo("SPRING_SESSION");
|
||||||
|
assertThat(this.context.getBean(SessionProperties.class)
|
||||||
|
.getJdbc().getInitializer().isEnabled()).isTrue();
|
||||||
assertThat(this.context.getBean(JdbcOperations.class)
|
assertThat(this.context.getBean(JdbcOperations.class)
|
||||||
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
@ -68,6 +70,8 @@ public class SessionAutoConfigurationJdbcTests
|
||||||
JdbcOperationsSessionRepository.class);
|
JdbcOperationsSessionRepository.class);
|
||||||
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
||||||
.isEqualTo("SPRING_SESSION");
|
.isEqualTo("SPRING_SESSION");
|
||||||
|
assertThat(this.context.getBean(SessionProperties.class)
|
||||||
|
.getJdbc().getInitializer().isEnabled()).isFalse();
|
||||||
this.thrown.expect(BadSqlGrammarException.class);
|
this.thrown.expect(BadSqlGrammarException.class);
|
||||||
assertThat(this.context.getBean(JdbcOperations.class)
|
assertThat(this.context.getBean(JdbcOperations.class)
|
||||||
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
||||||
|
|
@ -84,8 +88,27 @@ public class SessionAutoConfigurationJdbcTests
|
||||||
JdbcOperationsSessionRepository.class);
|
JdbcOperationsSessionRepository.class);
|
||||||
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
||||||
.isEqualTo("FOO_BAR");
|
.isEqualTo("FOO_BAR");
|
||||||
|
assertThat(this.context.getBean(SessionProperties.class)
|
||||||
|
.getJdbc().getInitializer().isEnabled()).isTrue();
|
||||||
assertThat(this.context.getBean(JdbcOperations.class)
|
assertThat(this.context.getBean(JdbcOperations.class)
|
||||||
.queryForList("select * from FOO_BAR")).isEmpty();
|
.queryForList("select * from FOO_BAR")).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void customTableNameWithDefaultSchemaDisablesInitializer() {
|
||||||
|
load(Arrays.asList(EmbeddedDataSourceConfiguration.class,
|
||||||
|
DataSourceTransactionManagerAutoConfiguration.class),
|
||||||
|
"spring.session.store-type=jdbc",
|
||||||
|
"spring.session.jdbc.table-name=FOO_BAR");
|
||||||
|
JdbcOperationsSessionRepository repository = validateSessionRepository(
|
||||||
|
JdbcOperationsSessionRepository.class);
|
||||||
|
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
||||||
|
.isEqualTo("FOO_BAR");
|
||||||
|
assertThat(this.context.getBean(SessionProperties.class)
|
||||||
|
.getJdbc().getInitializer().isEnabled()).isFalse();
|
||||||
|
this.thrown.expect(BadSqlGrammarException.class);
|
||||||
|
assertThat(this.context.getBean(JdbcOperations.class)
|
||||||
|
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue