2017-04-13 7 views
0

私は、多くのエンドポイントを提供するSpring REST APIを用意しています。 Basic httpまたはJWTトークンを使用した認証が必要です。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を使用しています。がなくても、エンドポイントを呼び出すときに例外を除いて、すべて正常に動作します。その場合には例外が私の@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()); 
} 
関連する問題