You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

H2控制台能否部署在Spring Boot的管理端口上?

将H2控制台部署到Actuator管理端口的方案

核心结论

可以实现把H2控制台部署到9090端口,甚至挂载到9090/actuator/h2-console路径下——官方没有直接提供配置项,但通过自定义Bean就能搞定。

方案一:绑定到管理端口(9090/h2-console)

H2控制台默认注册在主应用的Servlet上下文,要移到管理端口(9090),需要手动把H2的控制台Servlet注册到Actuator的管理上下文里:

import org.h2.server.web.WebServlet;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServletContext;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class H2ConsoleConfig {

    @Bean
    public ServletRegistrationBean<WebServlet> h2ConsoleServletRegistration(ManagementServletContext managementServletContext) {
        ServletRegistrationBean<WebServlet> registration = new ServletRegistrationBean<>(new WebServlet(), "/h2-console/*");
        // 将Servlet绑定到管理端口的上下文
        registration.setServletContext(managementServletContext.getServletContext());
        return registration;
    }
}

然后在application.properties里关闭主应用的自动注册:

spring.h2.console.enabled=false

配置完成后,H2控制台就只能通过http://localhost:9090/h2-console访问。

方案二:挂载到Actuator路径下(9090/actuator/h2-console)

要把控制台放在/actuator子路径下,需要调整Servlet映射规则,同时让Actuator不拦截这个路径:

  1. 修改上面的ServletRegistrationBean映射路径:
registration.setUrlMappings(List.of("/actuator/h2-console/*"));
  1. 在application.properties里配置Actuator的规则:
management.endpoints.web.exposure.include=*
management.endpoints.web.base-path=/actuator
# 排除h2-console,避免被Actuator当作端点处理
management.endpoints.web.exclude=h2-console
  1. 同样关闭主应用的自动注册:
spring.h2.console.enabled=false

此时访问地址就是http://localhost:9090/actuator/h2-console。

注意事项

  • 生产环境严禁开启H2控制台,哪怕放在管理端口也有安全风险,必须搭配Spring Security做访问限制。
  • 确保项目已正确引入Actuator和H2依赖,且版本与Spring Boot兼容。

内容的提问来源于stack exchange,提问作者Jeff Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 17:22:47