@Service
public class TokenService {
@Value("${api.security.token.secret}")
private String secret;
public String generarToken(Usuario usuario) {
try {
var algoritmo = Algorithm.HMAC256(secret);
return JWT.create()
.withIssuer("API Voll.med")
.withSubject(usuario.getLogin())
.withExpiresAt(fechaExpiracion())
.sign(algoritmo);
} catch (JWTCreationException exception){
throw new RuntimeException("error al generar el token jwt", exception);
}
}
private Instant fechaExpiracion() {
return LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.of("-03:00"));
}
}