Spring Boot Security with H2

Published: 2017-11-19 • Updated: 2019-09-08


A simple secure spring web application needs a database to be configures at its backend. This post covers how to configure h2 db and access its console.

Maven dependency

To add H2 in spring boot application, you need to add following maven dependency in pom.xml


Spring boot will automatically configure it when it sees its dependency on classpath.

To access the console.

You need to add the following bean.

public class H2Config {

    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
        return registrationBean;

That's it, fire up your application and try hitting URL: http://localhost:8080/console/

It will prompt for username and password, which will be
username : user
password: <check for log line, "Using default security password: 3cbb1572-36c6-4487-b602-11ab2dc576ad">

To access the console without login

Add the following security configuration

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity httpSecurity) throws Exception {



You're good to go.
To access the full working code sample, click here


Share this Post