Powered by Blogger.

Cross-Origin Resource Sharing

No comments :
CROSS is a system that allows client web applications (such as JavaScript code) loaded on one domain to interact with resources located on a different domain.
Origin: An "Origin" (or source) URL consists of three parts: 
Protocol (scheme): Such as http or https.
Domain (host): Such as a143mk.blogspot.com or localhost. 
Port (port number): Such as 8080 or 3000. 
If any of these three parts are different in a request, it is called a Cross-Origin request.
Example:
If your frontend (endpoints.com) sends a request to a backend API (api.com), it is Cross-Origin and will be intercepted by the browser. CORS is a way to relax this security restriction in a controlled manner, allowing legitimate cross-origin communication.
 


Configuring CORS globally in Spring Boot is the preferred approach, as it saves you from having to apply the @CrossOrigin annotation to every controller or method and keeps the configuration centralized.
Example: This method defines global CORS settings 
  1.  addMapping("/**"): This indicates that the CORS mapping will apply to all paths. 
  2.  allowedOrigins: Defines the client origins that are allowed to access your API. 
  3.  allowedMethods: The HTTP methods that are allowed. 
  4.  allowedHeaders: Allows all headers sent by the client. 
  5.  allowedCredentials: Set this to 'true' if the client is using cookies or HTTP authentication. 
  6.  maxAge: The time (in seconds) to cache the results of a pre-flight request.

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