Create User Login and Registration

This tutorial will demonstrate how to create User Login and Registration in Spring boot web application.
We will use – Spring Boot with Spring 5, Java 8, Spring Security and Maven. For complete instructions, you can go through the below video.

Resources

Source code download
User Tables & data SQL file download

Steps required to create user login and registration (Authentication) in Spring boot

  1. Create Database tables and sample data for Login & Registration
  2. Define models for Database tables.
  3. Declare Spring Bean for BCryptPasswordEncoder. (BCryptPasswordEncoder will be used for password encoding)
  4. Edit application.properties file and add two queries for AuthenticationManagerBuilder.
  5. Override Spring Security Default Configurations using Java Config.
  6. Define controllers which will show Login and Registration page to user.
  7. Generate HTML pages for Login and Registration

Database tables and sample data for Login & Registration

Execute the database script in your MySQL database, either by using GUI application like PHPMyadmin or use command line application.
In this video, I have used the command prompt to import the database tables and sample data.

In sample data we have user with below details:

Once the database tables and data is created, we are ready to create user login and registration in Spring boot.

Define models for Database tables

Now we have the database tables ready to store username and password and also ready access from tables. So, to programmatically access data from and to a database in Spring framework we need to define the models (Object relationship mappings). Two models required to map “two many to many tables”. So, for the AUTH_USER table, we will create a User model and for an AUTH_ROLE table, we will create a Role model. Instructions to create the models in Spring boot application is given in the video.

Declare Spring Bean for BCryptPasswordEncoder.

Implementation for BCryptPasswordEncoder is provided by Spring in package org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder. We need to define the bean which will return this implementation when autowired in another class. Stepwise process for defining bean is demonstrated in the video. Please refer to the video for defining BCryptPasswordEncoder bean using Java configuration by implementing WebMvcConfigurer.

Edit application.properties file and add two queries for AuthenticationManagerBuilder

In this step, we will add two queries – One for authentication in usersByUsernameQuery and the other for authorization in authoritiesByUserNameQuery.
We are going to need these queries when we override the default Spring Security Configurations. Check this section for more details.

Override Spring Security Default Configurations using Java Config

This is the most important step in this process to create a user login and registration in Spring boot web application. We will override Spring Security Default Configurations, to use JDBC based authentication and authorization. Queries written in this section will be used for JDBC based authentication and authorization. To override the Default Configurations of Spring Security, create the configuration class with @EnableWebSecurity annotation, which will extend WebSecurityConfigurerAdapter class. Override the methods. Steps to override methods are mentioned in details in the video, you can check the video for more details. Now we have set up the spring security, it’s time create controllers and views to use Spring Security features.

Define spring controllers for Login and Registration screens

We will need to show the login screen and registration screen to our users. So we will define two controllers, one for showing login screen and another for showing registration screen. Go through the video for more details on creating spring boot controllers for login and registration screen.

Generate HTML pages for Login and Registration

Once the controllers are created, we need to write the HTML code which will act as Spring Views. Spring Views contains the HTML code, which will be rendered on the browser when requested for the content. If you are not sure how Spring works, I would suggest you go through the Spring Hibernate tutorial first.

Now we are done with the setup process, it’s time to check the user login page and try to use Spring Security login functionality. For this, you need to run the project and check the URL http://localhost:8888/login, You will be able to see the login screen and working login mechanism. For more details please check the video tutorial

Login details : (Imported from database script import mentioned in this section)

For any diffuculties, you can write in comment section, we will try to solve your ptoblem.

Help others by sharing the content!

5 thoughts on “Create User Login and Registration”

  1. Hey!! Thanks for a great tutorials, can you please help, I am not able to login using the credentials

    I have connected my sql, also I am able to see the table using CMD, but when I login it shows invalid credentials, can you pleasee help me

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.