2017-04-13 64 views
0

我有一个Spring REST API,它提供了许多端点;他们需要使用基本http或JWT令牌进行身份验证。我使用的也是@ControllerAdvice就象这样:Spring REST API,拦截401需要完全验证

@ControllerAdvice 
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { 
    private Logger log = LogManager.getLogger(); 

    @Inject 
    private MessageSource messageSource; 


    @ExceptionHandler(value = { RuntimeException.class, Exception.class }) 
    public ResponseEntity<ErrorMessage> exceptionHandler(RuntimeException ex, Locale locale) {   
     ErrorMessage error = new ErrorMessage(ApiExceptionCodes.GENERIC_INTERNAL_SERVER_ERROR, messageSource.getMessage(
       ApiExceptionCodes.GENERIC_INTERNAL_SERVER_ERROR + "", null, locale));   
     return new ResponseEntity<ErrorMessage>(error, HttpStatus.INTERNAL_SERVER_ERROR); 
    } 

    @ExceptionHandler(value = { ApiException.class }) 
    public ResponseEntity<ErrorMessage> handlerApiException(ApiException ex, Locale locale) {   
     ErrorMessage error = new ErrorMessage(ex.getCode(), ex.getMessage()); 
     return new ResponseEntity<ErrorMessage>(error, HttpStatus.INTERNAL_SERVER_ERROR); 
    } 

    @ExceptionHandler(value = { ApiRuntimeException.class }) 
    public ResponseEntity<ErrorMessage> handlerApiRuntimeException(ApiRuntimeException ex, Locale locale) { 
     ErrorMessage error = new ErrorMessage(ex.getCode(), messageSource.getMessage(ex.getCode() + "", null, locale)); 
     return new ResponseEntity<ErrorMessage>(error, HttpStatus.INTERNAL_SERVER_ERROR); 
    } 

我的一个端点是这样的:

@ApiOperation(value = "Return Spring Principal object.", notes = "Return Spring Principal object. It contains several details of the logged-in user.") 
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
@PreAuthorize("hasAnyRole('B2B','API')") 
public ResponseEntity<?> user(Principal user) { 
    return ResponseEntity.ok(user); 
} 

总之,我在我的应用程序在全球捕获的异常。除了我尝试调用端点而没有进行身份验证之外,一切正常。在这种情况下,异常不是由我@ControllerAdvice拦截和用户看到这个身体的反应:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> 
     <title>Error 401 Full authentication is required to access this resource</title> 
    </head> 
    <body> 
     <h2>HTTP ERROR 401</h2> 
     <p>Problem accessing /aci/api/v1/tickets/chart. Reason: 

      <pre> Full authentication is required to access this resource</pre> 
     </p> 
     <hr> 
     <a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.9.v20160517</a> 
     <hr/> 
    </body> 
</html> 

我怎么能在全球范围还截获此异常?请建议我采取最佳做法。

回答

2

为了基本的安全,你可以扩展org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint并覆盖动工方法来回报您的自定义响应,例如:

@Override 
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { 
    response.addHeader("your_custom_header", "some_value"); 
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
    PrintWriter writer = response.getWriter(); 
    writer.println("HTTP Status 401 - " + authException.getMessage()); 
}