H2-In memory database console not opening
Asked Answered
I

12

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

Imprecise answered 10/11, 2017 at 11:17 Comment(2)
Unless your pom is empty, I think you forgot to attach it ;)Defibrillator
@PierreB.:Attached the dependency what I am using.Imprecise
P
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();
    }

}
Patrizius answered 10/11, 2017 at 11:26 Comment(15)
I configured in a configuration fileImprecise
what configuration file?Patrizius
Springboot configuration file.Imprecise
did you add this http.headers().frameOptions().disable()Patrizius
No.I didn't.Attached my config snapshot.Imprecise
: Changing my configuration asks me username and password when I try accessing the h2 console.Imprecise
if you add antMatchers("/console/**").permitAll() and /console is the path you set for you H2 console it should'ntPatrizius
Even I am wondering.It worked fine in Mac.But not in Windows .Imprecise
are you sure it's the right build? since it's run on the browser it should be OS independentPatrizius
True.But I am not seeing any issue wrt build.Imprecise
weird.. try changing /console/** to /consolePatrizius
Let us continue this discussion in chat.Imprecise
Thanks Buddy, Voila,Your Suggestions http.headers().frameOptions().disable(); did the trick as I was not able to get console display anything after connect to H2.Sulphurate
Works with boot 1.5.10. Thanks.Soundboard
am not using spring security - but it is not working for meHueyhuff
M
2

Another possible solution, which was the case for me, was that I was using

'org.springframework.boot:spring-boot-starter-webflux'

as a dependency.

Switching to

'org.springframework.boot:spring-boot-starter-web'

resolved the issue.

Mellins answered 15/9, 2023 at 10:23 Comment(1)
this worked for me. it also led me to looking into it and webflux doesnt create a servlet app like web. heres the explanation i found github.com/spring-projects/spring-boot/issues/34896Malo
L
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();
    }
}
Lenticel answered 12/11, 2017 at 5:53 Comment(0)
R
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
Realtor answered 17/3, 2021 at 23:59 Comment(0)
I
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>
Imprecise answered 10/11, 2017 at 16:5 Comment(0)
A
0

go the POM file and add the dependency :

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

rebuild your project

Autarky answered 24/2, 2020 at 14:56 Comment(0)
H
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

Hueyhuff answered 24/7, 2020 at 6:52 Comment(0)
R
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

Reply answered 9/6, 2021 at 18:20 Comment(0)
E
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.

Episiotomy answered 23/11, 2022 at 18:39 Comment(0)
C
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.
Cercus answered 3/2, 2023 at 8:44 Comment(0)
P
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
Peridotite answered 27/8, 2023 at 14:53 Comment(1)
Welcome to Stack Overflow! While this configs may solve the question, including an explanation 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.Zamindar
H
-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 *

Hom answered 20/4, 2019 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.