一、配置拦截器
1、创建一个类继承HandlerInterceptorAdapter 拦截器适配器
MyHandlerInterceptor .java
@Component
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle................:"+request.getRequestURI());
return super.preHandle(request, response, handler);
}
}
二、注册拦截器
配置类 实现WebMvcConfigurer
@SpringBootApplication
@MapperScan("cn.itsource.web.controller.mapper")
@ImportResource("classpath:applicationContext-service.xml")
public class ApplicationConfig implements WebMvcConfigurer {
@Autowired
private MyHandlerInterceptor myHandlerInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myHandlerInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/xx").setViewName("hello2.html");
}
public static void main(String[] args) {
SpringApplication.run(ApplicationConfig.class);
}
}