SpringBoot中实现拦截器和过滤器
【SpringBoot中实现过滤器和拦截器】1.过滤器和拦截器简述
过滤器Filter和拦截器Interceptor,在功能方面很类似,但在详细实现方面差距还是比较大的。
2.过滤器的设置
2.1 自界说过滤器,实现Filter接口(SpringBoot 3.0 开始,jakarta.servlet.Filter,jdk需要升级到17)
2.2 重写doFilter,实现过滤逻辑,酌情放行
2.3 创建注解,实现过滤的范围设置
3.拦截器的设置
3.1 自界说拦截器,实现HandlerInterceptor接口
3.2 实现接口中的拦截方法(preHandle-请求实行前、postHandler-请求实行后、aftercompletion-视图渲染后)
3.3 创建设置类,实现WebMvcConfigurer接口,重写addInterceptors方法
4.总结
4.1 Filter依赖于Servlet容器,属于Servlet规范的一部门,而Interceptor依赖于SpringMVC框架;【所依赖框架】
4.2 Filter的生命周期由servlet容器管理,而Interceptor通过IOC容器来管理,可通过注入等方式来获取其Bean的实例;【管理的容器】
4.3 Filter可拦截所有web资源(包罗]sp,Servlet,静态资源),而Interceptor则只拦截controller。【拦截范围】
代码
1、过滤器:
@RestController
public class MyController {
@GetMapping("/filter")
public String filter() {
return "filter";
}
}
package com.example.demos.web.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;
/**
* @date 2025/1/5 18:06
* @description ip 请求的次数
*/
@Component
//所有请求都会经过这个过滤器
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
// 获取请求的ip
String ip = request.getRemoteAddr();
HttpSession session = request.getSession();
Integer count = (Integer) session.getAttribute("count");
count=Objects.isNull(count)?1:++count;
System.out.println("ip:"+ip+" count:"+count);
session.setAttribute("count", count);
//放行
filterChain.doFilter(servletRequest, servletResponse);
}
}
结果:
https://i-blog.csdnimg.cn/direct/61bc149beccf4210a0cdd47981c20fdd.png
https://i-blog.csdnimg.cn/direct/8cbed076d38442d3970415afcd144930.png
2、拦截器
@RestController
public class MyController {
@GetMapping("/interceptor")
public String filter() {
return "interceptor";
}
}
举行拦截处理惩罚
public class MyHandlerInterceptorimplements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 获取请求的ip
String ip = request.getRemoteAddr();
HttpSession session = request.getSession();
Integer count = (Integer) session.getAttribute("count");
count= Objects.isNull(count)?1:++count;
System.out.println("ip:"+ip+" count:"+count);
session.setAttribute("count", count);
//放行
return true;
}
}
设置文件需要将自界说拦截器举行注册
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
MyHandlerInterceptor interceptor = new MyHandlerInterceptor();
//拦截所有请求
registry.addInterceptor(interceptor).addPathPatterns("/**");
}
}
结果:
https://i-blog.csdnimg.cn/direct/8c1d7da81bef4579b698f82222f4cad2.png
https://i-blog.csdnimg.cn/direct/3a513d6380fa48e7a23f4598fd400910.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]