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

2
  • Unless your pom is empty, I think you forgot to attach it ;) Commented Nov 10, 2017 at 11:25
  • @PierreB.:Attached the dependency what I am using. Commented Nov 10, 2017 at 11:33

13 Answers 13

35

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(); } } 
Sign up to request clarification or add additional context in comments.

14 Comments

I configured in a configuration file
what configuration file?
Springboot configuration file.
did you add this http.headers().frameOptions().disable()
am not using spring security - but it is not working for me
|
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.

1 Comment

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/34896
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(); } } 

Comments

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 

Comments

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

Comments

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

Comments

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.

Comments

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.

Comments

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 

1 Comment

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.
0

go the POM file and add the dependency :

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

rebuild your project

Comments

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> 

Comments

0

Another potential solution is to check if your application.properties file contains the following line:

spring.main.web-application-type=reactive

To resolve this, you can either remove or comment out the line.

Comments

-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 *

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.