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
This commit is contained in:
Marten Deinum
2016-03-15 13:50:30 +01:00
committed by Stephane Nicoll
parent 52461b42ae
commit ec8b94f13c
3 changed files with 34 additions and 1 deletions

View File

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

View File

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

View File

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