<mvc:interceptors> 가 동작하지 않는 이유가 궁금합니다

2,974 views
Skip to first unread message

flashover

unread,
May 27, 2012, 9:16:49 PM5/27/12
to Korea Spring User Group
그동안 바쁘다는 핑계로 눈팅만 하다, 막상 자기가 급해지니까 글을 올리고 있는 이기적인 개발자중 한명입니다 ㅠㅠ

제가 여기에 글을 쓴 이유는 프로젝트에서 특정URL 패턴에는 로그인 인터셉터를 해야 하는데 전혀 동작이 되지않아 여러 고수님들
의 가르침을 받고 싶어 올리게 되었습니다.
우선 환경은 spring3.05에 restful 형식으로 컨트롤러에 접근하고 있습니다.


===============applicationContext.xml====================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!-- ▼▼▼▼▼▼▼▼▼▼▼ Exception Resolver ▼▼▼▼▼▼▼▼▼ -->
<bean id="exceptionResolver"
class="com.real.common.resolver.CustomMappingExceptionResolver"
p:exceptionMappings-ref="exceptionMappings"
p:exceptionAttribute="exceptionMessage"
p:statusCodes-ref="statusCodes" />
<util:properties id="exceptionMappings">
<prop key="java.lang.Exception">common/error</prop>
</util:properties>
<util:properties id="statusCodes">
<prop key="common/error">500</prop>
</util:properties>

<!-- ▲▲▲▲▲▲▲▲▲ Exception Resolver ▲▲▲▲▲▲▲▲▲▲▲▲▲▲ -->

</beans>
======================================
========================web.xml===============
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/
ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>real</display-name>

