content: package com.example.apigatewayservice.security;
  
  import io.github.cdimascio.dotenv.Dotenv;
  import io.jsonwebtoken.Claims;
  import io.jsonwebtoken.Jwts;
  import io.jsonwebtoken.io.Decoders;
  import io.jsonwebtoken.security.Keys;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.stereotype.Component;
  import java.security.Key;
  import java.util.Date;
  import java.util.function.Function;
  
  @Component
  @Slf4j
  public class JwtService {
  
      private final String secretKey;
  
      public JwtService() {
          Dotenv dotenv = Dotenv.load();
          this.secretKey = dotenv.get("SECRET_KEY");
      }
  
      public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
          final Claims claims = extractAllClaims(token);
          return claimsResolver.apply(claims);
      }
  
      public boolean isTokenExpired(String token) {
          return extractExpiration(token).before(new Date());
      }
  
      private Date extractExpiration(String token) {
          return extractClaim(token, Claims::getExpiration);
      }
  
      public Claims extractAllClaims(String token) {
          return Jwts
                  .parserBuilder()
                  .setSigningKey(getSigningKey())
                  .build()
                  .parseClaimsJws(token)
                  .getBody();
      }
  
      private Key getSigningKey() {
          byte[] keyBytes = Decoders.BASE64.decode(secretKey);
          return Keys.hmacShaKeyFor(keyBytes);
      }
  
      public boolean validateToken(String token) {
          try {
              Jwts.parserBuilder().setSigningKey(getSigningKey()).build().parseClaimsJws(token);
              log.warn("Token validation passed.");
              return !isTokenExpired(token);
          } catch (Exception e) {
              return false;
          }
      }
  }
  