Boa tarde Rodrigo, tudo bem?
Ao executar o método DELETE no postman, estou recebendo a seguinte mensagem no console do Eclipse:
java.lang.NumberFormatException: For input string: "br.com.alura.forum.modelo.Usuario@20"
at java.lang.NumberFormatException.forInputString(Unknown Source) ~[na:1.8.0_201]
at java.lang.Long.parseLong(Unknown Source) ~[na:1.8.0_201]
at java.lang.Long.parseLong(Unknown Source) ~[na:1.8.0_201]
at br.com.alura.forum.config.TokenSecurity.getUsuarioId(TokenSecurity.java:54) ~[classes/:na]
at br.com.alura.forum.config.AutenticacaoTokenFilter.autenticarCliente(AutenticacaoTokenFilter.java:44) ~[classes/:na]
at br.com.alura.forum.config.AutenticacaoTokenFilter.doFilterInternal(AutenticacaoTokenFilter.java:36) ~[classes/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
Poderia me dar uma luz sobre o que pode estar errado?
Seguem as classes:
@Service
public class TokenSecurity {
@Value("${forum.jwt.expiration}")
private String expiration;
@Value("${forum.jwt.secret}")
private String secret;
public String gerarToken(Authentication authentication) {
Usuario logado = (Usuario) authentication.getPrincipal();
Date hoje = new Date();
Date expiracao = new Date(hoje.getTime() + Long.parseLong(expiration));
return Jwts.builder()
.setIssuer("API do Fórum da Alura")
.setSubject(logado.toString())
.setIssuedAt(hoje)
.setExpiration(expiracao)
.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 getUsuarioId(String token) {
Claims claims = Jwts.parser().setSigningKey(this.secret).parseClaimsJws(token).getBody();
return Long.parseLong(claims.getSubject());
}
}
public class AutenticacaoTokenFilter extends OncePerRequestFilter{
private TokenSecurity tokenSecurity;
private UsuarioRepository repository;
public AutenticacaoTokenFilter(TokenSecurity tokenSecurity, UsuarioRepository repository) {
this.tokenSecurity = tokenSecurity;
this.repository = repository;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String token = recuperarToken(request);
Boolean valido = tokenSecurity.isTokenValido(token);
if(valido) {
autenticarCliente(token);
}
filterChain.doFilter(request, response);
}
private void autenticarCliente(String token) {
Long usuarioId = tokenSecurity.getUsuarioId(token);
Usuario usuario = repository.findById(usuarioId).get();
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(usuario, null, usuario.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
}
private String recuperarToken(HttpServletRequest request) {
String token = request.getHeader("Authorization");
if(token == null || token.isEmpty() || !token.startsWith("Bearer ")) {
return null;
}
return token.substring(7, token.length());
}
}
Desde já agradeço pela ajuda!