Customize auth error from Spring Security using OAuth2

后端 未结 4 2173
北恋
北恋 2020-12-14 22:17

I was wondering if I could customize the following authorization error:

{
  \"error\": \"unauthorized\",
  \"error_description\": \"Full authentication is re         


        
4条回答
  •  情书的邮戳
    2020-12-14 23:05

    I think you can use @ControllerAdviceto catch the exception unauthorized then format response as your expectation and return it. Something like this:

    @ResponseBody
    @ExceptionHandler(CustomException.class)
    @ResponseStatus(value=HttpStatus.UNAUTHORIZED, reason="Exception message")
    public JsonResponse unAuthorised(HttpServletRequest request, Exception ex) {
        return new JsonResponse("ERROR", 401, "Unauthorised Request");
    }
    

    Hope this help.

提交回复
热议问题