3
respostas

Erro em tokenService

Apareceu esse erro para mim e não consegui resolve-lo

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-06-24 10:38:38.230 ERROR 21592 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfigurations': Unsatisfied dependency expressed through field 'tokenService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tokenService': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'forum.jwt.expiration' in value "${forum.jwt.expiration}" at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.8.jar:5.3.8] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.8.jar:5.3.8]

3 respostas

Estou usando o java 11

Minhas classe tokenService

@Service public class TokenService {

@Value("${forum.jwt.expiration}")
private String expiration;

// a anotação @Value injeta as propriedades do arquivo application.properties
// como o token e o tempo de expiração

@Value("${forum.jwt.secret}")
private String secret;

public String geraToken(Authentication authentication) {
    Usuario logado = (Usuario) authentication.getPrincipal();
    Date hoje = new Date();
    Date dataExpericao = new Date(hoje.getTime() + Long.parseLong(expiration));

    return Jwts.builder()
            .setIssuer("API do Forum da Alura")
            .setSubject(logado.getId().toString())
            .setIssuedAt(hoje)
            .setExpiration(dataExpericao)
            .signWith(SignatureAlgorithm.HS256, secret)
            .compact();
}

public boolean isTokenValido(String token) {
    try {
        Jwts.parser().setSigningKey(this.secret).parseClaimsJws(token);
        return true;
    } catch (Exception e) {
        return false;
    }

}

public Long getIdUsuario(String token) {
    Claims claims = Jwts.parser().setSigningKey(this.secret).parseClaimsJws(token).getBody();
    return Long.parseLong(claims.getSubject());
}

}

Diego um dos erros apresentado ali é a respeito das variávies forum.jwt.secret e forum.jwt.expiration, que você precisa criar primeiro no arquivo application.properties

Então elas estão criadas no application.properties, fiz etapas de atualizar o projeto, removi e inseri de novo, porém continua com o erro