Spring Security – JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap

In the tutorial, JavaSampleApproach will show you how to create a Spring Security JDBC Authentication with SpringBoot + PostgreSQL + Bootstrap.

I. Technologies

– Apache Maven 3.5.2
– Spring Tool Suite – Version 3.9.0.RELEASE
– Spring Boot – 1.5.10.RELEASE
– Bootstrap
– PostgreSQL

II. Goal

We create a Spring MVC Web Application as below:

JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap - project structure

With 5 urls:

– ‘/’: access with everyone.

JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap - home page

– ‘/user’: must authenticate and be accessed with user ROLE {USER, ADMIN}

JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap - user page

– ‘/admin’: accessed by user with role Admin

JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap - admin page

– ‘/login’: login page

JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap - sign in

– ‘/403’: HTTP Error 403 Forbidden

JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap - access denied

We create 2 MySQL tables for 2 users (username/password):
– jack/jack has 2 roles {USER, ADMIN}
– peter/peter has 1 role USER

JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap - tables

-> jack/jack can access both pages {user.html, admin.html}. While peter/peter just accesses 1 page user.html.

III. Implementation

Step to do
– Create Spring Boot project
– Create Controller
– Create View Pages
– Configure Database
– Configure WebSecurity

1. Create Spring Boot project

Use SpringToolSuite to create a SpringBoot project with below dependencies:

2. Create Controller

3. Create View Pages





4. Configure Database

Open ‘application.properties’, configure database properties:

Create ‘user’ table with 3 columns:

Create ‘user_roles’ table:

Insert data to 2 tables:

5. Configure WebSecurity

IV. SourceCode


By grokonez | February 9, 2017.

Last updated on March 8, 2018.

Related Posts

12 thoughts on “Spring Security – JDBC Authentication – SpringBoot + PostgreSQL + Bootstrap”

  1. HI!

    I was trying to do your tutorial, but there was a problem. When i want to autowire the dataSource in the SecurityConfig, IDEA sais, that “Could not autowire. There is more than one bean of ‘DataSource’ type.” I tried to google the solution and found that if i exclude the datasource autoconfiguration from autoconfig, its ok for IDEA, but it looks, that the page isn’t working, it asks for authentication again, and again. Does’t allow me to sign in.

    Do you have any idea, whats wrong? I have a local postgreSQL database, which is created and tested the connection.


    1. Hi Mate,

      I had tested the attached sourcecode again and it works well!
      I also review the code, it is okay with:

      We use SpringToolSuite editor to create tutorial, please double check again your IDE.

      And you can download the attached sourcecode then try to build and run it with below commands:
      mvn clean install and mvn spring-boot:run

      Don’t forget to create 2 tables: users & user_roles then insert data to it with SQL scripts in the tutorial.


  2. Very nice tutorial.
    I have a nice question regarding mapping of /login to login.html.

    How application comes to know that he needs to invoke ‘login.html’ when he accesses ‘/login’ and gets ‘login’ String as view name?

    1. Hi,

      We have a segment code for configure HttpSecurity

      So when a user accesses security paths like: {“/admin”, “/welcome”}.
      The “/login” will always be redirected for asking to authenticate with every first time accessing.
      And also whenever the session is expired, “/login” url will be invoked.


  3. Exactly what I was looking for, thank you! But can you explain like I’m 5, why this:

    will recognize new additions to the users table, even while spring-boot is running (no reboot needed), but this will not?

    despite that both of these methods are ran only once when spring-boot starts up? There seems to be lots of behind-the-scenes operations that I’m not grasping. I get that in memory authentication is hardcoding and jdbc authentication is not, can someone explain what’s going on behind jdbcauthentication? When (or in which file) exactly is it querying the database again during runtime?

    It also doesn’t matter if you name the method “configAuthentication” or “configureGlobal”, it still gets called the same way/sequence? I’m quite confused on what exactly is it overriding (I don’t see the methods listed in WebSecurityConfigurerAdapter class) .

    1. Hello Emily,

      We have some usecase to work with SpringSecurity Provider:
      – If just for testing with some users, We can use Memory-Provider
      – If your users store in database, you can user JDBC-Provider
      – Spring Security also support LDAP-provider.

      We can use extends WebSecurityConfigurerAdapter for configure.


      is used to invoked AuthenticationManagerBuilder auth with jdbcAuthentication.


    1. Hi Igor Lima,

      You can implement AuthenticationSuccessHandler as below sample:

      More details at: Spring Security – Customize Login Handler


  4. Hi sir I don’t know but when I add this part of your code

  5. 18:52:45.025 [localhost-startStop-1] ERROR org.springframework.web.context.Conte
    xtLoader – Context initialization failed
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
    bean with name ‘securityConfiguration’: Unsatisfied dependency expressed throug
    h field ‘dataSource’; nested exception is org.springframework.beans.factory.NoSu
    chBeanDefinitionException: No qualifying bean of type ‘javax.sql.DataSource’ ava
    ilable: expected at least 1 bean which qualifies as autowire candidate. Dependen
    cy annotations: {@org.springframework.beans.factory.annotation.Autowired(require

    I am running through servlet

    1. Hello,
      Very nice tutorials, please publish the tutorial on OAuth with Spring Boot having the database, preferably MySQL implementation.


Got Something To Say:

Your email address will not be published. Required fields are marked *