Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2-Console is not showing in browser

Tags:

spring-boot

h2

I am working on SpringBoot api and using H2 database with following property settings.

spring.h2.console.enabled=true spring.datasource.name=test spring.datasource.username=sa spring.datasource.password= spring.datasource.driver-class-name=org.h2.Driver spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.datasource.initialization-mode = embedded spring.datasource.url=jdbc:h2:mem:test spring.jpa.hibernate.ddl-auto = update 

When I want to use browser to view the H2 database console through 'http://localhost:8082/h2-console', a screen open in browser with connect and test connection button. When I click on Test Connection, it returns successful but when click on Connect button, error comes that localhost refused to connect.

Here is the screen of that error

like image 559
TAB Avatar asked Nov 20 '18 14:11

TAB


People also ask

How do I access the H2 console on my browser?

Access the H2 Console You can access the console at the following URL: http://localhost:8080/h2-console/. You need to enter the JDBC URL, and credentials. To access the test database that the greeter quickstart uses, enter these details: JDBC URL: jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1.

How do I know if H2 is running?

Step 3: Verify H2 Database InstallationClick Windows → type H2 Console → Click H2 console icon. Connect to the URL http://localhost:8082. At the time of connecting, the H2 database will ask for database registration as shown in the following screenshot.


2 Answers

add this two lines in your spring security file and you are good to go.

    http.csrf().disable();     http.headers().frameOptions().disable(); 
like image 54
Odwori Avatar answered Oct 04 '22 02:10

Odwori


By default Spring Security disables rendering within an iframe because allowing a webpage to be added to a frame can be a security issue, for example Clickjacking. Since H2 console runs within a frame so while Spring security is enabled, frame options has to be disabled explicitly, in order to get the H2 console working.

http.headers().frameOptions().disable(); 

In general there are two possible directives for X-Frame-Options, which are DENY or SAMEORIGIN, so the following configuration can also be used for restricted but secured access.

headers().frameOptions().sameOrigin(); 

This allows the page to be displayed in a frame on the same origin as the page itself

like image 45
sankha Avatar answered Oct 04 '22 02:10

sankha