当前位置:网站首页>使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
2022-07-27 03:38:00 【时间是一种解药】
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
问题背景
项目中经常用的拦截器进行请求拦截进行日志统一打印输入输出,介绍一下WebMvcConfigurer拦截器的使用
注意事项:
- 可以使用文章的代码自己创建工程,也可下载源码进行参考
WebMvcConfigurer拦截器做增强处理
1 添加拦截器的配置类
package com.lanran.webmvcconfigurer.config;
import com.lanran.webmvcconfigurer.interceptor.WebInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** * @Description: * @Created: IDEA2021 * @author: 蓝染 * @createTime: 2022-07-30 17:57 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new WebInterceptor()) //注册拦截器,
.addPathPatterns("/**"); //所有请求都需要拦截
}
}
2 添加拦截器
package com.lanran.webmvcconfigurer.interceptor;
import com.lanran.webmvcconfigurer.entity.UserInfoTo;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** * @Description: 在执行目标方法之前,判断用户的登录状态.是临时用户,还是登录用户,并封装传递给controller目标请求 * @Created: IDEA2021 * @author: 蓝染 * @createTime: 2022-07-30 17:31 **/
//拦截器需要添加配置才能生效,添加配置类GulimallWebConfig implements WebMvcConfigurer
//HandlerInterceptor这个是MVC的拦截器,所以所有的getmapping和postmapping都会被拦截
public class WebInterceptor implements HandlerInterceptor {
/*** * 目标方法执行之前,这里的目标方法 * @param request * @param response * @param handler * @return * @throws Exception */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("before");
return true;
}
/** * 业务执行之后,分配临时用户来浏览器保存 * * @param request * @param response * @param handler * @param modelAndView * @throws Exception */
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("after");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
3 添加测试接口
package com.lanran.webmvcconfigurer.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/** * @Author suolong * @Date 2022/7/22 9:52 * @Version 2.0 */
@RestController
public class WebController {
@GetMapping("/test")
public String test(){
System.out.println("test method");
return "success";
}
}
4 使用的实体类
package com.lanran.webmvcconfigurer.entity;
import lombok.Data;
/** * @Author suolong * @Date 2022/7/22 8:44 * @Version 2.0 */
@Data
public class UserInfoTo {
private String name;
private String gender;
private Integer age;
private Boolean flag;
}
5 启动类
package com.lanran.webmvcconfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebMvcConfigurerApplication {
public static void main(String[] args) {
SpringApplication.run(WebMvcConfigurerApplication.class, args);
}
}
6 项目目录
7 启动项项目,使用postman测试接口
日志打印结果
总结
把想拦的拦下来吧
作为程序员第 214 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …


Lyric: 在有眼泪的雨里
边栏推荐
- 2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units
- List Simulation Implementation
- Elastic certification test: 30 day FastPass Study Guide
- leetcode每日一题:数组的相对排序
- [OBS] dynamic bit rate: bit rate estimation
- Internet of things smart home project - Smart bedroom
- Practice of microservice in solving Library Download business problems
- 面试题 02.05. 链表求和
- Sum of binary numbers from root to leaf
- Brightcove任命Dan Freund为首席营收官
猜你喜欢

Rust:axum学习笔记(1) hello world

Detailed explanation of TCP protocol knowledge

ArrayList与LinkedList区别

C how to set different text watermarks for each page of word

Worship the 321 page PDF of the core technology of Internet entrepreneurship that Alibaba is pushing internally. It's really kneeling

2022年危险化学品经营单位主要负责人复训题库及答案

Redis面试题(2022)

Framework learning journey: init process startup process

Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化

Okaleido生态核心权益OKA,尽在聚变Mining模式
随机推荐
Session&Cookie&token
面试题 02.05. 链表求和
C how to set different text watermarks for each page of word
List Simulation Implementation
对NIO的初步理解
Remember the major performance problems caused by a TCP packet loss
链表内指定区间反转
Which securities company has the lowest handling charge? Is it safe to open an account on your mobile phone
2022危险化学品生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作
2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units
Learning route from junior programmer to architect + complete version of supporting learning resources
C language learning notes - memory management
Cool Lehman VR panorama paves the way for you to start a business
卷积神经网络——24位彩色图像的卷积的详细介绍
C get UUID
playwright网络爬虫实战案例分享
Plato Farm全新玩法,套利ePLATO稳获超高收益
Sum of binary numbers from root to leaf
Rust:axum学习笔记(1) hello world
从根到叶的二进制数之和