...
| Bloque de código | ||
|---|---|---|
| ||
@Log4j2
@Component
public class JwtTokenUtil {
@Value( "${es.um.jwt.secret}" )
private String secret;
// In minutes
@Value( "${es.um.jwt.expires}" )
private long expires;
/**
* Get the claim from the token If token expirated throw TokenExpiredException If token is invalid throw
* UnauthorizedException
*/
public Claims getClaim( String token ) {
final String[] tokenSplits = token.split( " " );
if ( !tokenSplits[0].equals( "Bearer" ) ) {
throw new UnauthorizedException();
} else {
token = tokenSplits[1];
try {
final Claims claims = Jwts.parser().setSigningKey( TextCodec.BASE64.encode( secret ) )
.parseClaimsJws( token ).getBody();
log.debug( "El claims es: " + claims.toString() );
log.debug( "El subject es: " + claims.getSubject() );
final Date expiration = claims.getExpiration();
if ( Instant.now().isAfter( expiration.toInstant() ) ) {
log.error( "Expiration Date: " + expiration.toString() );
log.error( "Current Instant: " + Instant.now().toString() );
log.error( "Error el token " + token + " está caducado" );
throw new TokenExpiredException();
}
return claims;
} catch ( final ExpiredJwtException e ) {
log.error( "Error el token está caducado" );
throw new TokenExpiredException();
} catch ( final JwtException e ) {
log.error( "Error filtering:" + e );
throw new UnauthorizedException();
}
}
}
} |
...