Spring Boot User Registration and Login
Update 2020/05/02: I removed useless `csrf().disabled()` config and CustomAuthenticationProvider class. Also I changed Optional logic and used `orElseThrow()` method. Thanks @gokcendedat and @utkuozdemir for their valuable comments.
Hello! In this article I’ll explain how to register users to your application, how to send confirmation mail and handle user logins. My main resource will be official docs and you can access Github repository link at the end of the article. I will not explain exception handling parts since our main subject is different.
Requirements
You need to know Java and must have developed at least one Spring project to understand this article.
1. Creating a Project with Spring Initialzr
Create a project with Spring Web, Lombok, Thymeleaf, Spring Security, Java Mail Sender, H2, and Spring Data JPA dependencies with Spring Initialzr. You can use your IDE’s plugin or https://start.spring.io/.
2. Create User Entity
Create an entity
package and UserRole
enum inside the package. We will add ADMIN and USER values for now but you can add more according to your needs.