Se o token está ausente ou inválido o retorno não deveria ser 401?
Se o token está ausente ou inválido o retorno não deveria ser 401?
Olá, Francisco!
O padrão do Spring é devolver 403, mas você pode alterar esse comportamento criando uma classe EntryPoint:
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Acesso não autorizado");
}
}
Nesse exemplo, quando a autenticação falha (ou seja, o token está ausente ou inválido), o método commence
é chamado e retorna o status 401 com a mensagem "Acesso não autorizado".
Espero ter ajudado e bons estudos!