Note
- Java
- Spring Framework
- Spring Boot
- Spring Data JPA
- lombok
- Jwt
- Spring Security
- MySQL
- Postman
- Swagger UI
- Admin Modules
- User Modules
- Open Url Modules
Swagger UI Documentation - http://localhost:8080/swagger-ui/
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 https://localhost:8080/ http://localhost:8080/swagger-ui/ user this data for checking purpose.
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)
Secure the Rest API by adding security dependecy and adding Jwt dependency.
Add Secret key, issuer and expiry duration in pom.xml file.
Create Jwtservice class inside the service package to implement
- Secret key, issuer and expiry duration
- Create PostContruct method to load the Jwt Algorithm
- Create generateToken method to generate the token.
- Create _verifyToken_ method to validateToken and verify User Credentials.
Create JwtFilter class inside the config package.
- extend the class with OncePerRequestFilter.
- Inject the handlerExceptionResolver dependency to handler filter level exception.
- create a list Array of Permitted_path which should not filter endpoint.
- override shouldNotFilter method and doFilterInternal method.
Create SecurityConfig class inside the Config package and create Bean SecurityFilterChain method to Authorize endpoint url with based on user role.
Create SwaggerConfig class to integrate OpenApi Components for authorize user access token.
- For rest api
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> - For Getter and Setter
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> - For Security
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> - 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> - For Swagger
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.3.0</version> <!-- Latest version --> </dependency>