我目前正在使用一个配置了 xml 的拦截器,如下所示:
<mvc:interceptors>
<bean class="org.resthub.dashboard.BasicInterceptor" />
</mvc:interceptors>
但我想把它放在我的配置类中:
@Configuration
@ComponentScan("org.resthub.dashboard")
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class WebAppConfig extends WebMvcConfigurerAdapter{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new BasicInterceptor());
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
但它不起作用,我不知道为什么。拦截器永远不会被调用。
有任何想法吗 ?
谢谢
edit
: 这里是 BasicInterceptor
package org.resthub.dashboard;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class BasicInterceptor extends HandlerInterceptorAdapter {
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
super.postHandle(request, response, handler, modelAndView);
System.out.println("INTERCEPTORORRR§§§§§§");
if (modelAndView != null && modelAndView.getModelMap() != null) {
...
}
}
}