// 注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(userTokenInterceptor())
.addPathPatterns("/hello");
WebMvcConfigurer.super.addInterceptors(registry);
}
// 拦截器对象
@Bean
public UserTokenInterceptor userTokenInterceptor() {
return new UserTokenInterceptor();
}
##拦截器
public class UserTokenInterceptor implements HandlerInterceptor {
// 访问controller之前
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
return true;
}
// 访问controller之后,渲染视图之前
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView)throws Exception {
}
// 访问controller之后,渲染视图之后
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}