AuthCheckInterceptor.java - 인터셉터 파일 해당 url의 첫번째 path로 제어
package com.ssd.admin.util;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class AuthCheckInterceptor extends HandlerInterceptorAdapter {
@Autowired
private WebApplicationContext context;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
boolean isAuth = false;
HttpSession session = request.getSession();
// 요청 URI로 1depth path를 구한다.
String reqUri = request.getRequestURI();
String[] reqUris = reqUri.split("/");
String firstPath = "";
firstPath = reqUris[1];
if(firstPath.equals("products") || firstPath.equals("resources") || firstPath.equals("aboutUs") || firstPath.equals("manage")){
if(session.getAttribute("userSeq") != null){
String grade = (String) session.getAttribute("userGrade");
if(firstPath.equals("manage")&&grade.equals("1")){
isAuth = false;
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<script>alert('권한이 없습니다.'); history.go(-1);</script>"); <== 관리자 등급별로 메뉴 제어
out.flush();
}
isAuth = true;
}else{
isAuth = false;
response.sendRedirect(context.getServletContext().getContextPath()+"/login"); <== 로그인 페이지로 이동
}
}
return isAuth;
}
}
스프링 서블릿 설정에 아래 내용 추가하기 해당 url 을 받을경우 서블릿 수행
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/products//**" />
<mvc:mapping path="/resources/**" />
<mvc:mapping path="/aboutUs/**" />
<mvc:mapping path="/manage/**" />
<bean class="com.ssd.admin.util.AuthCheckInterceptor" />
</mvc:interceptor>
</mvc:interceptors>