WebConfig.java:
package cn.ted.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import cn.ted.intercept.LoginInterceptor; @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { // 需要拦截的路径 String[] addPathPatterns = { "/boot/**" }; // 不拦截的路径 String[] excludePathPatterns = { "/boot/students" }; // 注册登录拦截器 registry.addInterceptor(new LoginInterceptor()).addPathPatterns(addPathPatterns) .excludePathPatterns(excludePathPatterns); // 注册权限拦截器 /*registry.addInterceptor(new AuthInterceptor()).addPathPatterns(addPathPatterns) .excludePathPatterns(excludePathPatterns);*/ } }
LoginInterceptor.java:
package cn.ted.intercept; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class LoginInterceptor implements HandlerInterceptor{ public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { System.out.println("进入拦截器"); return false; } }