Class JwtService
java.lang.Object
com.nicholasalfonso.lato.security.JwtService
Service for generating and validating JWT access tokens.
-
Constructor Summary
ConstructorsConstructorDescriptionJwtService(String secret, long expirationMs) Creates the JWT service using configured secret and expiration values. -
Method Summary
Modifier and TypeMethodDescriptionextractUserId(String token) Extracts the user ID from a validated access token.generateAccessToken(Long userId, String email) Generates a signed access token for the given user.generateToken(Long userId, String email, long expiration) Generates a signed JWT with a custom expiration interval.booleanvalidateAccessToken(String token) Validates the signature and expiration of an access token.
-
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 configurationexpirationMs- access token lifetime in milliseconds
-
-
Method Details
-
generateAccessToken
Generates a signed access token for the given user.- Parameters:
userId- the authenticated user's database IDemail- the authenticated user's email address- Returns:
- compact JWT access token string
-
generateToken
Generates a signed JWT with a custom expiration interval.- Parameters:
userId- the subject user IDemail- the email claim valueexpiration- token lifetime in milliseconds- Returns:
- compact JWT token string
-
validateAccessToken
Validates the signature and expiration of an access token.- Parameters:
token- the JWT access token to validate- Returns:
trueif the token is valid
-
extractUserId
Extracts the user ID from a validated access token.- Parameters:
token- the JWT access token- Returns:
- the user ID stored in the token subject
-