meu lombok tava dando erro, fiz a instalação dele pelo plugin e agora estou tendo isso
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'JWT_SECRET' in value "${JWT_SECRET}" Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tokenservice': Injection of autowired dependencies failed Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'filtersecurity': Unsatisfied dependency expressed through field 'tokenservice': Error creating bean with name 'tokenservice': Injection of autowired dependencies failed org.springframework.context.ApplicationContextException: Unable to start web server
import com.example.demo.repository.UsuarioRepositorio; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
@Component public class Filtersecurity extends OncePerRequestFilter { @Autowired private Tokenservice tokenservice; @Autowired private UsuarioRepositorio repository; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { var TokenJWT = recuperarToken(request); if (TokenJWT != null){ var subject = tokenservice.getsubject(TokenJWT); var Usuario = repository.findByLogin(subject);
var authentication = new UsernamePasswordAuthenticationToken(Usuario, null, Usuario.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
}
filterChain.doFilter(request,response);
}
private String recuperarToken(HttpServletRequest request) {
var authorization = request.getHeader("Authorization");
if (authorization == null || !authorization.startsWith("Bearer ")) {
return null;
}
return authorization.replace("Bearer ", "").trim();
}
} package com.example.demo.infra.securityconfiguration;
import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.exceptions.JWTCreationException; import com.auth0.jwt.exceptions.JWTVerificationException; import com.example.demo.domain.objeto.Usuario; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;
import javax.management.RuntimeMBeanException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.Date;
@Service public class Tokenservice { @Value("${api.security.token.secret}") private String secret; public String gerartoken(Usuario usuario){
try {
Algorithm algorithm = Algorithm.HMAC256(secret);
return JWT.create()
.withIssuer("API chat")
.withSubject(usuario.getLogin())
.withExpiresAt(Date.from(DataExpiracao()))
.sign(algorithm);
} catch (JWTCreationException exception){
throw new RuntimeException("erro ao gerar chave", exception);
}
}
public String getsubject(String tokenJWT){
try {
var algoritmo = Algorithm.HMAC256(secret);
return JWT.require(algoritmo)
.withIssuer("API chat")
.build()
.verify(tokenJWT)
.getSubject();
} catch (JWTVerificationException exception) {
throw new RuntimeException("Token JWT inválido ou expirado!");
}
}
private Instant DataExpiracao() {
return LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.of("-03:00"));
}
}