This tutorial will walk you through the process of creating a Registration and Login Example with Spring MVC, Spring Security, Spring Data JPA, Hibernate, MySQL, JSP, and Bootstrap.

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

If you don't want to deal with XML Configurations, try:
Registration and Login Example with Spring Boot, Spring Security, Spring Data JPA, Hibernate, HSQL, JSP and Bootstrap

What you'll build

Log in

Log out

Register account

Welcome

What you'll need

  • JDK 8+ or OpenJDK 8+
  • Maven 3+
  • MySQL Server 5.6+

Project structure

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── account
│       │               ├── 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
│       ├── resources
│       │   ├── application.properties
│       │   ├── db.sql
│       │   ├── logback.xml
│       │   └── validation.properties
│       └── webapp
│           ├── resources
│           │   ├── css
│           │   │   ├── bootstrap.min.css
│           │   │   └── common.css
│           │   └── js
│           │       └── bootstrap.min.js
│           └── WEB-INF
│               ├── views
│               │   ├── login.jsp
│               │   ├── registration.jsp
│               │   └── welcome.jsp
│               ├── appconfig-data.xml
│               ├── appconfig-mvc.xml
│               ├── appconfig-root.xml
│               ├── appconfig-security.xml
│               └── web.xml
└── pom.xml

Project dependencies

Database

Define JPA 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. @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 common functions to work with a database such as findOne, findAll, and save

Implement Spring Security's UserDetailsService

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

Authorization is implemented via XML file configuration appconfig-security.xml

Define Security Service

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

Define User Service

Provide service for registering account

Implement 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

Define Controllers

/login POST controller is provided by Spring Security

Define View Templates

Define Properties

XML Configurations

Web Config

Spring MVC Config

Spring Data JPA Config

Spring Security Config

Run with Maven and Jetty

You can run the application using mvn jetty:run and visit to http://localhost:8080

Source code

https://github.com/hellokoding/hellokoding-courses/blob/master/spring-mvc-examples/registration-login-spring-xml-maven-jsp-mysql

If you don't want to deal with XML Configurations, try:
Registration and Login Example with Spring Boot, Spring Security, Spring Data JPA, Hibernate, HSQL, JSP and Bootstrap