与之前的过滤器不一样,过滤器是指在servlet中的,而拦截器是属于SpringMVC的,可以对请求的数据进行提前操作和后置操作:
- 先自己创建一个拦截器
Interception
类(其实人家真名叫Interceptor
,这是我随便起的类名),在里面对请求的参数,相应的参数等等进行各种操作- 有了拦截器了,你还需要一个工具类,来用将拦截器放进工具类中(工具类中可以放入各种东西,如
放行类
等)- 工具类准备妥当了,就需要SpringMVC可以在加载的时候知道它,于是,又在SpringMVC中加上注解,保证可以扫描到它
拦截器:重写接口HandlerInterceptor
中的三个方法,接口中的方法都被default
修饰了,所以需要主动手动重写方法,不写也不会报错,具体看注释
package com.itjh.contorller.interception;
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;
//保证能注册成Bean
@Component
public class Interception implements HandlerInterceptor {
//原始数据被拦截之前进行的操作
//如果返回值是false时,就会停止
// 接下来的所有操作,也就是说后面的流程会被打断
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
//原始数据被拦截之后进行的操作
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
//原始数据被拦截之后进行的操作
//且在postHandle之后进行操作
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterHandle");
}
}
工具类:在其中还放了放行类
package com.itjh.config;
import com.itjh.contorller.interception.Interception;
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 SpringMvcSupportConfig implements WebMvcConfigurer {
@Autowired
private Interception interception;
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//前面位置的包的请求访问被拦截时,会自动进入后面的包里面找数据
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
//添加拦截器,并且写出访问哪一个路径时要拦截
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interception).addPathPatterns("/books","/books/**");
}
}
Springmvc配置类:
package com.itjh.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
//扫描控制类和放行前端需求SpringMvcSupportConfig,还有包含了拦截器的工具类
@ComponentScan({"com.itjh.contorller","com.itjh.config"})
//转换器,这样就可以json的数据转换了
@EnableWebMvc
public class SpringMvcConfig {
}
将工具类去掉,直接在SpringMVC中写,以此来代替工具类,但是这样代码就和spring写一块,焊在一块了
SpringMVC配置类:工具类就可以直接删掉了
package com.itjh.config;
import com.itjh.contorller.interception.Interception;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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
//扫描控制类和放行前端需求SpringMvcSupportConfig
@ComponentScan({"com.itjh.contorller","com.itjh.config"})
//转换器,这样就可以json的数据转换了
@EnableWebMvc
//实现接口,这个接口和直接创建工具类实现的接口不一样
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
private Interception interception;
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//前面位置的包的请求访问被拦截时,会自动进入后面的包里面找数据
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interception).addPathPatterns("/books");
}
}
比如想知道得到的数据的类型等:如图中箭头所示
比如想知道时间:
//保证能注册成Bean
@Component
public class Interception implements HandlerInterceptor {
//原始数据被拦截之前进行的操作
//如果返回值是false时,就会停止
// 接下来的所有操作,也就是说后面的流程会被打断
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String date=request.getHeader("Date");
System.out.println("date: "+date);
System.out.println("preHandle");
return true;
}
再比如想知道当前访问的方法就:结果中有方法的描述:handler: com.itjh.contorller.serv#getAll()
//保证能注册成Bean
@Component
public class Interception implements HandlerInterceptor {
//原始数据被拦截之前进行的操作
//如果返回值是false时,就会停止
// 接下来的所有操作,也就是说后面的流程会被打断
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("handler: "+handler);
System.out.println("preHandle");
return true;
}
只要其中任意一个拦截器的preHandle()
方法返回值为false
,程序就不会执行任意一个postHandle()
方法
举个例子:
两个拦截器:
package com.itjh.contorller.interception;
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;
//保证能注册成Bean
@Component
public class Interception implements HandlerInterceptor {
//原始数据被拦截之前进行的操作
//如果返回值是false时,就会停止
// 接下来的所有操作,也就是说后面的流程会被打断
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// String date=request.getHeader("Date");
// System.out.println("date1111: "+date);
// System.out.println("handler11111: "+handler);
System.out.println("preHandle11111");
return true;
}
//原始数据被拦截之后进行的操作
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle11111");
}
//原始数据被拦截之后进行的操作
//且在postHandle之后进行操作
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterHandle11111");
}
}
package com.itjh.contorller.interception;
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;
//保证能注册成Bean
@Component
public class Interception2 implements HandlerInterceptor {
//原始数据被拦截之前进行的操作
//如果返回值是false时,就会停止
// 接下来的所有操作,也就是说后面的流程会被打断
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// String date=request.getHeader("Date");
// System.out.println("date22222: "+date);
// System.out.println("handler22222: "+handler);
System.out.println("preHandle22222");
return true;
}
//原始数据被拦截之后进行的操作
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle22222");
}
//原始数据被拦截之后进行的操作
//且在postHandle之后进行操作
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterHandle222222");
}
}
结果:
四月 18, 2023 4:55:52 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
preHandle11111
preHandle22222
[INFO] {dataSource-1} inited
postHandle22222
postHandle11111
afterHandle222222
afterHandle11111