SpringBoot访问H2控制台

Nuñ*_*ada 5 spring jpa h2 spring-data-jpa spring-boot

我有一个基本的SpringBoot应用程序,嵌入式Tomcat,Thymeleaf模板引擎.我创建了这个bean来访问控制台:

@Bean
    public ServletRegistrationBean h2ConsoleServletRegistration() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new WebServlet());
        bean.addUrlMappings("/console/*");
        return bean;
    }
Run Code Online (Sandbox Code Playgroud)

但是当我访问控制台时http:// localhost:8080/appContext/console/login.do?jsessionid = f3585792a9bf1f0cf1a0b6a09dcefe1a

我的bean注释如下:

@Entity
@Table(name="t_user")
public class User implements Serializable, UserDetails {
..
}
Run Code Online (Sandbox Code Playgroud)

我的应用属性:

Spring Data JPA属性

spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

hibernate.dialect=org.hibernate.dialect.H2Dialect
Run Code Online (Sandbox Code Playgroud)

但我没有看到JPA创建的任何表格:

在此输入图像描述

pvp*_*ran 8

删除属性文件中的所有内容.你提到的所有这些都是默认的.一旦它确定了你的pom中的h2依赖性,Springboot就会以任何方式配置它.而且你也不需要那个ServletRegistration豆子.删除它.只需将其放在属性文件中即可 spring.h2.console.enabled=true

默认情况下,可以在http:// localhost:8080/h2上访问控制台 - 控制台
默认路径为h2-console.您可以使用
spring.h2.console.path属性对其进行配置


Moh*_*sal 8

我们只需要在文件中进行以下配置application.properties

spring.h2.console.enabled=true
Run Code Online (Sandbox Code Playgroud)

默认情况下,h2 可用于http://localhost:8080/h2-console/

但是可以spring.h2.console.path=/h2在 中application.properties和之后定义 h2 可以使用 访问http://localhost:8080/h2

现在,如果您已在应用程序中实现,SecurityConfig那么您将需要添加

// Make H2-Console non-secured; for debug purposes
.and().csrf().ignoringAntMatchers("/h2/**")
// Allow pages to be loaded in frames from
// the same origin; needed for H2-Console
.and().headers().frameOptions().sameOrigin()
Run Code Online (Sandbox Code Playgroud)

http.authorizeRequests()


Pat*_*mil 6

我们可以使用默认路径访问 H2 控制台,就好像http://localhost:8080/h2-console我们在 pom.xml 中有 devtools 依赖项一样,否则我们必须在 application.properties 中指定 H2 的路径,如下所示


使用 devtools可以访问http://localhost:8080/h2-console/

POM: spring-boot-starter、h2、spring-boot-starter-web、spring-boot-devtools

没有 devtools - 我们需要在属性中设置它:

spring.h2.console.enabled=true 
spring.h2.console.path=/h2-console
Run Code Online (Sandbox Code Playgroud)

POM:spring-boot-starter、h2、spring-boot-starter-web

Spring Boot 2.1.1 就是这种情况,可能对其他人有帮助