Commit 3109fd50 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #5417 from mdeinum/gh-5416

* pr/5417:
  Polish contribution
  Support setting webAllowOthers for the H2 Web Console
parents 52461b42 bca83bde
......@@ -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
......@@ -61,7 +63,15 @@ 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);
H2ConsoleProperties.Settings settings = this.properties.getSettings();
if (settings.isTrace()) {
registration.addInitParameter("trace", "");
}
if (settings.isWebAllowOthers()) {
registration.addInitParameter("webAllowOthers", "");
}
return registration;
}
@Configuration
......
/*
* 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,6 +44,8 @@ public class H2ConsoleProperties {
*/
private boolean enabled = false;
private final Settings settings = new Settings();
public String getPath() {
return this.path;
}
......@@ -58,4 +62,37 @@ public class H2ConsoleProperties {
this.enabled = enabled;
}
public Settings getSettings() {
return this.settings;
}
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;
}
}
}
......@@ -34,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 {
......@@ -70,6 +72,10 @@ 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");
}
@Test
......@@ -104,4 +110,21 @@ public class H2ConsoleAutoConfigurationTests {
.contains("/custom/*");
}
@Test
public void customInitParameters() {
this.context.register(H2ConsoleAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"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("trace", "");
assertThat(this.context.getBean(ServletRegistrationBean.class).getInitParameters()).
containsEntry("webAllowOthers", "");
}
}
......@@ -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`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment