본문 바로가기

OpenSource/Spring

어노테이션을 이용한 스프링MVC에서 인터셉터 설정


1. 빈설정

<!--

@RequestMapping어노테이션을 이용하여 요청을 처리할 컨트롤러사용

-->

<bean id="annotationHandlerMapping"

      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

       <property name="order" value="1" />

<!--   <property name="alwaysUseFullPath" value="true" />-->

       <property name="interceptors">

        <list>

            <ref bean="logHandlerInterceptor" />

            <ref bean="noCacheHeaderInterceptor" />

            <ref bean="loginCheckInterceptor" />

        </list>

       </property>

</bean>


2. 인터셉터 bean : LoginCheckInterceptor.java
@Service 어노테이션을 반드시 해줘야함.
@Service

public class LoginCheckInterceptor extends HandlerInterceptorAdapter {

 

       /** The Constant logger. */

       private Logger logger = Logger.getLogger(this.getClass());

 

 

       /**

        * <pre>

        * preHandle

        *

        * </pre>

        * @param request

        * @param response

        * @param handler

        * @return

        * @throws Exception

        */

       @Override

       public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

         if (... ) {

            return false;

         }

         return true;

       } 

}