Let’s Implement JWT Based Authentication in Spring boot
Tags: backend, java, springboot, jwt
Published: 2021-03-12 18:19:36

Part 2: Integrate database and implement Signup, Login features

![Photo by [Markus Winkler](https://unsplash.com/@markuswinkler?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/7998/0*l9-LcPg6OM9f3gIA)

In part 1, we implemented the basic JWT auth without real database, by hardcoding user
{% medium https://medium.com/javarevisited/lets-implement-basic-jwt-based-authentication-in-spring-boot-593c46f616c8 %}

In this tutorial, we will extend the JWT auth by integrating with real users in the [MySQL database](https://medium.com/javarevisited/top-5-courses-to-learn-mysql-in-2020-4ffada70656f) and implementing signup, login functionality using BCryptPasswordEncoder for hashing password.

[**You can find the github code here**](https://github.com/webtutsplus/spring-auth-jwt/tree/nilmadhab/user-from-db)

## Step 1 : Create the User Model

create Jwtuser model and reimplement the UserDetailsService interface of spring security.
{% gist https://gist.github.com/nilmadhab/e1bef4425876ee074f65486e8756bda0.js %}
**Step 2: Reimplement the UserDetailsService Interface of Spring Security**

Reimplement the UserDetailsService and override the loadUserByUsername method which we previously hardcoded.
{% gist https://gist.github.com/nilmadhab/ef32dc4d79dcbcc9962b277847e53759.js %}
## 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
{% gist https://gist.github.com/nilmadhab/7065f312b68a5b7b1f681eb662d78cb9.js %}
## 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.
{% gist https://gist.github.com/nilmadhab/f96bddf6424425dafcb68f36ea7578cd.js %}
![signup](https://cdn-images-1.medium.com/max/3144/1*HIJUaARA1Dal4ibLmlqn7w.png)

![](https://cdn-images-1.medium.com/max/2000/1*NgwlZ1LoSDkdy30ClwtAtA.png)

## 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
{% gist https://gist.github.com/nilmadhab/6119207db2b7ae2d9b738863e766c38d.js %}
![](https://cdn-images-1.medium.com/max/2886/1*lvhxQJJW34vOY18scFiMtA.png)

**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](https://cdn-images-1.medium.com/max/5216/1*iCATQvyyenzh4_Mb3Hf3sw.png)

If the token is valid, we will get the user from the JWT token and we can create various rules for authorization
{% gist https://gist.github.com/nilmadhab/14cf7f42603e8b1e7b30b7ff8618bbf4.js %}
![](https://cdn-images-1.medium.com/max/4544/1*DQ9ntF-fti8oFXtESKPP_Q.png)

## 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](https://medium.com/javarevisited/top-5-online-courses-to-learn-vue-js-in-2021-249e66b60646)