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

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

View File

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