Class JwtUtil

java.lang.Object
com.needleandstitch.pavuk.security.JwtUtil

@Component public class JwtUtil extends Object
Utility class for handling JWT token operations, such as generating, parsing, and validating JWT tokens.
Since:
15.12.2024
Version:
1.0.0
Author:
Needle & Stitch
  • Constructor Details

    • JwtUtil

      public JwtUtil()
  • Method Details

    • generateToken

      public String generateToken(User user)
      Generates a JWT token for a given user.
      Parameters:
      user - The user for whom the token is generated
      Returns:
      The generated JWT token
    • extractEmail

      public String extractEmail(String token)
      Extracts the email (subject) from the JWT token to identify the user associated with the token.
      Parameters:
      token - The JWT token from which to extract the email
      Returns:
      The email (subject) from the JWT token
    • extractExpiration

      public Date extractExpiration(String token)
      Extracts the expiration date from the JWT token.
      Parameters:
      token - The JWT token from which to extract the expiration date
      Returns:
      The expiration date of the JWT token
    • extractClaim

      public <T> T extractClaim(String token, Function<io.jsonwebtoken.Claims,T> claimsResolver)
      Extracts a specific claim from the JWT token.
      Type Parameters:
      T - Claim type
      Parameters:
      token - The JWT token from which to extract the claim
      claimsResolver - A function that extracts the desired claim from the Claims object
      Returns:
      The value of the extracted claim
    • isTokenValid

      public Boolean isTokenValid(String token)
      Validates the JWT token.
      Parameters:
      token - The JWT token to validate
      Returns:
      True if the token is valid, false otherwise