2015-09-15 3 views
4

Я могу обработать ошибку 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, вы можете увидеть его в комментариях.

+1

Возможный дубликат [Как я могу получить Spring-Security, чтобы вернуть ответ 401 в формате JSON?] (Http://stackoverflow.com/questions/22783583/how-can-i-get-spring- security-to-return-a-401-response-as-a-json-format) –

ответ

1

Вот полный обработчик для всех небольшой набор страниц ошибок:

@Controller 
public class ErrorCodeController extends BaseController { 

    @ExceptionHandler(ApplicationException.class) 
    @RequestMapping(value="errorPage400", method=RequestMethod.GET) 
    @ResponseStatus(value = HttpStatus.BAD_REQUEST) 
    public String handleBadRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
     map.addAttribute("http-error-code", HttpStatus.BAD_REQUEST); 
     return processErrorCodes(ex,response,map); 
    } 


    @ExceptionHandler(ApplicationException.class) 
    @RequestMapping(value="errorPage401", method=RequestMethod.GET) 
    @ResponseStatus(value=HttpStatus.UNAUTHORIZED,reason="Unauthorized Request") 
    public String handleUnauthorizedRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
     map.addAttribute("http-error-code", HttpStatus.UNAUTHORIZED); 
     return processErrorCodes(ex,response,map); 
    } 


    @ExceptionHandler(ApplicationException.class) 
    @RequestMapping(value="errorPage404", method=RequestMethod.GET) 
    @ResponseStatus(HttpStatus.NOT_FOUND) 
    public String handleNotFoundRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
     map.addAttribute("http-error-code", HttpStatus.NOT_FOUND); 
     return processErrorCodes(ex,response,map); 
    } 


    @ExceptionHandler(ApplicationException.class) 
    @RequestMapping(value="errorPage500", method=RequestMethod.GET) 
    @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR,reason="Internal Server Error") 
    public String handleInternalServerError(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
     map.addAttribute("http-error-code", HttpStatus.INTERNAL_SERVER_ERROR); 
     return processErrorCodes(ex,response,map); 
    } 

    @ExceptionHandler(ApplicationException.class) 
    public void handleApplicationExceptions(Throwable exception, HttpServletResponse response) { 

    } 

    private String processErrorCodes(ApplicationException ex,HttpServletResponse response, ModelMap map){ 
     map.addAttribute("class", ClassUtils.getShortName(ex.getClass())); 
     map.addAttribute("message", ex.getMessage()); 
     map.addAttribute("errorMessage", ex.getErrorMessage()); 
     map.addAttribute("errorCode", ex.getErrorCode()); 
     map.addAttribute("timestamp", ex.getCurrentDate()); 
     return "errorPage"; 
    } 


} 

Базовый контроллер:

@Controller 
@RequestMapping("/") 
public class BaseController { 

    // Remember to add any exception that you suspect can be thrown in this web application. 


@ExceptionHandler({ApplicationException.class,NullPointerException.class}) 
    public ModelAndView handleException(Throwable exception,HttpServletRequest req) { 

     ModelMap model = new ModelMap(); 
     ApplicationException ex = new ApplicationException(); 
     String timeStamp = ex.getCurrentDate().toString(); 
     //String temp = ClassUtils.getShortName(ex.getClass()); 
     //model.addAttribute("class", ClassUtils.getShortName(ex.getClass())); 
     model.addAttribute("timeStamp", timeStamp); 
     return new ModelAndView("errorPage", model); 
    } 

web.xml:

<web-app id="WebApp_ID" version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <display-name>myApp</display-name> 
    <error-page> 
     <error-code>400</error-code> 
     <location>/errorPage400.xhtml</location> 
    </error-page> 
    <error-page> 
     <error-code>401</error-code> 
     <location>/errorPage401.xhtml</location> 
    </error-page> 
    <error-page> 
     <error-code>404</error-code> 
     <location>/errorPage404.xhtml</location> 
    </error-page> 
    <error-page> 
     <error-code>500</error-code> 
     <location>/errorPage500.xhtml</location> 
    </error-page> 
    <error-page> 
     <exception-type>java.lang.Throwable</exception-type> 
     <location>/errorPage.xhtml</location> 
    </error-page> 
+0

Я использую java Config, а не web.xml И мне нужно отправить json, а не перенаправлять на страницу с ошибкой – annoirq

+1

, и мы должны знать это от ..? Отформатируйте свой пост. –

0

Я знаю, что это старая тема, но я struggeled, как хорошо, так вот мое решение, которое хорошо работает только для Tomcat.

Выполните следующий метод в вашем *DispatcherServletInitializer/WebAppInitializer#onStartup(...) метод.

Field appContextInFacade = ApplicationContextFacade.class.getDeclaredField("context"); 
appContextInFacade.setAccessible(true); 
ApplicationContext appContext = (ApplicationContext) appContextInFacade.get(servletContext); 
Field appContextInContext = ApplicationContext.class.getDeclaredField("context"); 
appContextInContext.setAccessible(true); 
StandardContext context = (StandardContext) appContextInContext.get(appContext); 
ErrorPage defaultErrorPage = new ErrorPage(); 
defaultErrorPage.setLocation("/myinternalerrorhandlercontroller"); 
context.addErrorPage(defaultErrorPage); // You may have to use reflection here as well. 

Затем добавить контроллер, который способен обрабатывать эти запросы об ошибках:

@RequestMapping("/myinternalerrorhandlercontroller") 
public ResponseEntity<T> renderErrorPage(HttpServletRequest httpRequest) 

Вы можете извлечь сведения об ошибке с помощью:

(Integer) httpRequest.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); 

Важное замечание:

  • Вы должны обрабатывать все RequestMethod s.
  • Вы должны обрабатывать все Content-Type s (=> Вернуть байты или зарегистрировать резервный HttpMessageConverter, который может преобразовать ваш объект ошибки в json, независимо от того, что было запрошено).
  • Ваша страница ошибки никогда не должна бросать исключения или не возвращать ответ.
  • Страница ошибки отображается только в том случае, если обычные @ExceptionHandler или аналогичные механизмы не вызываются или (не выполняются).
  • Не обращайтесь в состояние своих приложений, таких как auth пользователя, поскольку вы находитесь за пределами их области действия, и они могут быть недоступны или содержать недопустимые данные.
Смежные вопросы