Class JwtService

java.lang.Object
com.nicholasalfonso.lato.security.JwtService

@Service public class JwtService extends Object
Service for generating and validating JWT access tokens.
  • Constructor Details

    • JwtService

      public JwtService(@Value("${lato.jwt.secret}") String secret, @Value("${lato.jwt.expiration-ms}") long expirationMs)
      Creates the JWT service using configured secret and expiration values.
      Parameters:
      secret - the HMAC signing secret from configuration
      expirationMs - access token lifetime in milliseconds
  • Method Details

    • generateAccessToken

      public String generateAccessToken(Long userId, String email)
      Generates a signed access token for the given user.
      Parameters:
      userId - the authenticated user's database ID
      email - the authenticated user's email address
      Returns:
      compact JWT access token string
    • generateToken

      public String generateToken(Long userId, String email, long expiration)
      Generates a signed JWT with a custom expiration interval.
      Parameters:
      userId - the subject user ID
      email - the email claim value
      expiration - token lifetime in milliseconds
      Returns:
      compact JWT token string
    • validateAccessToken

      public boolean validateAccessToken(String token)
      Validates the signature and expiration of an access token.
      Parameters:
      token - the JWT access token to validate
      Returns:
      true if the token is valid
    • extractUserId

      public Long extractUserId(String token)
      Extracts the user ID from a validated access token.
      Parameters:
      token - the JWT access token
      Returns:
      the user ID stored in the token subject