From 9141177f1a2500c8d2ab0985d37720693d7e70a2 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Sat, 13 Aug 2016 22:29:43 +0200 Subject: [PATCH 1/2] 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 --- .../session/SessionProperties.java | 12 +++++++--- .../SessionAutoConfigurationJdbcTests.java | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java index 5134a91a33..84c9b1e98c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java @@ -107,6 +107,8 @@ public class SessionProperties { private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/" + "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. */ @@ -115,7 +117,7 @@ public class SessionProperties { /** * 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(); @@ -139,7 +141,7 @@ public class SessionProperties { return this.initializer; } - public static class Initializer { + public class Initializer { /** * Create the required session tables on startup if necessary. @@ -147,7 +149,11 @@ public class SessionProperties { private boolean enabled = true; 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) { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java index 5c4477dce4..03745f35cd 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java @@ -54,6 +54,8 @@ public class SessionAutoConfigurationJdbcTests JdbcOperationsSessionRepository.class); assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName")) .isEqualTo("SPRING_SESSION"); + assertThat(this.context.getBean(SessionProperties.class) + .getJdbc().getInitializer().isEnabled()).isTrue(); assertThat(this.context.getBean(JdbcOperations.class) .queryForList("select * from SPRING_SESSION")).isEmpty(); } @@ -68,6 +70,8 @@ public class SessionAutoConfigurationJdbcTests JdbcOperationsSessionRepository.class); assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName")) .isEqualTo("SPRING_SESSION"); + 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(); @@ -84,8 +88,27 @@ public class SessionAutoConfigurationJdbcTests JdbcOperationsSessionRepository.class); assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName")) .isEqualTo("FOO_BAR"); + assertThat(this.context.getBean(SessionProperties.class) + .getJdbc().getInitializer().isEnabled()).isTrue(); assertThat(this.context.getBean(JdbcOperations.class) .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(); + } + } From a347a780e74a195104aa745d556753e5a890abd7 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 12 Sep 2016 14:34:40 +0200 Subject: [PATCH 2/2] Polish contribution Closes gh-6649 --- .../autoconfigure/session/SessionProperties.java | 15 ++++++++++----- .../asciidoc/appendix-application-properties.adoc | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java index 84c9b1e98c..e8ffbf67f6 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java @@ -144,16 +144,21 @@ public class SessionProperties { public class Initializer { /** - * Create the required session tables on startup if necessary. + * Create the required session tables on startup if necessary. Enabled + * automatically if the default table name is set or a custom schema is + * configured. */ - private boolean enabled = true; + private Boolean enabled; public boolean isEnabled() { - boolean isDefaultTableName = DEFAULT_TABLE_NAME.equals( + if (this.enabled != null) { + return this.enabled; + } + boolean defaultTableName = DEFAULT_TABLE_NAME.equals( Jdbc.this.getTableName()); - boolean isDefaultSchema = DEFAULT_SCHEMA_LOCATION.equals( + boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals( Jdbc.this.getSchema()); - return this.enabled && (isDefaultTableName || !isDefaultSchema); + return (defaultTableName || customSchema); } public void setEnabled(boolean enabled) { diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 1045c577c7..8b29e94dd3 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -369,7 +369,7 @@ content into your application; rather pick only the properties that you need. # SPRING SESSION ({sc-spring-boot-autoconfigure}/session/SessionProperties.{sc-ext}[SessionProperties]) spring.session.hazelcast.map-name=spring:session:sessions # Name of the map used to store sessions. - spring.session.jdbc.initializer.enabled=true # Create the required session tables on startup if necessary. + spring.session.jdbc.initializer.enabled= # Create the required session tables on startup if necessary. Enabled automatically if the default table name is set or a custom schema is configured. spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema. spring.session.jdbc.table-name=SPRING_SESSION # Name of database table used to store sessions. spring.session.mongo.collection-name=sessions # Collection name used to store sessions.