public class CheckLoginInterceptor implements HandlerInterceptor{
private LoginUserBean loginUserBean;
public CheckLoginInterceptor(LoginUserBean loginUserBean) {
this.loginUserBean = loginUserBean;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
if(loginUserBean.isUserLogin() == false) {
String contextPath = request.getContextPath();
response.sendRedirect(contextPath + "/user/not_login");
return false;
}
return true; //요청한 페이지 이동
}
}
@GetMapping("/not_login")
public String not_login() {
return "user/not_login";
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="root" value="${pageContext.request.contextPath }/"/>
<script>
alert("로그인 해주세요.")
location.href="${root}user/login"
</script>
@Override
public void addInterceptors(InterceptorRegistry registry) {
//생략...
//로그인해야 수정페이지 볼 수있도록 설정
CheckLoginInterceptor checkLoginInterceptor = new CheckLoginInterceptor(loginUserBean);
InterceptorRegistration reg2 = registry.addInterceptor(checkLoginInterceptor);
reg2.addPathPatterns("/user/modify");
}
인터셉터으로 로그인을 하지않으면 볼 수없도록 설정