From 1d4023473768a1716fb5be5f98e0d3f7c0123ba2 Mon Sep 17 00:00:00 2001 From: Radek Koubsky Date: Thu, 21 May 2020 18:38:03 +0200 Subject: [PATCH 1/2] Add support for webAdminPassword property of H2 Console See gh-21533 --- .../h2/H2ConsoleAutoConfiguration.java | 22 +++++++++++++------ .../autoconfigure/h2/H2ConsoleProperties.java | 10 +++++++++ .../h2/H2ConsoleAutoConfigurationTests.java | 5 ++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index c857d486fcd..4b1365e80b4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -62,13 +62,7 @@ public class H2ConsoleAutoConfiguration { String path = properties.getPath(); String urlMapping = path + (path.endsWith("/") ? "*" : "/*"); ServletRegistrationBean 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 registration) { + if (settings.isTrace()) { + registration.addInitParameter("trace", ""); + } + if (settings.isWebAllowOthers()) { + registration.addInitParameter("webAllowOthers", ""); + } + + if (settings.getWebAdminPassword() != null) { + registration.addInitParameter("webAdminPassword", settings.getWebAdminPassword()); + } + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java index 5118a062774..34ca09ebd20 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java @@ -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; + } + } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index b7c390840ab..6752affedc6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -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 From bf96b0c57b7c6f9a6a5ab1904c8ccc1c1d912964 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 15 Jun 2020 16:19:56 +0200 Subject: [PATCH 2/2] Polish "Add support for webAdminPassword property of H2 Console" See gh-21533 --- .../autoconfigure/h2/H2ConsoleAutoConfiguration.java | 9 ++++----- .../boot/autoconfigure/h2/H2ConsoleProperties.java | 5 ++++- .../h2/H2ConsoleAutoConfigurationTests.java | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java index 4b1365e80b4..0fd3099629b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; +import org.springframework.boot.autoconfigure.h2.H2ConsoleProperties.Settings; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; @@ -62,7 +63,7 @@ public class H2ConsoleAutoConfiguration { String path = properties.getPath(); String urlMapping = path + (path.endsWith("/") ? "*" : "/*"); ServletRegistrationBean registration = new ServletRegistrationBean<>(new WebServlet(), urlMapping); - configureSettings(properties.getSettings(), registration); + configureH2ConsoleSettings(registration, properties.getSettings()); dataSource.ifAvailable((available) -> { try (Connection connection = available.getConnection()) { logger.info("H2 console available at '" + path + "'. Database available at '" @@ -75,15 +76,13 @@ public class H2ConsoleAutoConfiguration { return registration; } - private void configureSettings(H2ConsoleProperties.Settings settings, - ServletRegistrationBean registration) { + private void configureH2ConsoleSettings(ServletRegistrationBean registration, Settings settings) { if (settings.isTrace()) { registration.addInitParameter("trace", ""); } if (settings.isWebAllowOthers()) { registration.addInitParameter("webAllowOthers", ""); } - if (settings.getWebAdminPassword() != null) { registration.addInitParameter("webAdminPassword", settings.getWebAdminPassword()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java index 34ca09ebd20..d17ccfabbb6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/h2/H2ConsoleProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -77,6 +77,9 @@ public class H2ConsoleProperties { */ private boolean webAllowOthers = false; + /** + * Password to access preferences and tools of H2 Console. + */ private String webAdminPassword; public boolean isTrace() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java index 6752affedc6..c5e8ce5d374 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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.