springboot自定义拦截器
时间: 2023-10-12 09:18:37 浏览: 95
Spring Boot提供了很多的自定义拦截器方式,下面介绍几种常见的方式。
1. 实现HandlerInterceptor接口
实现HandlerInterceptor接口是自定义拦截器的最常见方式。代码示例如下:
```
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 在请求处理之前进行调用(Controller方法调用之前)
return true; // 只有返回true才会继续向下执行,返回false取消当前请求
}
@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 {
// 在整个请求结束之后被调用,也就是在DispatchServlet渲染了对应的视图之后执行(主要是用于进行资源清理工作)
}
}
```
然后在配置类中注册拦截器:
```
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); // 拦截所有请求
}
}
```
2. 使用注解方式
使用注解方式定义拦截器,需要自定义一个注解,然后在拦截器中使用该注解进行标记。示例代码如下:
定义注解:
```
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
```
定义拦截器:
```
public class MyInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Method method = ((HandlerMethod) handler).getMethod();
if (method.isAnnotationPresent(MyAnnotation.class)) { // 判断是否有MyAnnotation注解
return true;
} else {
response.getWriter().write("no annotation");
return false;
}
}
}
```
在Controller方法上使用注解:
```
@RestController
public class MyController {
@MyAnnotation
@GetMapping("/test")
public String test() {
return "hello";
}
}
```
然后在配置类中注册拦截器:
```
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
}
}
```
3. 使用过滤器方式
使用过滤器方式定义拦截器,需要自定义一个Filter类。示例代码如下:
定义过滤器:
```
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// 初始化方法
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String requestURI = request.getRequestURI();
if (requestURI.contains("test")) { // 判断请求路径是否包含test
filterChain.doFilter(servletRequest, servletResponse);
} else {
response.getWriter().write("no test");
}
}
@Override
public void destroy() {
// 销毁方法
}
}
```
然后在配置类中注册过滤器:
```
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean<MyFilter> filterRegistrationBean() {
FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new MyFilter());
registrationBean.addUrlPatterns("/*"); // 拦截所有请求
registrationBean.setName("myFilter");
registrationBean.setOrder(1);
return registrationBean;
}
}
```
阅读全文