当前位置:网站首页>Interceptor interceptor
Interceptor interceptor
2022-07-27 18:55:00 【Get lazy】
The interceptor is SpringMVC An object in , Can interceptor pair Controller Request .
There are systematic interceptors in the interceptor framework , You can also customize interceptors , Implement pre-processing of requests .
Implement custom interceptors
1.SpringMVC Middle interceptor
1. Create a class implementation SpringMVC Framework of the HandlerInterceptor Interface
```java
public interface HandlerInterceptor {
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
```
2. Need to be in SpringMVC In the configuration file , Declaration interceptor
```xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:path="url" />
<bean class=" Interceptor class fully qualified name "/>
</mvc:interceptor>
</mvc:interceptors>
```2.SpringBoot Custom interceptors
First create Interceptor Class implementation HandlerIntercetor Interface
HandlerInterceptor Interface
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}What we achieve is preHandle.
/**
* @param request
* @param response
* @param handler Objects controlled by interceptors
* @return boolean
* true : The request can be Controller Handle
* false: The request was truncated
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("lanjieqi");
return true;
}Here is the custom interceptor , We will put the custom interceptor into the container
establish config Inherit WebMvcConfigurer Interface
Be careful : You need to add notes to the class @Configuration If it is not added, the configuration will not be automatically loaded into the container .
WebMvcConfigurer There are many things implemented by interfaces , What we need to realize here is addInterceptors Method .
@Override
public void addInterceptors(InterceptorRegistry registry) {
WebMvcConfigurer.super.addInterceptors(registry);
}We need to reference the custom interceptor just written .
LoginInterctor loginInterctor=new LoginInterctor();What is written on it can be used normally , however Java It's object-oriented programming , So we should face interface programming CSDN
So you should write
HandlerInterceptor loginInterctor = new LoginInterctor();Write what should be blocked , Release what
addPathPatterns Is intercepted
excludePathPatterns Is released
registry.addInterceptor(loginInterctor)
.excludePathPatterns( path)
.addPathPatterns(path1);Use the array to write the path that needs to be intercepted and released , Comments can be made later @ConditionalOnProperty To optimize
String path []={"/test"};
String path1 []={"/api"};Finally, we write the configuration class Controller
@GetMapping("/test")
@ResponseBody
public String test() {
return "test";
}
@GetMapping("/api")
@ResponseBody
public String api() {
return "api";
}边栏推荐
- Filebeat.yml configuration file about the configuration of multiple services
- 低噪负离子风扇触摸IC
- 兆骑科创海内外引进高层次人才,创新创业项目对接
- 文件的上传和下载
- Have you ever stumbled on MySQL's order by
- C static method and non static method
- 飞机大战英雄出场加子弹实现
- Functions in JS and the use of DOM to obtain elements and event attributes
- PyGame aircraft war game background implementation
- MySQL create event execution task
猜你喜欢

Build a simple knowledge question and answer system

I'm stupid. When completable future is used with openfegin, it even reports an error

Must the MySQL query column be consistent with the group by field?

The song of the virtual idol was originally generated in this way!

图文结合,完美解释MySQL逻辑备份的实现流程

Join query and subquery

Nodejs 模板引擎ejs

JDBC MySQL 01 JDBC operation MySQL (add, delete, modify and query)

家用静音驱蚊灯芯片-DLTAP703SD-杰力科创

"MySQL things" explains the indexing principle in detail
随机推荐
TypeError: conv2d(): argument ‘padding‘ (position 5) must be tuple of ints, not str【报错】
Matplotlib (basic usage)
I was forced to optimize the API gateway query interface
低噪负离子风扇触摸IC
How to send external mail to the company mailbox server on the Intranet
[yuntu said] 249 mobile application security service - app's physical examination center, comprehensive testing, safe on the road!
网红RGB镜子灯触摸芯片-DLT8S15B-杰力科创
LeetCode 刷题 第三天
EN 1155 building hardware swing door opener - CE certification
Basic operations of MySQL view
Jianmu continuous integration platform v2.5.2 release
JDBC-MySql 02 数据访问和DAO模式
MySQL 01 关系型数据库设计
百度地图技术概述,及基本API与WebApi的应用开发
Must the MySQL query column be consistent with the group by field?
Multifunctional wireless remote control moxibustion instrument chip dltap703sd
Aircraft battle with enemy aircraft
Intelligent insomnia therapeutic instrument product dlt8p68sa Jericho
Mode= "widthfix" attribute in image tag
Here are all the MySQL interview questions you can't expect (the latest version of 2022)