1 过滤器Filter和拦截器Interceptor,在功能方面很类似,但在具体实现方面差距还是比较大的。
2.过滤器的配置
2.1 自定义过滤器,实现Filter接口(SpringBoot 3开始,jakarta.servlet.Filter)
2.2 重写doFilter,实现过滤逻辑,酌情放行
package com.lenovo.springboot.filter;
import io.swagger.v3.oas.models.security.SecurityScheme;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
String ip = httpRequest.getRemoteAddr();
HttpSession session = httpRequest.getSession();
Integer count = (Integer) session.getAttribute("count");
count = Objects.isNull(count) ? 1: ++count;
System.out.println("ip"+ip+"\tcount"+count);
session.setAttribute("count",count);
filterChain.doFilter(servletRequest,servletResponse);
}
}
2.3 创建注解,实现过滤的范围配置
启动文件中加入
@ServletComponentScan(basePackages = "com.lenovo.springboot.filter")
3.拦截器的配置
3.1 白定义拦截器,实现HandlerInterceptor接口
3.2 实现接口中的拦截方法(preHandle-请求执行前、postHandler-请求执行后、afterCompletion-视图渲染后)
package com.lenovo.springboot.interceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import java.util.Objects;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
String ip = request.getRemoteAddr();
HttpSession session = request.getSession();
Integer count = (Integer) session.getAttribute("countip");
count = Objects.isNull(count) ? 1: ++count;
System.out.println("ip##"+ip+"\tcountip##"+count);
session.setAttribute("countip",count);
//放行
return true;
}
}
3.3 创建配置类,实现WebMvcconfigurer接口,重写addInterceptors方法
package com.lenovo.springboot.config;
import com.lenovo.springboot.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
MyInterceptor myInterceptor = new MyInterceptor();
registry.addInterceptor(myInterceptor).addPathPatterns("/**");
}
}
4.总结
4.1 Filter依赖于servlet容器,属于Servlet规范的一部分,而Interceptor依赖于SpringMVC框架:
4.2 Filter的生命周期由servlet容器管理,而Interceptor通过IOC容器来管理,可通过注入等方式来获取其Bean
4.3 Filter可拦截所有web资源(包括Jsp,Servlet,静态资源),而Interceptor则只拦截controller。
文章评论