Class JWTHelperImpl

java.lang.Object
com.example.blogs.app.api.auth.service.JWTHelperImpl
All Implemented Interfaces:
JWTHelper

@Component public class JWTHelperImpl extends Object implements JWTHelper
Generates JWT tokens using JJWT library with HMAC SHA256 signing.
  • 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 properties
      jwtParser - the JWT parser for validating and parsing tokens
  • Method Details

    • generateToken

      public String generateToken(String subject, Duration expiration)
      Generates a JWT token with no additional claims.
      Specified by:
      generateToken in interface JWTHelper
      Parameters:
      subject - the token subject (typically user ID)
      expiration - time until expiration
      Returns:
      signed JWT token string
    • generateToken

      public String generateToken(String subject, Map<String,Object> claims, Duration expiration)
      Generates a JWT token with custom claims and expiration.
      Specified by:
      generateToken in interface JWTHelper
      Parameters:
      subject - the token subject (typically user ID)
      claims - additional JWT claims to include in payload
      expiration - time until expiration
      Returns:
      signed JWT token string
    • parseClaims

      public Map<String,Object> parseClaims(String token)
      Parses and extracts claims from a JWT token.
      Specified by:
      parseClaims in interface JWTHelper
      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