Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erra na criação do token : The constructor RuntimeErrorException(String, JWTCreationException) is undefined

Rodrigo, estou obtendo um erro de compilação na criação do token:

"The constructor RuntimeErrorException(String, JWTCreationException) is undefined"

Estou utilizando o Eclipse.

java-jwt 4.2.1

Alguma sugestão?

package med.voll.api.infra.security;



import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

import javax.management.RuntimeErrorException;

import org.springframework.stereotype.Service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;

import med.voll.api.domain.usuario.Usuario;


@Service
public class TokenService {
    public String gerarToken(Usuario usuario) {
        try {
            var algoritmo = Algorithm.HMAC256("123456");
            return JWT.create()
                .withIssuer("API Voll med")
                .withSubject(usuario.getLogin())
                .withExpiresAt(dataExpiracao())
                .sign(algoritmo);
        } catch (JWTCreationException e){
            throw new RuntimeErrorException("Erro na geração do token jwt", e);
        }	
    }

    private Instant dataExpiracao() {
        // TODO Auto-generated method stub
        return LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.of("-03:00"));
    }

}
2 respostas
solução!

Oi!

Você utilizou a exception incorreta:

throw new RuntimeErrorException("Erro na geração do token jwt", e);

Deveria ser:

throw new RuntimeException("Erro na geração do token jwt", e);

Obrigado.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software