私はこのようなカスタムアノテーションを持っている:Spring @ExceptionHandlerでカスタムアノテーションをキャッチするには?
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@PreAuthorize(AllowedForSystemUsers.condition)
public @interface AllowedForSystemUsers {
String condition = "hasAnyRole({'ROLE_ADMIN', 'ROLE_USER'})";
}
をし、私はこのような私のController
方法で注釈を入れて:だから
@ExceptionHandler(value = AccessDeniedException.class)
public ModelAndView accessDenied(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) {
return new ModelAndView("redirect:/error");
}
とき:このような
@RequestMapping(value="/search", method=RequestMethod.POST)
@AllowedForSystemUsers
public String searchRooms(@ModelAttribute Booking booking, Model model, long type) {
//do something
}
マイExceptionHandler
ユーザーがROLE_ADMIN
またはROLE_USER
のような認証を持っていない場合、AccessDeniedException
が発生し、方法はaccessDenied()
に進みます。
質問@AllowedForSystemUsers
注釈がこのAccessDeniedException
をトリガーするかどうかは私の質問です。他のコードにも手を加えることがあります。AccessDeniedException
ありがとう!
コントローラに例外を投げる必要があります。グローバル例外ハンドラを実行するには、他の場所から例外をスローする必要があります。 –
'AccessDeniedException'のサブクラスを作成し、' @ AllowedForSystemUsers'処理中にそれをスローすることが考えられます –
@SungJinSteveYoo? – YOUNG