728x90
21. com/mvc/upgrade/common/interceptor/LoginInterceptor (impl HandlerInterceptor)
org.springframework.web.servlet패키지에 있는HandlerInterceptor를 상속받겠습니다!
package com.mvc.upgrade.common.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoginInterceptor implements HandlerInterceptor {
private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
logger.info("[Interceptor] : preHandle");
// spring 3.2 이상부터는 servlet-context.xml에서 <exclude-mapping-path> 를 사용하여 설정 가능
if(request.getRequestURI().contains("/loginform.do")) {
return true;
} else if(request.getRequestURI().contains("/ajaxlogin.do")) {
return true;
} else if(request.getRequestURI().contains("/test.do")) {
return true;
} else if(request.getRequestURI().contains("/registform.do")) {
return true;
} else if(request.getRequestURI().contains("/registres.do")) {
return true;
}
if(request.getSession().getAttribute("login") != null) {
return true;
} else if(request.getSession().getAttribute("login") == null ) {
response.sendRedirect("loginform.do");
return false;
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
if(modelAndView != null) {
logger.info("[Interceptor] : " + modelAndView.getViewName());
}
logger.info("[Interceptor] : postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
logger.info("[Interceptor] : afterCompletion");
}
}
22. WEB-INF/spring/appServlet/servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.mvc.upgrade" />
<!-- interceptor -->
<interceptors>
<interceptor>
<mapping path="/*"/>
<beans:bean class="com.mvc.upgrade.common.interceptor.LoginInterceptor" />
</interceptor>
</interceptors>
</beans:beans>
인터셉터는 DispacherServlet에서 Controller로 가기전 동작합니다.
interceptor는 handlerMapping 신호를 강탈해서 가지고 옵니다.
그런 다음 session에 저장되어 있는 값이 없다면 다른데 가지말고 로그인 화면으로 가라! 하는 명령이 있습니다.
혹은 회원등급이 admin이면 admin페이지로 가라! 등급이 user면 user페이지로 가라! 하는 명령도 수행할수 있겠죠.
빨간색 영역에서 원하는 대로 명령을 할수가 있습니다.
preHandle메서드가 true가 리턴이 되면 controller로 보내고 false면 controller로 보내지 말아라 라는 명령이 됩니다.
따라서 응답하는 신호가 없으면 아무것도 없는 비어있는 화면이 응답되겠죠.
728x90
'Java 관련 > Spring Legecy' 카테고리의 다른 글
[Spring] security(비밀번호 암호화) (0) | 2022.04.08 |
---|---|
[Spring] transation(트랜잭션 - 원자성) (0) | 2022.04.07 |
[Spring] Login / Regist(로그인 / 회원가입) (0) | 2022.04.05 |
[Spring] 스프링MVC - AOP (0) | 2022.04.04 |
[Spring] filter(javax.servlet.Filter) (0) | 2022.04.03 |