すべてHandlerInterceptorAdapter
のリクエストを傍受し、ユーザー認証チェックを実行する があります。非常に基本的に:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
User user = ... // get user
checkIfAuthorized(user); // throws AuthorizationException
return true;
}
次に、その@ExceptionHandler
ための がありAuthorizationException
ます。
@ExceptionHandler(value = AuthorizationException.class)
public ResponseEntity<String> handleNotAuthorized(AuthorizationException e) {
// TODO Custom EXCEPTION HANDLER for json/jsp/xml/other types, based on content type
ResponseEntity<String> responseEntity = new ResponseEntity<>("You are not authorized to access that page.", HttpStatus.UNAUTHORIZED);
return responseEntity;
}
(許可されていない) 要求が受け入れtext/plain
られる (そして json 用に簡単に変更できる) 場合、これは問題ありません。@ExceptionHandler
特定のAccept
ヘッダーに異なる を作成するにはどうすればよいですか?
@RequestMapping
持っていproduces()
ます。に似たものはあり@ExceptionHandler
ますか?