Class JwtServiceImpl

java.lang.Object
api.mpba.rastvdmy.service.impl.JwtServiceImpl
All Implemented Interfaces:
JwtService

@Service public class JwtServiceImpl extends Object implements JwtService
Implementation of the JwtService interface for managing JSON Web Tokens (JWT). This service provides methods for token generation, extraction, validation, and claim handling using the specified secret key and expiration time.
  • Constructor Details

    • JwtServiceImpl

      public JwtServiceImpl()
  • Method Details

    • extractUsername

      public String extractUsername(String token)
      Extracts the username from the given JWT token.
      Specified by:
      extractUsername in interface JwtService
      Parameters:
      token - the JWT token
      Returns:
      the username extracted from the token
    • extractToken

      public String extractToken(jakarta.servlet.http.HttpServletRequest request)
      Extracts the JWT token from the HTTP request's Authorization header.
      Specified by:
      extractToken in interface JwtService
      Parameters:
      request - the HTTP request containing the Authorization header
      Returns:
      the extracted token, or null if no valid token is found
    • generateToken

      public String generateToken(org.springframework.security.core.userdetails.UserDetails userDetails)
      Generates a JWT token for the given user details.
      Specified by:
      generateToken in interface JwtService
      Parameters:
      userDetails - the user details for which the token is to be generated
      Returns:
      the generated JWT token
    • getExpirationTime

      public long getExpirationTime()
      Retrieves the expiration time for the JWT token.
      Specified by:
      getExpirationTime in interface JwtService
      Returns:
      the expiration time in milliseconds
    • isTokenValid

      public boolean isTokenValid(String token, org.springframework.security.core.userdetails.UserDetails userDetails)
      Validates the given JWT token against the provided user details.
      Specified by:
      isTokenValid in interface JwtService
      Parameters:
      token - the JWT token to validate
      userDetails - the user details to compare against
      Returns:
      true if the token is valid, false otherwise
    • extractSubject

      public String extractSubject(String token)
      Extracts the subject from the given JWT token.
      Specified by:
      extractSubject in interface JwtService
      Parameters:
      token - the JWT token
      Returns:
      the subject extracted from the token