<servlet>
<servlet-name>real-restful</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</
servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mercury-restful</servlet-name>
<url-pattern>/restful/*</url-pattern>
</servlet-mapping>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log/log4j.properties</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</
listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/real-restful-servlet.xml
/WEB-INF/applicationContext.xml
/WEB-INF/context-security.xml
</param-value>
</context-param>

<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</
filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- <filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</
filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
-->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>

</web-app>
==============================================

===========real-restful-servlet.xml=========================
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<mvc:annotation-driven/>

<context:component-scan base-package="com.real"/>

<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
p:alwaysUseFullPath="true">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></
bean>
</list>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp" />
<property name="order" value="1"/>
</bean>

<!-- Spring interceptors -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/restful/*" />
<bean class="com.real.common.interceptor.LoginCheckInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

</beans>

===========real-restful-servlet.xml=========================

===========LoginCheckInterceptor.java=========================
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginCheckInterceptor extends HandlerInterceptorAdapter
{
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {

System.out.println("===== preHandler
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
return true;
}

public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("===== postHandle
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

}

public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("===== afterCompletion
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

}

}
=========================================

설정은 이와같이 해서
http://localhost:9080/real/restful/userinfo/test

이런형태로 컨트롤러를 호출하면 동작은 잘되는데
<mvc:interceptors>가 동작하지 않습니다.
<!-- Spring interceptors -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/restful/*" />
<bean class="com.real.common.interceptor.LoginCheckInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
부분에서
<mvc:mapping path="/restful/*" /> 이렇게 설절하면 /restful/가 들어가는 모든 url은 설정

인터셉터를 동작시키는 걸로 알고 있는데 , 아무리 해봐도 동작을 안하네요.. ㅠㅠ

다른 웹사이트에 나와있는 소스를 보면 특별하게 별다른 설정하지 않고 사용하던데 ㅠㅠ

지금 걸리는 것은
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
p:alwaysUseFullPath="true">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></
bean>
</list>
</property>
</bean>
때문에
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/restful/*" />
<bean class="com.real.common.interceptor.LoginCheckInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

가 동작 하지 않는건가 하는 추측으로 바꿔봐도 인터셉터를 안타네요






CHOI YongEun

unread,
May 27, 2012, 9:43:33 PM5/27/12
to ks...@googlegroups.com
간단 한 것을 놓치셨네요..
*(아스트) 표현식을 한개막 적여 주셔서 그래요..

<mvc:mapping path="/restful/**" />

이렇게 ** 두개 적어주시면 될 겁니다.

즐프 하세요^^


2012. 5. 28., 오전 10:16, flashover 작성:

min seo

unread,
May 27, 2012, 11:06:55 PM5/27/12
to ks...@googlegroups.com
^^ 감사합니다.
** 부분도 잘 못되어있었네요. 감사합니다.
<mvc:mapping path="/restful/**" /> 로 수정하고 ㄱㄱ
ㅠㅠ 그래도 똑같이 안돼네요 

LoginCheckInterceptor 
에도
@Service
 추가하고 ㄱㄱ ㅠㅠ
 @@@ 드디어 패닉이 몰려옵니다 ㅠㅠ

2012년 5월 28일 오전 10:43, CHOI YongEun <choi...@gmail.com>님의 말:

--
Google 그룹스 'Korea Spring User Group' 그룹에 가입했으므로 본 메일이 전송되었습니다.
이 그룹에 게시하려면 ks...@googlegroups.com(으)로 이메일을 보내세요.
그룹에서 탈퇴하려면 ksug+uns...@googlegroups.com로 이메일을 보내주세요.
더 많은 옵션을 보려면 http://groups.google.com/group/ksug?hl=ko에서 그룹을 방문하세요.

min seo

unread,
May 28, 2012, 3:58:49 AM5/28/12
to ks...@googlegroups.com
테스트 해본결과 
<mvc:mapping path="/mercuryweb/restful/**" />
로는 인터셉트에는 안걸리고
<mvc:mapping path="/**" />
했을때는 인터셉터를 제대로 탑니다.

혹 web.xml의
<servlet-mapping>
<servlet-name>mercury-restful</servlet-name>
<url-pattern>/restful/*</url-pattern>
</servlet-mapping>
의 영향때문에 그런건지 아직 파악은 못해봤지만
일단 0%에서 10%로의 희망이 생기네요 ^^
앞으로는 로그인 으로 들어오는 url은 어떻게 인터셉터를 안타게 할까가 남았네요 ^^;;
여러분들은 어떤식으로 로그인url 요청이 올때 인터셉터를 안타게 하시나요 ???

2012년 5월 28일 오후 12:06, min seo <wrot...@gmail.com>님의 말:

코바(이수홍)

unread,
May 28, 2012, 6:17:07 AM5/28/12
to ks...@googlegroups.com
간단하게 생각하세요 
일단 모든 url은 인터셉터 타게 하시고 

그다음 내부에서 request.getRequestURI() 가져와서 

if 비교 해서 해당 로그인 url 이면 그냥 return 하시면 되겠네요 



2012년 5월 28일 월요일 오후 4시 58분 49초 UTC+9, flashover 님의 말:
그룹에서 탈퇴하려면 ksug+unsubscribe@googlegroups.com로 이메일을 보내주세요.

flashover

unread,
May 28, 2012, 7:59:20 AM5/28/12
to Korea Spring User Group
감사합니다. 간단하게 해결 가능하네요.
역시 보는 관점에 따라 어려울수도 쉽게 될수도 있네요
에휴 나는 왜 이렇게 생각 못하는지 ^^;;

On 5월28일, 오후7시17분, 코바(이수홍) <sbc...@gmail.com> wrote:
> 간단하게 생각하세요
> 일단 모든 url은 인터셉터 타게 하시고
>
> 그다음 내부에서 request.getRequestURI() 가져와서
>
> if 비교 해서 해당 로그인 url 이면 그냥 return 하시면 되겠네요
>
> 2012년 5월 28일 월요일 오후 4시 58분 49초 UTC+9, flashover 님의 말:
>
>
>
>
>
>
>
>
>
> > 테스트 해본결과
> > <mvc:mapping path="/mercuryweb/restful/**" />
> > 로는 인터셉트에는 안걸리고
> > <mvc:mapping path="/**" />
> > 했을때는 인터셉터를 제대로 탑니다.
>
> > 혹 web.xml의
> > <servlet-mapping>
> > <servlet-name>mercury-restful</servlet-name>
> > <url-pattern>/restful/*</url-pattern>
> > </servlet-mapping>
> > 의 영향때문에 그런건지 아직 파악은 못해봤지만
> > 일단 0%에서 10%로의 희망이 생기네요 ^^
> > 앞으로는 로그인 으로 들어오는 url은 어떻게 인터셉터를 안타게 할까가 남았네요 ^^;;
> > 여러분들은 어떤식으로 로그인url 요청이 올때 인터셉터를 안타게 하시나요 ???
>

> > 2012년 5월 28일 오후 12:06, min seo <wrots1...@gmail.com>님의 말:


>
> >> ^^ 감사합니다.
> >> ** 부분도 잘 못되어있었네요. 감사합니다.
> >> <mvc:mapping path="/restful/**" /> 로 수정하고 ㄱㄱ
> >> ㅠㅠ 그래도 똑같이 안돼네요
>
> >> LoginCheckInterceptor
> >> 에도
> >> @Service
> >> 추가하고 ㄱㄱ ㅠㅠ
> >> @@@ 드디어 패닉이 몰려옵니다 ㅠㅠ
>

> >> 2012년 5월 28일 오전 10:43, CHOI YongEun <choiy...@gmail.com>님의 말:


>
> >> 간단 한 것을 놓치셨네요..
> >>> *(아스트) 표현식을 한개막 적여 주셔서 그래요..
>
> >>> <mvc:mapping path="/restful/**" />
>
> >>> 이렇게 ** 두개 적어주시면 될 겁니다.
>
> >>> 즐프 하세요^^
>
> >>> 2012. 5. 28., 오전 10:16, flashover 작성:
>
> >>> <mvc:mapping path="/restful/*" />
>
> >>> --
> >>> Google 그룹스 'Korea Spring User Group' 그룹에 가입했으므로 본 메일이 전송되었습니다.
> >>> 이 그룹에 게시하려면 ks...@googlegroups.com(으)로 이메일을 보내세요.

> >>> 그룹에서 탈퇴하려면 ksug+uns...@googlegroups.com로 이메일을 보내주세요.

Toby Lee

unread,
May 28, 2012, 5:28:42 PM5/28/12
to ks...@googlegroups.com
SpringMVC 설정에서 path를 주실 때는 기본적으로 웹 애플리케이션 경로와 DispatcherServlet의 매핑 경로를 빼셔야 합니다.

웹 앱이 /mercuryweb으로 설정되어 있고, 그 안에서 다시 스프링 서블릿(DS)가 /restful로 되어있다면 SpringMVC 내의 경로 설정에는 자동으로 /mercuryweb/resful 이 앞에 붙는다고 보시면 됩니다. 인터셉터 설정을 주실 때도 이 부분을 빼고 뒤의 것만 주시면 될 것 같습니다. 

<servlet-mapping>
<servlet-name>mercury-restful</servlet-name>
<url-pattern>/restful/*</url-pattern>
</servlet-mapping>


라고 하셨으니 당연히 /mercuryweb/restful/* 조건을 만족하는 것만 스프링MVC 서블릿으로 요청이 전달될 것이고, 그래야지만 스프링 내부의 인터셉터에 연결되겠죠. 


2012/5/28 flashover <wrot...@gmail.com>
Reply all
Reply to author
Forward
0 new messages