[EDIT] 権限エラー時の遷移先変更

This commit is contained in:
s-choi
2026-09-10 15:56:20 +09:00
parent fc84fc3fda
commit 9500eac50f

View File

@@ -1,25 +1,22 @@
package com.example.rhizome.aop.Handler;
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.LoginInfoForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import com.example.rhizome.exception.InsufficientPermissionException;
import com.example.rhizome.exception.NoSessionException;
import static com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.LoginInfoForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import com.example.rhizome.exception.InsufficientPermissionException;
import com.example.rhizome.exception.NoSessionException;
import static com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword.LOGIN_INFO_FORM;
import static com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword.MESSAGE_ERROR;
/**
* 例外処理用のコントローラ
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@@ -28,12 +25,6 @@ public class GlobalExceptionHandler {
private final String SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION = "不正な遷移です。";
private final String SEVERE_ERROR_MESSAGE_BAD_REQUEST = "不正なリクエストです。";
/**
* セッションが切れている場合の例外処理
*
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@ExceptionHandler(NoSessionException.class)
public String handleNoSessionException(Model model, Exception e) {
@@ -42,18 +33,11 @@ public class GlobalExceptionHandler {
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
return TransitionTargetPageNameKeyword.LOGIN_HTML;
}
/**
* 権限がない場合の例外処理
*
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@ExceptionHandler(InsufficientPermissionException.class)
public String handleInsufficientPermissionException(Model model, Exception e) {
@@ -62,50 +46,32 @@ public class GlobalExceptionHandler {
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
return TransitionTargetPageNameKeyword.LOGIN_HTML;
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
/**
* リクエストメソッドが不正な場合の例外処理
*
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public String handleBadRequestException(Model model, Exception e) {
// エラーメッセージやその他の情報をモデルに追加する
model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST);
model.addAttribute("status", HttpStatus.BAD_REQUEST.value());
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
// カスタムエラーページにリダイレクト
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
/**
* その他の例外処理
*
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@ExceptionHandler(Exception.class)
public String AllException(Model model, Exception e) {
// エラーメッセージやその他の情報をモデルに追加する
model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST);
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
// カスタムエラーページにリダイレクト
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}