11

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,153
  • 4
  • 26
  • 42
Bindumalini KK
  • 1,073
  • 3
  • 13
  • 24

8 Answers8

31

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,581
  • 1
  • 19
  • 33
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
  • 707
  • 6
  • 11
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>
Bindumalini KK
  • 1,073
  • 3
  • 13
  • 24
0

go the POM file and add the dependency :

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

rebuild your project

Hilal Aissani
  • 653
  • 4
  • 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,324
  • 2
  • 19
  • 32
0

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
  • 121
  • 5
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
  • 344
  • 1
  • 5
  • 13
-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,153
  • 4
  • 26
  • 42
Rohith
  • 307
  • 4
  • 11