当前位置:网站首页>Interceptors and filters
Interceptors and filters
2022-07-24 18:59:00 【Why not sell egg cakes well】
Interceptors and filters
contrast
1 Filters can only be called once during container initialization , and action In cycle , Interceptors can be called multiple times .
2 Filters work on all requests , Interceptors can only be used to action Work .
3 Filter cannot access context , Stack objects .
4 Filters depend on servlet Containers
5 Filters are based on function callbacks , Interceptors are based on java Reflection .
6 The filter cannot get ioc Container of bean, Interceptors can , Intercept injection service, You can call the business logic .
Execution order
filter–interceptor.preHandle–handler–interceptor.postHandle–interceptor.afterCompletion–filter
Interceptor
@Component
public class SampleInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("\n Has entered the interceptor ");
return true;
//return HandlerInterceptor.super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println(" The controller is finished ");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println(" Get the results "+response);
System.out.println(" End of request \n");
}
}
Configuration class
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
SampleInterceptor sampleInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sampleInterceptor).addPathPatterns("*/**").excludePathPatterns("/**/LoginController/**","/**/error")
.excludePathPatterns("/error");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
Interceptor scenario
Section oriented programming , Functions that conform to crosscutting concerns can be used in interceptors
Login authentication
Authority verification
logging Log request operations , Count the requested visits .
Handle cookie
Performance monitoring , Monitoring processing time .
Execution sequence considerations
First define the interceptor and then execute .
You can also use order Method to set the execution order , The smaller the value, the earlier to execute .
filter
servlet A component of , Before users access a resource , Filter access requests and responses .
principle
Based on function callbacks , A filter instance can only be called once during container initialization .
Different from interceptors
Can't get ioc Container of bean
Filter>>interceptor>>controllerAdvice>>aspect>>controller
Implement filter methods
Annotation mode
@Order(2)
@WebFilter(filterName = "sampleFilterAnnotation",urlPatterns = {
"/study/interfaces/v1/user/*"})
public class SampleFilterAnnotation implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println(" initialization filter");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println(" What to do before entering the filter ");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println(" Process the returned response");
}
@Override
public void destroy() {
System.out.println(" The filter is destroyed ");
}
}
The parameters above , To baidu .
Last
After configuring the above , You need to add a scan package annotation to the startup class , Turn on package scanning .
The use of tools is fast , In what scene , How do you use it? , This is what we need to think about .
边栏推荐
- Valentine's Day gift ----- use her photos and our chat records to generate word clouds~
- Thread lifecycle and basic methods
- Convolutional Neural Networks in TensorFlow quizs on Coursera
- MySQL hidden version number
- Add column by column selection for JTable
- Tcl/tk grouping and replacement rules
- Math
- [Tkinter] common components (II)
- Integer
- Network security port 80 - PHP CGI parameter injection Execution Vulnerability
猜你喜欢

OpenGL learning (III) glut two-dimensional image rendering

Crazy God redis notes 11

网络安全80端口—-PHP CGI参数注入执行漏洞

L4l7 load balancing

全国职业院校技能大赛网络安全竞赛——Apache安全配置详解

Nacos简介和控制台服务安装

Data analysis of network security competition of national vocational college skills competition digital forensics-a

多线程与并发编程常见问题(未完待续)

Leetcode memory deep search / dynamic planning V2

Sqoop
随机推荐
Ionic4 learning notes 11 - popular goods display of an East Project
matplotlib
[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast
2022 Hangdian multi school second session 1009 shuangq (Mathematics)
Wireshark simple filter rule
Rookie colleagues cost me 2K. Did you recite the secret of salary increase? (collect it quickly!)
Get module handle / base address
About core files
Tcl/tk grouping and replacement rules
【微信小程序开发】自定义tabBar案例(定制消息99+小红心)
理解corners_align,两种看待像素的视角
Understand dynamic calculation diagram, requires_ grad、zero_ grad
使用der格式公钥生成publicKey报错
[question 39] special question for Niuke in-depth learning
Wechat applet reverse
Type-C边充边听PD协议芯片
epoch,batch_ size
OpenGL learning (V) modern OpenGL triangle rendering
Reading notes of XXL job source code
[Tkinter] layout management and event system