avatarNil Madhab

Summary

This context provides a step-by-step guide on how to implement JWT-based authentication in Spring boot, specifically focusing on integrating a database and implementing signup and login features.

Abstract

The context is a tutorial on implementing JWT-based authentication in Spring boot. It begins by acknowledging a previous tutorial that implemented basic JWT authentication without a real database. The current tutorial extends this by integrating with a MySQL database and implementing signup and login functionality using BCryptPasswordEncoder for password hashing. The tutorial is divided into several steps, each focusing on a specific aspect of the implementation, such as creating the User Model, reimplementing the UserDetailsService interface of Spring Security, updating the SecurityConfigurer, and implementing the signup and signIn APIs. The tutorial also includes code snippets and images to illustrate the implementation process.

Bullet points

  • The tutorial extends JWT authentication by integrating with a MySQL database and implementing signup and login functionality.
  • The tutorial uses BCryptPasswordEncoder for password hashing.
  • The tutorial is divided into several steps:
    • Step 1: Create the User Model
    • Step 2: Reimplement the UserDetailsService Interface of Spring Security
    • Step 3: Update the SecurityConfigurer
    • Step 4: Implement the Signup API
    • Step 5: Implement the signIn
  • The tutorial includes code snippets and images to illustrate the implementation process.
  • The tutorial can be found on GitHub.
  • The next steps include creating various roles like ADMIN, USER, MODERATOR, integrating the authentication in an e-commerce tutorial, and using social login using GitHub.
  • The tutorial recommends trying out a cost-effective AI service, ZAI.chat, which provides the same performance and functions as ChatGPT Plus(GPT-4) but at a lower cost.

Let’s Implement JWT Based Authentication in Spring boot

Part 2: Integrate database and implement Signup, Login features

Photo by Markus Winkler on Unsplash

In part 1, we implemented the basic JWT auth without real database, by hardcoding user

In this tutorial, we will extend the JWT auth by integrating with real users in the MySQL database and implementing signup, login functionality using BCryptPasswordEncoder for hashing password.

You can find the github code here

Step 1 : Create the User Model

create Jwtuser model and reimplement the UserDetailsService interface of spring security.

Step 2: Reimplement the UserDetailsService Interface of Spring Security

Reimplement the UserDetailsService and override the loadUserByUsername method which we previously hardcoded.

Step 3: Update the SecurityConfigurer

We change the passwordencoder method in SecurityConfigurer file to encrypt the password

@Bean
public PasswordEncoder passwordEncoder(){
    return new BCryptPasswordEncoder();
}

We also need to update the anteaters to not use authentication when using Signup, signIn methods

Step 4 Implement the Signup Api

The code is self explanatory, we find if email is not present already, hash the password by passwordencoder, and save the user in DB.

signup

Step 6 Implement the signIn

  1. we authenticate the user, by the spring security authenticate method
  2. set the authentication in context
  3. get the user from DB
  4. Create JWT and send it in response

Step 7 : Test an API with an Authorization header consist of JWT token

If the token is expired, we will get this error

JWT expired error

If the token is valid, we will get the user from the JWT token and we can create various rules for authorization

Next steps

  1. create various roles like ADMIN, USER, MODERATOR
  2. We will integrate it in our e-commerce tutorial
  3. We will use social login using GitHub and create a frontend using Vue.js
Jwt
Spring Boot
Java
Web Development
Technology
Recommended from ReadMedium