Skip to content

Custom-Database-User-Jwt-authentication-and-authorization-using-Spring-Boot-Rest-API. In this Api we used Spring security, Validation and Jwt implementation for authentication and authorization and we solved all types of exception in running test cases

Notifications You must be signed in to change notification settings

KundanChourasiya/Custom-Database-User-Jwt-authentication-and-authorization-using-Spring-Boot-Rest-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom-database-User-authentication-and-authorization-using-JWT-API.

Note

In this Api we used Spring Security, Validation and Jwt implementation for authentication and authorization and we solved all types of exception in running test cases.

Tech Stack

  • Java
  • Spring Framework
  • Spring Boot
  • Spring Data JPA
  • lombok
  • Jwt
  • Spring Security
  • MySQL
  • Postman
  • Swagger UI

Modules

  • Admin Modules
  • User Modules
  • Open Url Modules

Documentation

Swagger UI Documentation - http://localhost:8080/swagger-ui/

Installation & Run

Before running the API server, you should update the database config inside the application.properties file. Update the port number, username and password as per your local database config.

spring.datasource.url=jdbc:mysql://localhost:3306/mydb; spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=root 

API Root Endpoint

https://localhost:8080/ http://localhost:8080/swagger-ui/ user this data for checking purpose. 

Step To Be Followed

  1. Create Rest Api will return to user authentication with database custom user details.

    Project Documentation

    • Entity - AppUser (class)
    • Payload - AppUserDto, ApiResponceDto, ErrorDto, LoginDto, TokenDto (class)
    • Repository - AppUserRepository (interface)
    • Service - AppUserService (interface), AppUserServiceImpl, JwtService (class)
    • Controller - AuthUserController, UserAccessController, OpenUrlController (Class)
    • Global Exception - GlobalException, JwtException (class)
    • Config - SecurityConfig, JwtFilter, SwaggerConfig (Class)
  2. Secure the Rest API by adding security dependecy and adding Jwt dependency.

  3. Add Secret key, issuer and expiry duration in pom.xml file.

  4. Create Jwtservice class inside the service package to implement

    1. Secret key, issuer and expiry duration
    2. Create PostContruct method to load the Jwt Algorithm
    3. Create generateToken method to generate the token.
    4. Create _verifyToken_ method to validateToken and verify User Credentials.
  5. Create JwtFilter class inside the config package.

    1. extend the class with OncePerRequestFilter.
    2. Inject the handlerExceptionResolver dependency to handler filter level exception.
    3. create a list Array of Permitted_path which should not filter endpoint.
    4. override shouldNotFilter method and doFilterInternal method.
  6. Create SecurityConfig class inside the Config package and create Bean SecurityFilterChain method to Authorize endpoint url with based on user role.

  7. Create SwaggerConfig class to integrate OpenApi Components for authorize user access token.

important Dependency to be used

  1. For rest api
 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 
  1. For Getter and Setter
 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> 
  1. For Security
 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> 
  1. For JWT
 <!-- https://mvnrepository.com/artifact/com.auth0/java-jwt --> <dependency>	<groupId>com.auth0</groupId>	<artifactId>java-jwt</artifactId>	<version>4.4.0</version> </dependency> 
  1. For Swagger
<dependency>	<groupId>org.springdoc</groupId>	<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>	<version>2.3.0</version> <!-- Latest version --> </dependency> 

About

Custom-Database-User-Jwt-authentication-and-authorization-using-Spring-Boot-Rest-API. In this Api we used Spring security, Validation and Jwt implementation for authentication and authorization and we solved all types of exception in running test cases

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages