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

If you are new to Spring Boot or Spring Data JPA, it would be best to try the following tutorials
- Spring Boot Hello World Example with JSP 
- JPA/Hibernate Many To Many Bidirectional Relationship Mapping

What you'll build

Log in

Log out

Register account

Welcome

What you'll need

  • JDK 8+ or OpenJDK 8+
  • Maven 3+

Stack

  • Spring Boot 2 and Spring Security
  • Spring Data JPA, Hibernate 5 and HSQL
  • JSP

Init project structure and dependencies

Project structure

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── auth
│       │               ├── model
│       │               │   ├── Role.java
│       │               │   └── User.java
│       │               ├── repository
│       │               │   ├── RoleRepository.java
│       │               │   └── UserRepository.java
│       │               ├── service
│       │               │   ├── SecurityServiceImpl.java
│       │               │   ├── SecurityService.java
│       │               │   ├── UserDetailsServiceImpl.java
│       │               │   ├── UserServiceImpl.java
│       │               │   └── UserService.java
│       │               ├── validator
│       │               │   └── UserValidator.java
│       │               ├── web
│       │               │   └── UserController.java
│       │               ├── WebApplication.java
│       │               └── WebSecurityConfig.java
│       ├── resources
│       │   ├── application.properties
│       │   └── validation.properties
│       └── webapp
│           ├── resources
│           │   ├── css
│           │   │   ├── bootstrap.min.css
│           │   │   └── common.css
│           │   └── js
│           │       └── bootstrap.min.js
│           ├── login.jsp
│           ├── registration.jsp
│           └── welcome.jsp
└── pom.xml

Project dependencies


Define JPA Entities and Repositories

JPA and Hibernate Entities

JPA Entity is defined with @Entity annotation, represent a table in your database.



@Table maps the entity with the table. If no @Table is defined, the default value is used: the class name of the entity.

@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.

Spring Data JPA Repositories

Spring Data JPA contains some built-in Repository implemented common functions to work with database such as findOne, findAll and save



Define Services

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 account



Define Validator, Controllers and View Templates

Spring 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 validation.properties


Controllers


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

JSP View Templates with Bootstrap




Define Properties, Configurations and Run

Properties



Web Security Configuration


Application Configuration


Run with Maven

You can run the application using mvn clean spring-boot:run and visit to http://localhost:8080

Source code

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

See also

Spring Boot Course In Practice with Hands-On Experienced Tutorials and Examples Series