Authenticating users using a database

In this recipe, you'll learn how to use user credentials (username and password) from a database for authentication.

How to do it…

Here are the steps to use user credentials in a database for authentication:

  1. Add the Spring JDBC Maven dependency in pom.xml:
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-jdbc</artifactId> 
        <version>${spring.version}</version> 
    </dependency> 
  2. In the database, create the users and authorities tables:
    create table users( username varchar(50) not null, password varchar(50) not null, enabled boolean not null default true, primary key (username) ); create table authorities ( username varchar(50) not null, authority varchar(50) not null, constraint fk_authorities_users ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.