Class JWTHelperImpl
java.lang.Object
com.example.blogs.app.api.auth.service.JWTHelperImpl
- All Implemented Interfaces:
JWTHelper
Generates JWT tokens using JJWT library with HMAC SHA256 signing.
-
Constructor Summary
ConstructorsConstructorDescriptionJWTHelperImpl(String secretKey, io.jsonwebtoken.JwtParser jwtParser) Constructs a JWTHelperImpl with the configured secret key. -
Method Summary
Modifier and TypeMethodDescriptiongenerateToken(String subject, Duration expiration) Generates a JWT token with no additional claims.Generates a JWT token with custom claims and expiration.parseClaims(String token) Parses and extracts claims from a JWT token.
-
Constructor Details
-
JWTHelperImpl
public JWTHelperImpl(@Value("${jwt.secret-key}") String secretKey, io.jsonwebtoken.JwtParser jwtParser) Constructs a JWTHelperImpl with the configured secret key.- Parameters:
secretKey- the secret key used for signing tokens, loaded from application propertiesjwtParser- the JWT parser for validating and parsing tokens
-
-
Method Details
-
generateToken
Generates a JWT token with no additional claims.- Specified by:
generateTokenin interfaceJWTHelper- Parameters:
subject- the token subject (typically user ID)expiration- time until expiration- Returns:
- signed JWT token string
-
generateToken
Generates a JWT token with custom claims and expiration.- Specified by:
generateTokenin interfaceJWTHelper- Parameters:
subject- the token subject (typically user ID)claims- additional JWT claims to include in payloadexpiration- time until expiration- Returns:
- signed JWT token string
-
parseClaims
Parses and extracts claims from a JWT token.- Specified by:
parseClaimsin interfaceJWTHelper- Parameters:
token- the JWT token to parse- Returns:
- map of claims from the token payload
- Throws:
FailedToParseClaimsException- if token is malformed or signature is invalid
-