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

Spring Security双H2内存库:H2控制台访问失败及Bean缺失问题求助

问题

使用Spring Boot 3.0.2版本配置两个H2内存数据库后,访问localhost:8080/h2-console控制台时出现以下问题:

  1. 启动应用时抛出org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.autoconfigure.h2.H2ConsoleProperties'异常
  2. 移除Security配置中的auth.requestMatchers(toH2Console()).permitAll();后启动不再报错,但访问控制台仍返回403 Forbidden
  3. 自定义H2ConsoleProperties Bean后问题依旧

解决方案

1. 清理JPA配置中的无效H2控制台属性

h2.console.*属于Spring Boot全局配置,不是Hibernate的JPA属性,在两个持久化配置类中移除相关配置:

  • 在PersistenceEnqueueupConfiguration的enqueueUpEntityManager()方法中,删除:
    properties.put("h2.console.enabled", false);
    
  • 在PersistenceUsersConfiguration的userEntityManager()方法中,删除:
    properties.put("h2.console.enabled", true);
    properties.put("h2.console.path", toH2Console());
    

2. 通过配置文件开启H2控制台

创建/修改application.properties,添加Spring Boot官方支持的H2控制台配置:

# 开启H2控制台
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
# 控制台附加设置
spring.h2.console.settings.web-allow-others=true
spring.h2.console.settings.trace=true

3. 修正Security放行规则

保留直接路径放行,移除依赖H2ConsoleProperties的toH2Console()调用,确保H2控制台请求完全放行:

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity, AuthenticationManager authenticationManager) throws Exception {

    JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(jwtUtils);
    jwtAuthenticationFilter.setAuthenticationManager(authenticationManager);
    jwtAuthenticationFilter.setFilterProcessesUrl("/login");

    return httpSecurity
            .csrf(AbstractHttpConfigurer::disable)
            .headers(headers -> headers.frameOptions(frame -> frame.sameOrigin())) // 允许H2控制台的iframe渲染
            .authorizeHttpRequests(auth -> {
                auth.requestMatchers("/hello", "/createUser", "/h2-console/**").permitAll();
                auth.requestMatchers("/api/path/**").hasAnyRole("ADMIN", "USER");
                auth.requestMatchers("/api/path1/**").hasRole("USER");
                auth.anyRequest().authenticated();
            })
            .sessionManagement(session -> {
                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
            })
            .addFilter(jwtAuthenticationFilter)
            .addFilterBefore(authorizationFilter, UsernamePasswordAuthenticationFilter.class)
            .build();
}

4. 删除自定义的H2ConsoleProperties Bean

移除H2ConsolePropConfig类,Spring Boot会根据配置文件自动生成H2ConsoleProperties实例,无需手动定义。

5. 访问两个数据源的方法

启动应用后访问http://localhost:8080/h2-console,登录时分别输入对应数据源的URL:

  • 第一个数据源:jdbc:h2:mem:enqueueupdb;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS ENQUEUEUP
  • 第二个数据源:jdbc:h2:mem:userdb;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
    用户名统一为sa,密码为空。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 12:27:06