12

I am using the H2 database in a Spring boot application. But unable to open it in the browser at http://localhost:8080/console. My pom.xml is as below:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.192</version>
</dependency>

Spring boot Configuration :

Springboot configuration file

@Configuration
public class WebConfiguration {
    @Bean
    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
        registrationBean.addUrlMappings("/console/*");
        return registrationBean;
    }
}

enter image description here

djm.im
  • 3,295
  • 4
  • 30
  • 45
Eternal_Explorer
  • 1,119
  • 3
  • 14
  • 26

11 Answers11

34

to use the H2 console you need to configure it in your .properties file

spring.h2.console.enabled=true
spring.h2.console.path=/h2console/

where /h2console/ is the path you want to use on the browser so you can change it to anything. Also if you have security enabled you might want to add it to the permitted paths

also add this to your HttpSecurity configuration http.headers().frameOptions().disable();

Edit

change your security configuration i'm pretty sure you might have spring security in your pom so use this instead, if not it should work

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
                .authorizeRequests().antMatchers("/console/**").permitAll();

        httpSecurity.csrf().disable();
        httpSecurity.headers().frameOptions().disable();
    }

}
Ayo K
  • 1,719
  • 2
  • 22
  • 34
1

If have included spring-boot-starter-security artifact in your pom then by default basic authentication is enabled. Hence, to access your console either you disable the basic authentication by adding security.basic.enabled=false in your application.properties or allow the access in your configure method as below:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable().authorizeRequests().antMatchers("/").permitAll().and().authorizeRequests()
                .antMatchers("/console/**").permitAll();
        httpSecurity.headers().frameOptions().disable();
    }
}
Dhiraj Ray
  • 827
  • 7
  • 11
1

You might have face 2 situation including following errors:

  1. localhost refused to connect

localhost refused to connect

  • Double check the URL. Chrome automatically try to change http:// to https://

  • Check spring.h2.console.path (Path at witch the console avilible) to get your URL:

    Default: /h2-console --> URL: http://localhost:8080/h2-console/

  • If you running IDE (e.g. IntelliJ Idea), make sure your app is running witch means your H2 data base is running!


  • You face 404 Error: Whitelabel Error Page In this case your H2 Data base is correctly running on Port 8080 and you already have the connection with it.

  • Check spring.h2.console.path (Path at witch the console avilible) to get your URL:

    Default: /h2-console --> URL: http://localhost:8080/h2-console/

  • Enable H2 Console

spring.h2.console.enabled=true
ARiyou Jahan
  • 622
  • 6
  • 7
0

Thank you all for your generous help.The application class (Springboot) was in a separate package and it was not scanning other packages.Also I modified my Pom.xml a bit which finally helped me to access the console.Attached is my new Pom.xml.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.app</groupId>
    <artifactId>Demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBootApp</name>
    <description>Generator of statistics </description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



        <!--WebJars -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Spring AOP + AspectJ -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>

        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>

        </dependency>


        <!-- JavaConfig need this library -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>

        </dependency>


        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>

        </dependency>
    </dependencies>
    <build>


        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>




</project>
Eternal_Explorer
  • 1,119
  • 3
  • 14
  • 26
0

go the POM file and add the dependency :

       <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

rebuild your project

Hilal Aissani
  • 709
  • 8
  • 3
0

this might help folks , all the configuration above is correct

Note - if you are not using any security then adding spring security is not required

Actualy problem is - when you open this url in chrome

http://localhost:8080/h2 chrome makes it --> https://localhost:8080/h2

To get rid of this issue - Below reference will help -

Google Chrome redirecting localhost to https

Ashish Shetkar
  • 1,414
  • 2
  • 18
  • 35
0

The issue might be also be caused by adding server.servlet.context-path to properties. The new url will be composed of server.servlet.context-path plus spring.h2.console.path

atkawa7
  • 461
  • 1
  • 6
  • 13
0

If you have renamed the h2 path in the application.properties to "console" you've to add it in your antMatcher like .antMatcher("/console/**") with two asterisks after the "console" because there are much more appendings.

cybin
  • 1
  • 1
0

If none of the above solution works, try below one, this worked for me :

  1. Add below property in your application.propertiesfollowing spring.data.jpa.repositories.bootstrap-mode=default
  2. Open console in browser using this URL : http://localhost:8080/h2-console
  3. On a login page make sure that you use jdbc:h2:mem:testdb as JDBC URL.
Nisarg Patil
  • 1,509
  • 1
  • 16
  • 27
0

maybe you need this in your .properties file:

spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true
spring.h2.console.path=/h2-console
Serterano
  • 1
  • 1
  • 1
    Welcome to Stack Overflow! While this configs may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – HardcoreGamer Aug 29 '23 at 04:11
-1

As h2 database console is mapped to "h2-console".

Use this:

http.csrf().disable().authorizeRequests()
        .antMatchers("/h2-console/**").permitAll()
        .anyRequest().authenticated();

// disable frame options
http.headers().frameOptions().disable();

` You don't need permit root access: .antMatchers("/") * NOT NEEDED *

djm.im
  • 3,295
  • 4
  • 30
  • 45
Rohith
  • 327
  • 4
  • 12