Я могу обработать ошибку 404.Ошибка ручки 401 (весенняя безопасность)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseBody
public void noHandlerFoundException (HttpServletResponse response) throws IOException{
//some code
}
Но как обращаться с ошибкой 401?
Редактировать Я использую Java и не web.xml
Редактировать Что я должен положить в NoHandlerFoundException обрабатывать HttpStatus.UNAUTHORIZED
Редактировать
У меня есть метод unsuccessfulAuthentication , когда аутентификация не выполнена:
public class StatelessLoginFilter extends AbstractAuthenticationProcessingFilter {
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException, ServletException {
SecurityContextHolder.clearContext();
if (logger.isDebugEnabled()) {
logger.debug("Authentication request failed: " + failed.toString());
logger.debug("Updated SecurityContextHolder to contain null Authentication");
logger.debug("Delegating to authentication failure handler " + failureHandler);
}
// response.setCharacterEncoding("UTF-8");
// response.getWriter().write(jsonService.toString(jsonService.getResponse(false, "Не удалось авторизоваться", "401")));
rememberMeServices.loginFail(request, response);
failureHandler.onAuthenticationFailure(request, response, failed);
}
}
Этот код отправил 401 error html. Мне нужно отправить json, вы можете увидеть его в комментариях.
Возможный дубликат [Как я могу получить Spring-Security, чтобы вернуть ответ 401 в формате JSON?] (Http://stackoverflow.com/questions/22783583/how-can-i-get-spring- security-to-return-a-401-response-as-a-json-format) –