Powered by Blogger.

How to receive a JWT-based access token from the client, verify the token, and push data using the client's URL?

1 comment :
JWT OAuth2 Integration and JSON Payload Handling in Java: JWT (JSON Web Token) is a token format used in OAuth2.
A JWT is:
  • Stateless
  • Digitally signed
  • Self-contained
Integrating with client APIs using APIs and JSON-based payloads involves an end-to-end process that includes making API calls with JSON payloads and pushing master/other data to REST API endpoints.

Step 1: Obtain the authentication details/Requirements from the client as shown in the example below.

############_MK_TOKEN_CLIENT_SECRET_#################### 

TOKEN_URL=https://a143mk-uat.client.com/api/v1/client/authentication/generate_token CLIENT_ID=AA143Mkbolgs  
CLIENT_SECRET=testa143mk

 ####Example of Configure URL ############# 
CUST_URL=https://a143mk-uat.client.com/api/master/push_customer 
CUST_USER_PASS_AUTH=useradmin:password //if it is required or not


Step 2: Making an API call with an Json payload and pushing master data to the location.
  1. A REST API Controller is responsible for handling HTTP requests (such as GET, POST, PUT, DELETE) from clients and routing them to the appropriate service methods. It acts as the entry point for external clients to interact with the backend application. 
  2. Purpose: The main role of the controller is to manage incoming HTTP requests, map them to specific methods (often called endpoints), and return appropriate responses to the client. 
  3. Responsibilities: Handle HTTP requests (e.g., GET, POST, PUT, DELETE). Map the incoming requests to service methods. Return HTTP responses, often in JSON or Json format. Perform basic request validation or authentication. Act as the "interface" between the client and the service layer. In a Spring Boot application, for example, the controller is typically annotated with @RestController and the request mappings are handled using annotations like @GetMapping, @PostMapping, etc. 
Example "ClientApiController.java" of a REST Controller in Spring Boot with handlined Json Payload :
 
 
The Service class is a part of the business logic layer of your application. It contains methods that implement the core functionality of the application (e.g., creating, reading, updating, deleting data). The service class is called by the controller to perform operations that are not directly related to the web request itself. 
  1. Purpose: The service layer acts as a mediator between the controller and the data layer (such as a repository or database). It focuses on business logic and operations. 
  2. Responsibilities: Perform the core business logic of the application. Interact with the database (via repositories or DAOs). Provide methods for the controller to use (i.e., encapsulate business operations). Handle any necessary validation or transformation before sending data back to the controller. In a Spring Boot application, service classes are typically annotated with @Service, and they are injected into the controller using @Autowired (or constructor injection). 
Example "CustMastSystemNameService.java" of a Service Class in Spring Boot: 
 Method Internal Working ->geta143mkCustMasterFOrSYS() ->pusha143mkCustToSystemNameSystem().
 


Step 3: The authorization server issues an access token in JWT format The client sends this token in requests using Authorization: Bearer<Token>
The API validates the JWT by checking its signature, expiration, issuer, and audience See the example below for how to obtain an access token for the client: Use this for server-to-server authentication.
 Method Internal Working ->geta143mkCustMasterFOrSYS() ->pusha143mkCustToSystemNameSystem().
 

JWT-based access tokens are secure, stateless, and scalable, making them ideal for modern authentication and authorization systems.

Implementing JWT Token for secure authentication

1 comment :
Implementing JWT (JSON Web Token) in Java typically involves creating a secure authentication mechanism for your web applications. Below is a basic guide to implement JWT using Java, Spring MVC, and jjwt (a popular Java JWT library).
WorkFlow:
















The lifecycle of a JSON Web Token (JWT) typically involves several stages, from creation to expiration and validation. JWTs are widely used for securely transmitting information between parties in the form of a signed and optionally encrypted token. Here’s an overview of the typical lifecycle: Token Creation
Authentication: When a user logs in, they typically provide credentials (e.g., username and password).
Server Validation: The server validates these credentials (e.g., checks the database for the user’s password).
JWT Creation: If the credentials are correct, the server generates a JWT. The token contains:
Header: Describes the signing algorithm (e.g., HS256, RS256) and token type (JWT).
Payload: Contains the claims. Claims are pieces of information (e.g., user ID, expiration time) that are encoded in the token.
Signature: The header and payload are signed with a secret key (HMAC) or a private key (RSA or ECDSA) to ensure integrity.
 
Download Jar file: jjwt-api-0.10.5.jar

To create a A143mk.JWTAPIConfig, we first need to define a package that will be handled in the configuration.
Create classes name in the A143mk.JWTAPIConfig package. 
  1.  JwtRequestFilter 
  2.  JwtUtil 
  3.  SecurityConfig 
  4.  AuthRequest
1. JwtRequestFilter
2.JwtUtil
3. SecurityConfig
4. AuthRequest/PayloderBeanClass
Create a Controller class create a simple controller name is AuthController an endpoint is accessed.

Steps to Test JWT Token in Postman:
Obtain the JWT Token: First, you need to obtain the JWT token from your authentication endpoint. Typically, this is done by making a POST request to your login endpoint with the correct user credentials (username, password, etc.). 
Example (login request):
{
  "username": "A143mk@Manoj"
}

Result :
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJCQVRBQGRtc0Vjb20iLCJpYXQiOjE3NDc5MDc5MzYsImV4cCI6MTc0NzkxMTUzNn0.1eEK4gwwR3mr2bqcIoYYjxwpcTzUKOCtBMH-GBo6X_4