- 定义拦截器类
public class MyInterceptor extends HandlerInterceptorAdapter { public MyInterceptor(){ System.out.println("MyInterceptor contructor..........."); } //在执行目标方法之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor preHandle"); return true; } //在执行目标方法之后,在目标方法return 返回响应视图之前 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor postHandle"); } //在目标方法return 返回响应视图之后 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor afterCompletion"); } }
- config/ConfigRegistCenter
/** * 配置中心 * 拦截器、视图解析器的配置等 web */ @EnableWebMvc //启用Spring MVC支持 @Configuration //允许在上下文中注册额外的bean或导入其他配置类 public class ConfigRegistCenter implements WebMvcConfigurer { //注册拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); .excludePathPatterns("/login") .excludePathPatterns("/regist"); } } http://localhost:8080/demo/login http://localhost:8080/demo/regist
登录检查拦截器
要求:我的xx(收藏,订单,个人中心,简历....)需要用户登录过后才能观察看的 controller: /myorder/18392839 /resume/ /job/ /member/getcode /member/login /member/regist 前端: /myorder/info.jsp /myorder/all.jsp /member/login.jsp /member/regist.jsp /resume.jsp 静态资源: /static/css /js /images /dist /layui registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/member/getcode", "/member/login", "/member/regist", "/job/**","/company" "/css/**", "/js/**","/images/**","/layui/**","/dist/**");
public class LoginInterceptor implements HandlerInterceptor { //检查登录业务拦截器,执行(controller)login方法之前就需要拦截检验的,所以需要把拦截的业务写在preHandle方法中 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); Member member = (Member)session.getAttribute("loginUser"); if(member ==null){ System.out.println("用户尚未登录,请先登录"); // request.getRequestDispatcher("/member/login.jsp").forward(request, response); response.sendRedirect(request.getContextPath()+"/member/login.jsp"); return false; } else{ System.out.println("用户已经登录,放行"); return true; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
将自定义的拦截器注册到 中心去
/** * WebConfigurer 需要实现WebMvcConfigurer接口,并实现里面的连个方法 * (老版本的spring-boot中使用的是WebMvcConfigurerAdapter,新版本中已经过时, * 还有不能通过继承WebMvcConfigurationSupport这个类实现,这样会在某些情况下失效) * */ @Configuration //允许在上下文中注册额外的bean或导入其他配置类 public class WebConfigurer implements WebMvcConfigurer { //这个方法用于注册拦截器,我们自己定义的拦截器需要通过在这里添加注册后才能生效 @Override public void addInterceptors(InterceptorRegistry registry) { //addPathPatterns 用来设置拦截路径 //excludePathPatterns 用来设置白名单,也就是不需要触发这个拦截器的路径。 //addPathPatterns("/**") 表示拦截所有的请求 //.excludePathPatterns("/login","/regist")表示出了登录与注册之外,都会拦截(因为登录注册不需要登录页可以访问) registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/member/getcode", "/member/login", "/member/regist","/css/**", "/js/**","/images/**","/layui/**","/dist/**"); } //这个方法是用来配置静态资源的,比如html,js,css等 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } }