【问题标题】:H2 database: h2 console not working with spring-bootH2 数据库:h2 控制台不适用于 spring-boot
【发布时间】:2023-02-08 19:59:16
【问题描述】:

我按照下一个tutorial 进行了 h2 实现。 H2 运行良好,但控制台运行不正常 - 页面未打开。

我尝试了很多礼物,所以我最终的 application.properties 看起来是这样的:

#Database settings
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.data.jpa.repositories.bootstrap-mode=default
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
spring.datasource.generate-unique-name = false 
spring.h2.console.path=/h2-console/
spring.jpa.hibernate.ddl-auto=update

spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
spring.datasource.sql-script-encoding=UTF-8
spring.jpa.open-in-view=false
spring.datasource.initialization-mode=always

#swagger
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

#Disable thymeleaf cashing
spring.template.cache = false;

依赖项:

ext {
    spring_boot_version = '2.6.6'
}

    implementation "org.springframework.boot:spring-boot-starter-parent:${spring_boot_version}"
    implementation "org.springframework.boot:spring-boot-starter-data-jpa:${spring_boot_version}"
    implementation "org.springframework.boot:spring-boot-starter-validation:${spring_boot_version}"
    implementation "org.springframework.boot:spring-boot-starter-thymeleaf:${spring_boot_version}"
    implementation "org.springframework.boot:spring-boot-starter-web:${spring_boot_version}"
    runtimeOnly "com.h2database:h2:1.4.193"

....

我尝试打开的 uri:

http://localhost:8080/h2-console

【问题讨论】:

    标签: spring-boot h2


    【解决方案1】:

    尝试这个:

    server.port=8080
    
    spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE;
    

    【讨论】:

    • 谢谢,但遗憾的是仍然无法正常工作
    【解决方案2】:

    您可以尝试将以下配置添加到安全配置中吗?

     http.authorizeRequests()
          .antMatchers("/h2-console").permitAll()
    

    理想情况下,您需要配置(取决于 spring boot 版本)以允许访问 /h2-console URL。 你也可以公开你的安全配置,让我看看。

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2019-09-06
      • 2014-08-30
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2021-01-26
      相关资源
      最近更新 更多