Class JWTUtil

java.lang.Object
vn.edu.uit.csbu.software_design.software_design_backend.jwt.JWTUtil

public class JWTUtil extends Object
The `JWTUtil` class provides methods to generate, validate, and extract information from JWT tokens using a secret key for signing.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    The function `extractName` extracts the subject from a JWT token after removing the "Bearer " prefix.
    static String
    The `generateToken` function creates a JWT token with the given ID, issued at the current time, and set to expire after a specified duration.
    static boolean
    The function `validateToken` checks if a given token is valid by parsing its claims using a specified key.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JWTUtil

      public JWTUtil()
  • Method Details

    • generateToken

      public static String generateToken(String id)
      The `generateToken` function creates a JWT token with the given ID, issued at the current time, and set to expire after a specified duration.
      Parameters:
      id - The `id` parameter in the `generateToken` method represents the subject of the JWT (JSON Web Token). It is typically a unique identifier for the entity (user, client, etc.) for whom the token is being generated. This subject is usually used to identify the recipient of the token when
      Returns:
      A JWT (JSON Web Token) is being returned by the `generateToken` method.
    • validateToken

      public static boolean validateToken(String token)
      The function `validateToken` checks if a given token is valid by parsing its claims using a specified key.
      Parameters:
      token - The `token` parameter in the `validateToken` method is a string that represents a JSON Web Token (JWT). This method attempts to parse and validate the JWT using the `Jwts` class from the io.jsonwebtoken library. If the token is successfully parsed and verified, the method returns
      Returns:
      The `validateToken` method returns a boolean value - `true` if the token is successfully validated, and `false` if an exception occurs during the validation process.
    • extractName

      public static String extractName(String token)
      The function `extractName` extracts the subject from a JWT token after removing the "Bearer " prefix.
      Parameters:
      token - The `token` parameter is a string that represents a JSON Web Token (JWT) which typically consists of three parts separated by dots: header, payload, and signature. In the `extractName` method, the token is expected to be a JWT where the subject claim is extracted from the payload part
      Returns:
      The method `extractName` returns the subject extracted from a JWT token after parsing it.