Add support for webAdminPassword property of H2 Console

See gh-21533
This commit is contained in:
Radek Koubsky 2020-05-21 18:38:03 +02:00 committed by Stephane Nicoll
parent 776cd08f31
commit 1d40234737
3 changed files with 29 additions and 8 deletions

View File

@ -62,13 +62,7 @@ public class H2ConsoleAutoConfiguration {
String path = properties.getPath();
String urlMapping = path + (path.endsWith("/") ? "*" : "/*");
ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping);
H2ConsoleProperties.Settings settings = properties.getSettings();
if (settings.isTrace()) {
registration.addInitParameter("trace", "");
}
if (settings.isWebAllowOthers()) {
registration.addInitParameter("webAllowOthers", "");
}
configureSettings(properties.getSettings(), registration);
dataSource.ifAvailable((available) -> {
try (Connection connection = available.getConnection()) {
logger.info("H2 console available at '" + path + "'. Database available at '"
@ -81,4 +75,18 @@ public class H2ConsoleAutoConfiguration {
return registration;
}
private void configureSettings(H2ConsoleProperties.Settings settings,
ServletRegistrationBean<WebServlet> registration) {
if (settings.isTrace()) {
registration.addInitParameter("trace", "");
}
if (settings.isWebAllowOthers()) {
registration.addInitParameter("webAllowOthers", "");
}
if (settings.getWebAdminPassword() != null) {
registration.addInitParameter("webAdminPassword", settings.getWebAdminPassword());
}
}
}

View File

@ -77,6 +77,8 @@ public class H2ConsoleProperties {
*/
private boolean webAllowOthers = false;
private String webAdminPassword;
public boolean isTrace() {
return this.trace;
}
@ -93,6 +95,14 @@ public class H2ConsoleProperties {
this.webAllowOthers = webAllowOthers;
}
public String getWebAdminPassword() {
return this.webAdminPassword;
}
public void setWebAdminPassword(String webAdminPassword) {
this.webAdminPassword = webAdminPassword;
}
}
}

View File

@ -79,6 +79,7 @@ class H2ConsoleAutoConfigurationTests {
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
assertThat(registrationBean.getInitParameters()).doesNotContainKey("trace");
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAllowOthers");
assertThat(registrationBean.getInitParameters()).doesNotContainKey("webAdminPassword");
}
@Test
@ -114,13 +115,15 @@ class H2ConsoleAutoConfigurationTests {
void customInitParameters() {
this.context.register(H2ConsoleAutoConfiguration.class);
TestPropertyValues.of("spring.h2.console.enabled:true", "spring.h2.console.settings.trace=true",
"spring.h2.console.settings.webAllowOthers=true").applyTo(this.context);
"spring.h2.console.settings.webAllowOthers=true", "spring.h2.console.settings.webAdminPassword=abcd")
.applyTo(this.context);
this.context.refresh();
assertThat(this.context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
ServletRegistrationBean<?> registrationBean = this.context.getBean(ServletRegistrationBean.class);
assertThat(registrationBean.getUrlMappings()).contains("/h2-console/*");
assertThat(registrationBean.getInitParameters()).containsEntry("trace", "");
assertThat(registrationBean.getInitParameters()).containsEntry("webAllowOthers", "");
assertThat(registrationBean.getInitParameters()).containsEntry("webAdminPassword", "abcd");
}
@Test