Registration and Login Example with Spring Boot, Spring Security, Spring Data JPA, and HSQL

This post walks you through the process of creating a simple Registration and Login Example with Spring Boot, Spring Security, Spring Data JPA and HSQL.

If you are new to Spring Boot or Spring Data JPA, it would be best to work your way through below before starting this example:
- Spring Boot Hello World Example with JSP 
- JPA Many-To-Many Relationship Mapping Example with Spring Boot, HSQL

What you’ll build

Log in

Log out

Register account

Welcome

What you’ll need

  • JDK 1.7 or later
  • Maven 3 or later

Stack

  • Spring Security
  • Spring Boot
  • Spring Data JPA
  • JSP

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

pom.xml

Define JPA Entities

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

src/main/java/com/hellokoding/auth/model/User.java

src/main/java/com/hellokoding/auth/model/Role.java

@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. @JoinColumn indicates the entity is the owner of the relationship: the corresponding table has a column with a foreign key to the referenced table. mappedBy indicates the entity is the inverse of the relationship.

Spring Data JPA Repositories

Spring Data JPA contains some built-in Repository implemented some common functions to work with database: findOnefindAllsave,…

src/main/java/com/hellokoding/auth/repository/UserRepository.java

src/main/java/com/hellokoding/auth/repository/RoleRepository.java

Spring Security’s UserDetailsService

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

src/main/java/com/hellokoding/auth/service/UserDetailsServiceImpl.java

Security Service

We create SecurityService to provide current loggedin user and auto login user after resgistering an account.

src/main/java/com/hellokoding/auth/service/SecurityServiceImpl.java

User Service

Provide service for registering account

src/main/java/com/hellokoding/auth/service/UserServiceImpl.java

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

src/main/java/com/hellokoding/auth/validator/UserValidator.java

Controllers

/login POST controller is provided by Spring Security

src/main/java/com/hellokoding/auth/web/UserController.java

Properties

src/main/resources/application.properties

src/main/resources/validation.properties

Web Security Configuration

src/main/java/com/hellokoding/auth/WebSecurityConfig.java

Application Configuration

src/main/java/com/hellokoding/auth/WebApplication.java

Run

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

Source code

git@github.com:hellokoding/registration-login-spring-hsql.git

https://github.com/hellokoding/registration-login-spring-hsql

Registration and Login Example Brothers:
- Registration and Login Example with Spring Security, Spring Data JPA, Spring Boot 
- Registration and Login Example with Spring MVC 4, Spring Security, Spring Data JPA, XML Configuration, Maven, JSP and MySQL

Leave a Comment