From ec8b94f13cdd8e31bf8de7489eba26438b352ab5 Mon Sep 17 00:00:00 2001 From: Marten Deinum Date: Tue, 15 Mar 2016 13:50:30 +0100 Subject: [PATCH 1/2] Support setting webAllowOthers for the H2 Web Console This commit adds a configuration option for the webAllowOthers option for the H2 WebServlet. It will only be added it the spring.h2.console.webAllowOthers is set to true, else it will be ignored. Closes gh-5416 --- .../h2/H2ConsoleAutoConfiguration.java | 6 +++++- .../autoconfigure/h2/H2ConsoleProperties.java | 12 ++++++++++++ .../h2/H2ConsoleAutoConfigurationTests.java | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index f96b8ebd2c3..054804d1d0f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -61,7 +61,11 @@ public class H2ConsoleAutoConfiguration { public ServletRegistrationBean h2Console() { String path = this.properties.getPath(); String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*"); - return new ServletRegistrationBean(new WebServlet(), urlMapping); + ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet(), urlMapping); + if (properties.getWebAllowOthers()) { + registration.addInitParameter("webAllowOthers", "true"); + } + return registration; } @Configuration diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java index 864f5709a21..4f8b24f85f3 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java @@ -42,6 +42,11 @@ public class H2ConsoleProperties { */ private boolean enabled = false; + /** + * Allow remote access. + */ + private boolean webAllowOthers = false; + public String getPath() { return this.path; } @@ -58,4 +63,11 @@ public class H2ConsoleProperties { this.enabled = enabled; } + public boolean getWebAllowOthers() { + return webAllowOthers; + } + + public void setWebAllowOthers(boolean webAllowOthers) { + this.webAllowOthers = webAllowOthers; + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index fdddd2e821f..61ef175e9b9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -25,6 +25,7 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.annotation.Bean; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -70,6 +71,8 @@ public class H2ConsoleAutoConfigurationTests { assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings()) .contains("/h2-console/*"); + assertThat(this.context.getBean(ServletRegistrationBean.class).getInitParameters()). + doesNotContainKey("webAllowOthers"); } @Test @@ -104,4 +107,18 @@ public class H2ConsoleAutoConfigurationTests { .contains("/custom/*"); } + @Test + public void propertySetsWebAllowOthersInitParameter() { + this.context.register(H2ConsoleAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.h2.console.enabled:true", "spring.h2.console.web-allow-others=true"); + this.context.refresh(); + assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); + assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings()) + .contains("/h2-console/*"); + assertThat(this.context.getBean(ServletRegistrationBean.class).getInitParameters()). + containsEntry("webAllowOthers", "true"); + + } + } From bca83bde5b5d45ef6f12023de3585453244cb1c7 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 18 Mar 2016 13:05:44 +0100 Subject: [PATCH 2/2] Polish contribution Closes gh-5417 --- .../h2/H2ConsoleAutoConfiguration.java | 10 ++++- .../autoconfigure/h2/H2ConsoleProperties.java | 43 +++++++++++++++---- .../h2/H2ConsoleAutoConfigurationTests.java | 16 ++++--- .../appendix-application-properties.adoc | 2 + 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index 054804d1d0f..eb852a8922c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -41,6 +41,8 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur * {@link EnableAutoConfiguration Auto-configuration} for H2's web console. * * @author Andy Wilkinson + * @author Marten Deinum + * @author Stephane Nicoll * @since 1.3.0 */ @Configuration @@ -62,8 +64,12 @@ public class H2ConsoleAutoConfiguration { String path = this.properties.getPath(); String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*"); ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet(), urlMapping); - if (properties.getWebAllowOthers()) { - registration.addInitParameter("webAllowOthers", "true"); + H2ConsoleProperties.Settings settings = this.properties.getSettings(); + if (settings.isTrace()) { + registration.addInitParameter("trace", ""); + } + if (settings.isWebAllowOthers()) { + registration.addInitParameter("webAllowOthers", ""); } return registration; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java index 4f8b24f85f3..b4cf1b6cc05 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 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. @@ -25,6 +25,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * Configuration properties for H2's console. * * @author Andy Wilkinson + * @author Marten Deinum + * @author Stephane Nicoll * @since 1.3.0 */ @ConfigurationProperties(prefix = "spring.h2.console") @@ -42,10 +44,7 @@ public class H2ConsoleProperties { */ private boolean enabled = false; - /** - * Allow remote access. - */ - private boolean webAllowOthers = false; + private final Settings settings = new Settings(); public String getPath() { return this.path; @@ -63,11 +62,37 @@ public class H2ConsoleProperties { this.enabled = enabled; } - public boolean getWebAllowOthers() { - return webAllowOthers; + public Settings getSettings() { + return this.settings; } - public void setWebAllowOthers(boolean webAllowOthers) { - this.webAllowOthers = webAllowOthers; + public static class Settings { + + /** + * Enable trace output. + */ + private boolean trace = false; + + /** + * Enable remote access. + */ + private boolean webAllowOthers = false; + + public boolean isTrace() { + return this.trace; + } + + public void setTrace(boolean trace) { + this.trace = trace; + } + + public boolean isWebAllowOthers() { + return this.webAllowOthers; + } + + public void setWebAllowOthers(boolean webAllowOthers) { + this.webAllowOthers = webAllowOthers; + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index 61ef175e9b9..f64ba7f8cb8 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -25,7 +25,6 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.test.EnvironmentTestUtils; -import org.springframework.context.annotation.Bean; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -35,6 +34,8 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link H2ConsoleAutoConfiguration} * * @author Andy Wilkinson + * @author Marten Deinum + * @author Stephane Nicoll */ public class H2ConsoleAutoConfigurationTests { @@ -71,6 +72,8 @@ public class H2ConsoleAutoConfigurationTests { assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings()) .contains("/h2-console/*"); + assertThat(this.context.getBean(ServletRegistrationBean.class).getInitParameters()). + doesNotContainKey("trace"); assertThat(this.context.getBean(ServletRegistrationBean.class).getInitParameters()). doesNotContainKey("webAllowOthers"); } @@ -108,17 +111,20 @@ public class H2ConsoleAutoConfigurationTests { } @Test - public void propertySetsWebAllowOthersInitParameter() { + public void customInitParameters() { this.context.register(H2ConsoleAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, - "spring.h2.console.enabled:true", "spring.h2.console.web-allow-others=true"); + "spring.h2.console.enabled:true", + "spring.h2.console.settings.trace=true", + "spring.h2.console.settings.webAllowOthers=true"); this.context.refresh(); assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1); assertThat(this.context.getBean(ServletRegistrationBean.class).getUrlMappings()) .contains("/h2-console/*"); assertThat(this.context.getBean(ServletRegistrationBean.class).getInitParameters()). - containsEntry("webAllowOthers", "true"); - + containsEntry("trace", ""); + assertThat(this.context.getBean(ServletRegistrationBean.class).getInitParameters()). + containsEntry("webAllowOthers", ""); } } 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 a79080f1d33..bd38422b179 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -573,6 +573,8 @@ content into your application; rather pick only the properties that you need. # H2 Web Console ({sc-spring-boot-autoconfigure}/h2/H2ConsoleProperties.{sc-ext}[H2ConsoleProperties]) spring.h2.console.enabled=false # Enable the console. spring.h2.console.path=/h2-console # Path at which the console will be available. + spring.h2.console.settings.trace=false # Enable trace output. + spring.h2.console.settings.web-allow-others=false # Enable remote access. # JOOQ ({sc-spring-boot-autoconfigure}/jooq/JooqAutoConfiguration.{sc-ext}[JooqAutoConfiguration]) spring.jooq.sql-dialect= # SQLDialect JOOQ used when communicating with the configured datasource. For instance `POSTGRES`