一 概念
拦截器:拦截web请求的一种技术,spring的组件
二 原理分析
1.DispatcherServlet是整个springmvc的入口,所有的请求都是从这里开始
2.如果设置了拦截器,则请求会转发给拦截器,并且执行拦截器的preHandle方法,如果该方法返回true,则请求会转发给controller,如果为false,则该请求终止
3.拦截器的preHandle方法返回为true的情况下,请求会转发给controller
4.controller请求处理完之后,会再次把请求转发给拦截器的postHandle方法
三 使用
1)springmvc配置类
继承WebMvcConfigurer 的配置类的重写方法:
//该方法用来配置静态资源
public void addResourceHandlers(ResourceHandlerRegistry registry)
// 该方法用来注册拦截器,自己写好的拦截器需要通过这里添加注册才能生效
public void addInterceptors(InterceptorRegistry registry)
2)自定义拦截器
该拦截器实现 HandlerInterceptor 接口,方法含义:
//访问控制器之前,执行此方法,若返回true,则继续传递请求,若返回false,则请求终止
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
//请求处理完之后,请求返回时,会执行此方法
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception
//在请求完全处理之后,会执行此方法,常进行资源销毁
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception
3)注册自定义拦截器
在 WebConfigurer 中的 addInterceptors 中添加拦截器,使其生效
//addPathPatterns 用来设置拦截路径,excludePathPatterns 用来设置白名单,也就是不需要触发这个拦截器的路径
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("排除的路径string数组");
四 应用场景
代码执行环境
- springboot:2.1.0.RELEASE
- jdk:1.8
- idea:2019
1)登录验证
拦截器类
package com.hwl.hwlspringboot.config.intercepors;
import com.hwl.hwlspringboot.entity.User;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Component
public class LoginInterceptor implements HandlerInterceptor {
//这个方法是在访问接口之前执行的,我们只需要在这里写验证登陆状态的业务逻辑,就可以在用户调用指定接口之前验证登陆状态了
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//每一个项目对于登陆的实现逻辑都有所区别,我这里使用最简单的Session提取User来验证登陆。
HttpSession session = request.getSession();
//这里的User是登陆时放入session的
User user = (User) session.getAttribute("loginUser");
//如果session中没有user,表示没登陆
if (user == null) {
//这个方法返回false表示忽略当前请求,如果一个用户调用了需要登陆才能使用的接口,如果他没有登陆这里会直接忽略掉
//当然你可以利用response给用户返回一些提示信息,告诉他没登陆
//return false;
String path = session.getServletContext().getContextPath();
//跳转到登录页面
response.sendRedirect(path + "/index2");
return true;
} else {
return true; //如果session里有user,表示该用户已经登陆,放行,用户即可继续调用自己需要的接口
}
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
拦截器注册类
package com.hwl.hwlspringboot.config;
import com.hwl.hwlspringboot.config.intercepors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Autowired
LoginInterceptor loginInterceptor;
// 这个方法是用来配置静态资源的,比如html,js,css,等等
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
// 这个方法用来注册拦截器,我们自己写好的拦截器需要通过这里添加注册才能生效
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns("/**") 表示拦截所有的请求,
// excludePathPatterns("/login", "/register") 表示除了登陆与注册之外,因为登陆注册不需要登陆也可以访问
//addPathPatterns 用来设置拦截路径,excludePathPatterns 用来设置白名单,也就是不需要触发这个拦截器的路径。
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/index/index", "/");
}
}