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
- Create Database tables and sample data for Login & Registration
- Define models for Database tables.
- Declare Spring Bean for BCryptPasswordEncoder. (BCryptPasswordEncoder will be used for password encoding)
- Edit application.properties file and add two queries for AuthenticationManagerBuilder.
- Override Spring Security Default Configurations using Java Config.
- Define controllers which will show Login and Registration page to user.
- 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:
- Username: [email protected]
- Password: abc123ABC
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)
- Username: [email protected]
- Password: abc123ABC
For any diffuculties, you can write in comment section, we will try to solve your ptoblem.
Hello.
I can´t play the video in your page 🙁
I see the div in code but it doesn’t play …
Cheers
Can you please tell which browser you are using? I’ll also add links to youTube video along with it. For now you can use link: https://www.youtube.com/playlist?list=PL3hpmQhMoz-cz1GBAtovJyrfspZctG03L
Can you please help me out sir, these Username and Password aren’t valid, can you please help
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
I can’t see the complete query statements in the application.properties file. can anyone share?
spring.basic.enabled=false
spring.queries.users-query= select email,password,’1′ as enabled from auth_user where email=? and status=”VERIFIED’
spring.queries.roles-query = select u.email,r.role_name from auth_user u inner join auth_user_role r on(u.auth_user_id = r.auth_role_id)
security.basic.enabled=false
spring.queries.users-query=select email, password, ‘1’ as enabled from auth_user where email=? and status=’VERIFIED’
spring.queries.roles-query=select u.email, r.role_name from auth_user u inner join auth_user_role ur on(u.auth_user_id=ur.auth_user_id) inner join auth_role r on(ur.auth_role_id=r.auth_role_id) where u.email=?
Have you changed source code url? The provided link is not working.