Spring security custom AuthenticationException message

前端 未结 4 823
太阳男子
太阳男子 2021-01-01 00:43

Hi i needed to add a new exception in Spring security login form, everything work perfectly except that i want to have my own error message (until now it display the \"wrong

4条回答
  •  忘掉有多难
    2021-01-01 01:25

    A very simple way is defining your custom message in exception handler (@ControllerAdvice), as following:

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    @ExceptionHandler(value = AuthenticationException.class)
    public ResponseModal handleAuthenticationExceptions(AuthenticationException ex, HttpServletResponse response) {
        LOGGER.info("Authentication Exception: {}", ex.getMessage());
        response.addCookie(new Cookie(JWTConfigurer.JWT_TOKEN_COOKIE, null));
        return new ResponseModal(HttpStatus.UNAUTHORIZED.value(), "whatever you want");
    }
    

提交回复
热议问题