Compare commits
3 Commits
1160af5711
...
d308f14df4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d308f14df4 | ||
|
|
a36705d83f | ||
|
|
ab802cdb25 |
11
src/main/java/com/example/demo/aop/aspect/SessionCheck.java
Normal file
11
src/main/java/com/example/demo/aop/aspect/SessionCheck.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.example.demo.aop.aspect;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface SessionCheck {
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.demo.aop.aspect;
|
||||
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.example.demo.exception.NoSessionException;
|
||||
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class SessionCheckAspect {
|
||||
|
||||
HttpServletRequest request;
|
||||
|
||||
@Autowired
|
||||
public SessionCheckAspect(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
@Pointcut("@annotation(com.example.demo.aop.aspect.SessionCheck)")
|
||||
public void sessionCheckMethod() {
|
||||
}
|
||||
|
||||
@Before("sessionCheckMethod()")
|
||||
public void checkSession() {
|
||||
HttpSession session = request.getSession(false);
|
||||
|
||||
if (session == null || session.getAttribute("loginUser") == null) {
|
||||
throw new NoSessionException("不正なログインです");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.example.demo.aop.handler;
|
||||
|
||||
|
||||
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 com.example.demo.exception.NoSessionException;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private final static Logger LOGGER = Logger.getLogger(GlobalExceptionHandler.class.getName());
|
||||
|
||||
private final String SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION = "不正な遷移です。";
|
||||
private final String SEVERE_ERROR_MESSAGE_BAD_REQUEST = "不正なリクエストです。";
|
||||
|
||||
@ExceptionHandler(NoSessionException.class)
|
||||
public String handleNoSessionException(Model model, Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
|
||||
|
||||
model.addAttribute(
|
||||
"errorMessage",
|
||||
"システムエラーが発生しました。"
|
||||
+ "初めからやり直してください。");
|
||||
|
||||
return "error";
|
||||
|
||||
}
|
||||
|
||||
@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 "error";
|
||||
}
|
||||
|
||||
@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 "error";
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.example.demo.exception;
|
||||
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public String handleException(
|
||||
Exception e,
|
||||
Model model) {
|
||||
|
||||
model.addAttribute(
|
||||
"errorMessage",
|
||||
"システムエラーが発生しました。"
|
||||
+ "初めからやり直してください。");
|
||||
|
||||
return "error";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.demo.exception;
|
||||
|
||||
public class InsufficientPermissionException extends RuntimeException {
|
||||
|
||||
public InsufficientPermissionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.demo.exception;
|
||||
|
||||
public class NoSessionException extends RuntimeException {
|
||||
|
||||
public NoSessionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user