Skip to content

Reinstate support for H2 Console #29755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2012-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.h2;

import java.sql.Connection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.h2.server.web.JakartaWebServlet;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* {@link EnableAutoConfiguration Auto-configuration} for H2's web console.
*
* @author Andy Wilkinson
* @author Marten Deinum
* @author Stephane Nicoll
* @since 1.3.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(JakartaWebServlet.class)
@ConditionalOnProperty(prefix = "spring.h2.console", name = "enabled", havingValue = "true")
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(H2ConsoleProperties.class)
public class H2ConsoleAutoConfiguration {

private static final Log logger = LogFactory.getLog(H2ConsoleAutoConfiguration.class);

@Bean
public ServletRegistrationBean<JakartaWebServlet> h2Console(H2ConsoleProperties properties,
ObjectProvider<DataSource> dataSource) {
String path = properties.getPath();
String urlMapping = path + (path.endsWith("/") ? "*" : "/*");
ServletRegistrationBean<JakartaWebServlet> registration = new ServletRegistrationBean<>(new JakartaWebServlet(),
urlMapping);
configureH2ConsoleSettings(registration, properties.getSettings());
if (logger.isInfoEnabled()) {
logDataSources(dataSource, path);
}
return registration;
}

private void logDataSources(ObjectProvider<DataSource> dataSource, String path) {
List<String> urls = dataSource.orderedStream().map((available) -> {
try (Connection connection = available.getConnection()) {
return "'" + connection.getMetaData().getURL() + "'";
}
catch (Exception ex) {
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList());
if (!urls.isEmpty()) {
StringBuilder sb = new StringBuilder("H2 console available at '").append(path).append("'. ");
String tmp = (urls.size() > 1) ? "Databases" : "Database";
sb.append(tmp).append(" available at ");
sb.append(String.join(", ", urls));
logger.info(sb.toString());
}
}

private void configureH2ConsoleSettings(ServletRegistrationBean<JakartaWebServlet> registration,
Settings settings) {
if (settings.isTrace()) {
registration.addInitParameter("trace", "");
}
if (settings.isWebAllowOthers()) {
registration.addInitParameter("webAllowOthers", "");
}
if (settings.getWebAdminPassword() != null) {
registration.addInitParameter("webAdminPassword", settings.getWebAdminPassword());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2012-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.h2;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;

/**
* Configuration properties for H2's console.
*
* @author Andy Wilkinson
* @author Marten Deinum
* @author Stephane Nicoll
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "spring.h2.console")
public class H2ConsoleProperties {

/**
* Path at which the console is available.
*/
private String path = "/h2-console";

/**
* Whether to enable the console.
*/
private boolean enabled = false;

private final Settings settings = new Settings();

public String getPath() {
return this.path;
}

public void setPath(String path) {
Assert.notNull(path, "Path must not be null");
Assert.isTrue(path.length() > 1, "Path must have length greater than 1");
Assert.isTrue(path.startsWith("/"), "Path must start with '/'");
this.path = path;
}

public boolean getEnabled() {
return this.enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public Settings getSettings() {
return this.settings;
}

public static class Settings {

/**
* Whether to enable trace output.
*/
private boolean trace = false;

/**
* Whether to enable remote access.
*/
private boolean webAllowOthers = false;

/**
* Password to access preferences and tools of H2 Console.
*/
private String webAdminPassword;

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

public String getWebAdminPassword() {
return this.webAdminPassword;
}

public void setWebAdminPassword(String webAdminPassword) {
this.webAdminPassword = webAdminPassword;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2012-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Auto-configuration for H2's Console.
*/
package org.springframework.boot.autoconfigure.h2;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
Expand All @@ -16,8 +16,17 @@

package org.springframework.boot.autoconfigure.security.servlet;

import java.util.function.Supplier;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.h2.H2ConsoleProperties;
import org.springframework.boot.autoconfigure.security.StaticResourceLocation;
import org.springframework.boot.security.servlet.ApplicationContextRequestMatcher;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.web.context.WebApplicationContext;

/**
* Factory that can be used to create a {@link RequestMatcher} for commonly used paths.
Expand All @@ -40,4 +49,43 @@ public static StaticResourceRequest toStaticResources() {
return StaticResourceRequest.INSTANCE;
}

/**
* Returns a matcher that includes the H2 console location. For example:
* <pre class="code">
* PathRequest.toH2Console()
* </pre>
* @return the configured {@link RequestMatcher}
*/
public static H2ConsoleRequestMatcher toH2Console() {
return new H2ConsoleRequestMatcher();
}

/**
* The request matcher used to match against h2 console path.
*/
public static final class H2ConsoleRequestMatcher extends ApplicationContextRequestMatcher<H2ConsoleProperties> {

private volatile RequestMatcher delegate;

private H2ConsoleRequestMatcher() {
super(H2ConsoleProperties.class);
}

@Override
protected boolean ignoreApplicationContext(WebApplicationContext applicationContext) {
return WebServerApplicationContext.hasServerNamespace(applicationContext, "management");
}

@Override
protected void initialized(Supplier<H2ConsoleProperties> h2ConsoleProperties) {
this.delegate = new AntPathRequestMatcher(h2ConsoleProperties.get().getPath() + "/**");
}

@Override
protected boolean matches(HttpServletRequest request, Supplier<H2ConsoleProperties> context) {
return this.delegate.matches(request);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
Expand Down
Loading