2017-06-19 70 views
0

我在我的應用程序中實現春季安全與JWT我自己的反應,當過未經授權的呼叫時,它返回以下響應如何發送春天

@Override 
    public void commence(HttpServletRequest request, 
         HttpServletResponse response, 
         AuthenticationException authException) throws IOException { 

     response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 
    } 

響應JSON看起來像下面

{ 
"timestamp": 1497832267379, 
"status": 401, 
"error": "Unauthorized", 
"message": "Unauthorized", 
"path": "/path" 
} 

取而代之的是,我可以送自己的自定義響應somethink喜歡:

{ 
"code":401, 
"message":"The request is unauthorized" 

} 

任何幫助表示讚賞

編輯

我的代碼更新到以下格式:

@Override 
    public void commence(HttpServletRequest request, 
         HttpServletResponse response, 
         AuthenticationException authException) throws IOException { 

       //response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 



       status UnautorizedEntry = new status(); 
       UnautorizedEntry.setCode(401); 
       UnautorizedEntry.setMessage("Unauthorized Entry"); 
       Map<String, Object> UnautorizedEntryResponse=new HashMap<String, Object>(); 
       UnautorizedEntryResponse.put("status", UnautorizedEntry); 
       objectMapper.writeValue(response.getOutputStream(), UnautorizedEntry); 
       response.flushBuffer(); 



    } 

我的狀態類如下:

public class status { 

    int code; 
    String message; 
    public int getCode() { 
     return code; 
    } 
    public void setCode(int code) { 
     this.code = code; 
    } 
    public String getMessage() { 
     return message; 
    } 
    public void setMessage(String message) { 
     this.message = message; 
    } 



} 

現在我得到一個200迴應,但沒有在屏幕上顯示。它完全blank.Any幫助表示讚賞

+0

你應該能夠覆蓋與自定義'AuthenticationFailureHandler'的響應。 – shmosel

+0

目前我重寫開始方法 – Ricky

+0

您的新代碼實際上並未設置狀態。你需要'response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);'使它工作。您必須告訴servlet引擎狀態發生了變化,或者它將默認爲200. – ngreen

回答

1

你可以嘗試添加控制器建議

@RestController 
@ControllerAdvice 
public class ExceptionHandlerController { 

    @ExceptionHandler(UsernameNotFoundException.class, DataAccessException.class) 
    @ResponseStatus(HttpStatus.SC_UNAUTHORIZED) 
    @ResponseBody ErrorInfo 
    UnauthorizeExceptionInfo(HttpServletRequest req, Exception ex) { 
     return new ErrorInfo(req.getRequestURL(), ex); 
    } 
} 

和ErrorInfo.class

@JsonIgnore 
public final StringBuffer url; 
public final String ex; 

public ErrorInfo(StringBuffer stringBuffer, Exception ex) { 
    this.url = stringBuffer; 
    this.ex = ex.getLocalizedMessage(); 
} 

並且當您將拋出新的UsernameNotFoundException時,控制器將處理響應。

我想如果密碼/電子郵件不匹配,異常將拋出CustomUserDetailsS​​ervice的@Override public loadUserByUsername。

更多細節在這裏:https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

1

這應該爲你工作:

@Autowired 
private ObjectMapper objectMapper; 

@Override 
public void commence(HttpServletRequest request, 
        HttpServletResponse response, 
        AuthenticationException authException) throws IOException { 
    // notify client of response body content type 
    response.addHeader("Content-Type", "application/json;charset=UTF-8"); 
    // set the response status code 
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
    // set up the response body 
    Status unauthorized = new Status(HttpServletResponse.SC_UNAUTHORIZED, 
            "The request is unauthorized"); 
    // write the response body 
    objectMapper.writeValue(response.getOutputStream(), unauthorized); 
    // commit the response 
    response.flushBuffer(); 
} 

public class Status { 
    private int code; 
    private String message; 

    public Status(int code, String message) { 
     this.code = code; 
     this.message = message; 
    } 

    public int getCode() { 
     return code; 
    } 

    public String getMessage() { 
     return message; 
    } 
} 

請注意,您需要

+0

我在其他REST控制器中使用jackson作爲@RequestMapping(value =「/ path」,產生= {MediaType.APPLICATION_JSON_VALUE },method = RequestMethod.GET)'。但在這種情況下,它不是一個REST控制器,所以我怎樣才能發送JSON響應使用傑克遜 – Ricky

+0

我添加了一個使用傑克遜的例子。 – ngreen

+0

我嘗試過這個示例,現在我得到一個200響應,但沒有顯示任何內容。我更新了問題代碼 – Ricky