bind Spring HandlerInterceptor only to one controller bind Spring HandlerInterceptor only to one controller spring spring

bind Spring HandlerInterceptor only to one controller


When you inject interceptors into a HandlerMapping bean, those interceptors apply to every handler mapped by that HandlerMapping. That was fine in the pre-annotation days, since you'd just have configure multiple HandlerMapping beans. However, with annotations, we tend to have a single DefaultAnnotationHandlerMapping that maps everything, so this model doesn't work.

The solution is to use <mvc:interceptors>, where you explicitly map paths to interceptor beans. See the docs, and this example:

<mvc:interceptors>    <mvc:interceptor>        <mvc:mapping path="/secure/*"/>        <bean class="org.example.SecurityInterceptor" />    </mvc:interceptor></mvc:interceptors>