SpringBoot实现过滤器和 * 的方法

作者:twilight0402 时间:2022-10-21 23:29:34 

过滤器

实现过滤器需要实现 javax.servlet.Filter 接口。重写三个方法。其中 init() 方法在服务启动时执行,destroy() 在服务停止之前执行。

可用两种方式注册过滤器:

  • 使用 FilterRegistrationBean 来注入。可使用 setOrder(0) 设置过滤器的优先级,越小优先级越高。

  • 使用 @WebFilter(filterName = "myFilter2" ,urlPatterns = "/*") 配合 @ServletComponentScan() 实现注入。(@Order 注解无效)

编写过滤器

package com.example.recorddemo.filters;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class MyFilter1 implements Filter {
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {
       System.out.println("初始化过滤器:" + filterConfig.getFilterName());
   }

@Override
   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
       System.out.println("在请求之前做处理");

if (servletRequest instanceof HttpServletRequest) {
           System.out.println("  URL:" + ((HttpServletRequest)servletRequest).getRequestURL());
       }

// 调用filter链中的下一个filter
       filterChain.doFilter(servletRequest, servletResponse);

System.out.println("在请求之后做处理");
   }

@Override
   public void destroy() {
       System.out.println("销毁:MyFilter1");
   }
}

注册过滤器

基于 FilterRegistrationBean

在配置类中注册一个 FilterRegistrationBean 类型的Bean。

  • 如果没有设置 UrlPatterns , 那么会自动关联到 /* 上。

  • 如果没有设置过滤器的名字,那么会自动推理出一个过滤器名称(bean的名字)

    When no URL pattern or servlets are specified the filter will be associated to '/*'. The filter name will be deduced if not specified.

  • fileter默认是enable的,将其设置为false表示关闭当前过滤器。

  • 可通过 setOrder(0) 方法设置过滤器的优先级,如果优先级相同,则先定义的优先级更高。

@Configuration
public class FilterConfiguration {
   @Bean
   public FilterRegistrationBean myFilter1(){
       MyFilter1 filter = new MyFilter1();
       FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter);
//        filterRegistrationBean.addUrlPatterns("/*");
//        filterRegistrationBean.setEnabled(true);
       return filterRegistrationBean;
   }
}

基于 @WebFilter

  • 使用 @WebFilter 修饰filter。

  • 在任意configuration类中添加 @ServletComponentScan("com.example.recorddemo.filters"),包名可以不填。

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "myFilter2" ,urlPatterns = "/*")
public class MyFilter2 implements Filter {
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {}

@Override
   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
       // 调用filter链中的下一个filter
       filterChain.doFilter(servletRequest, servletResponse);

}

@Override
   public void destroy() {}
}

*

* 会在处理指定请求之前和之后进行相关操作,配置 * 需要两步

  • 编写 * 类(实现 HandlerInterceptor 接口)

  • 添加已实现的 * (实现 WebMvcConfigurer 接口,并重写 addInterceptors() 方法)

  • 添加addPathPatterns()规定拦截哪些请求。(/*表示只拦截/下的所有目录,但是不包括子目录, /**表示拦截/下的所有目录,及其子目录)

* 类:

package com.example.recorddemo.interceptor;

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;

/**
* @author wangchao
*/
@Component
public class MyInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       // handle可拿到执行方法的反射对象。
       System.out.println("preHandle: MyInterceptor");
       return true;
   }

@Override
   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
       // 对于RESTful 接口用处不大
   }

@Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
       // 可捕捉异常,但是springboot已经有了全局异常捕捉
   }
}

配置 * :

package com.example.recorddemo.configuration;

import com.example.recorddemo.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
* @author wangchao
*/
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
   @Resource
   MyInterceptor myInterceptor;

/**
    * 添加 *
    * @param registry
    */
   @Override
   public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(myInterceptor).addPathPatterns("/**");
   }
}

registry.addInterceptor() 方法会返回当前的 interceptor, 因此可直接执行 addPathPatterns() 方法

public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {
       InterceptorRegistration registration = new InterceptorRegistration(interceptor);
       this.registrations.add(registration);
       return registration;
   }

* 的执行顺序类似于栈,按照如下顺序执行:

preHandle-1, preHandle-2, postHandle-2, postHandle-1, afterCompletion-2, afterCompletion-1

来源:https://www.cnblogs.com/twilight0402/p/16849129.html

标签:SpringBoot,过滤器, ,
0
投稿

猜你喜欢

  • Springboot整合MybatisPlus的实现过程解析

    2021-06-14 02:47:06
  • 详解SpringBoot中如何使用布隆过滤器

    2023-11-10 17:43:06
  • Mybatis Plus使用XML编写动态sql的超简易方法

    2022-02-20 01:49:16
  • 解读在C#中winform程序响应键盘事件的详解

    2023-10-08 09:17:00
  • android实现简单音乐播放器

    2021-06-28 22:07:26
  • Javaweb 定时器功能代码实例

    2022-04-07 00:47:20
  • JDK源码之Vector与HashSet解析

    2021-09-06 10:47:23
  • java面试题之try中含return语句时代码的执行顺序详解

    2023-11-24 07:34:16
  • 详解使用Spring Security OAuth 实现OAuth 2.0 授权

    2023-10-01 23:05:10
  • SpringAop @Aspect织入不生效,不执行前置增强织入@Before方式

    2021-09-18 04:00:58
  • java UDP实现一个聊天工具的示例代码

    2021-09-19 18:41:47
  • Spring基于注解的缓存声明深入探究

    2023-01-20 13:26:06
  • SpringBoot实战之高效使用枚举参数(原理篇)案例详解

    2022-02-10 23:54:23
  • Spring容器注册组件实现过程解析

    2023-07-10 11:08:07
  • 探讨Java中的深浅拷贝问题

    2023-01-07 01:12:44
  • 解决在for循环中remove list报错越界的问题

    2022-01-12 15:27:56
  • ASP.Net动态读取Excel文件最简方法

    2022-12-24 16:38:57
  • Spring Boot项目如何同时支持HTTP和HTTPS协议的实现

    2023-11-19 19:57:05
  • Java实现聊天室界面

    2023-12-15 10:43:32
  • jvm crash的崩溃日志详细分析及注意点

    2022-12-18 07:49:00
  • asp之家 软件编程 m.aspxhome.com