This tutorial will walk you through the process of creating a simple User Account Registration and Login Example with Spring Boot, Spring Security, Spring Data JPA, Hibernate, HSQL, Thymeleaf, and Bootstrap

What you'll build

Register account

Log in

Log out

Welcome

What you'll need

Your local computer should have JDK 8+ or OpenJDK 8+, Maven 3+

You should also walk through the following tutorials if you are new to Spring Boot, Thymeleaf, and Spring Data JPA

Init project structure

You can create and init a new Spring Boot project by using Spring CLI or Spring Initializr. Learn more about using these tools here

The final project structure as below

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── tutorials
│       │               ├── model
│       │               │   ├── Role.java
│       │               │   └── User.java
│       │               ├── repository
│       │               │   ├── RoleRepository.java
│       │               │   └── UserRepository.java
│       │               ├── service
│       │               │   ├── SecurityService.java
│       │               │   ├── SecurityServiceImpl.java
│       │               │   ├── UserDetailsServiceImpl.java
│       │               │   ├── UserService.java
│       │               │   └── UserServiceImpl.java
│       │               ├── web
│       │               │   ├── UserController.java
│       │               │   └── UserValidator.java
│       │               ├── DemoApplication.java
│       │               └── WebSecurityConfig.java
│       └── resources
│           ├── static
│           │   ├── css
│           │   │   └── main.css
│           │   └── js
│           │       └── main.js
│           ├── templates
│           │   ├── login.html
│           │   ├── registration.html
│           │   └── welcome.html
│           ├── application.properties
│           └── validation.properties
├── .gitignore
├── mvnw
├── mvnw.cmd
└── pom.xml

Project dependencies

Define JPA and Hibernate Entities

@Entity is a JPA annotation which specifies the class as an entity (so the class name can be used in JPQL queries)

@Table annotation with the name attribute specifies the table name in the underlying database for the annotated entity. If no @Table is defined, the class name of the entity will be used as the table name

@Id declares the identifier property of the entity

@ManyToMany defines a many-to-many relationship between 2 entities

mappedBy indicates the entity is the inverse of the relationship

Learn more about JPA and Hibernate

Spring Data JPA Repositories

Spring Data JPA Repositories help you reduce boilerplate code required to implement data access layers for various persistence stores such as MySQL and PostgreSQL

They provide some CRUD functions to query, create, update and delete against the underlying database such as findAll, findById, save, saveAll, delete and deleteAll

Define Spring Security's UserDetailsService

To implement login/authentication with Spring Security, we need to implement org.springframework.security.core.userdetails.UserDetailsService interface

Security Service

We create SecurityService to provide current logged-in user and auto-login user after registration

User Service

Provide service for registering an account

Define Validator

To provide input-data validation for /registration controller with Spring Validator, we implement org.springframework.validation.Validator. Error codes, e.g. Size.userForm.username, are defined by the below validation.properties

Define Controllers

We don't define /login POST controller, it is provided by Spring Security

Thymeleaf View Templates with Bootstrap

Static files

Define Properties

Web Security Configuration

Application Configuration

Run and Test

mvn clean spring-boot:run  

Access to localhost:8080 and start playing around with the app

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/springboot-examples/springboot-security-login-thymeleaf