开发基础
开发基础笔记
mybatis-plus通用的service方法
通过jenkins配置前后端自动打包及发布
mybatis-plus实现分页的方式
Java极客技术公众号PDF
狂神说SpringBoot
Hashids java 版使用
SpringBoot
1、Web快速开发
2、结果集的封装
3、集成MyBatis实现数据库操作
4、Springboot @Validated参数校验
5、SpringBoot全局异常处理
6、拦截器HandlerInterceptor
7、集成Swagger实现API自动生成
8、集成knife4j实现API自动生成
9、Springboot集成MyBatis-Plus快速入门
10、springboot自定义注解及AOP切面使用
11、使用Shiro实现登陆和权限认证,基于MyBatis
12、集成SpringSecurity实现授权认证
13、SpringBoot集成EasyExcel实现数据导入与导出
14、Spring Task定时任务的实现
15、Quartz快速上手与实践
16、如何用代码实现Spring IOC
17、SpringBoot集成JWT,实现接口的鉴权交互
SpringCloud
Nacos作为服务注册中心
seata1.6.1 结合springcloud实现分布锁的技术笔记
一些技术博客推荐
前端面试相关
看这一篇就够了
java.util包常用的类和接口
CountDownLatch介绍与使用
Common-lang3使用入门
Hutool简单使用入门
lombok 介绍及基本使用方法
git项目统计成员代码行数和提交的次数
mysql 逗号分隔的数据 like查询
使用sonar进行代码质量检查
线上使用jmeter进行压测的时候,使用Arthas诊断工具排查响应慢的接口
php结合phpstudy8、vscode开启xdebug进行代码调试
node-red使用入门
本文档使用 MrDoc 发布
-
+
首页
6、拦截器HandlerInterceptor
## 前言 很多组件都有拦截器的概念,比如 mybatis拦截器,axios拦截器 因为他们都涉及到数据的接收与发送,接收前和发送前,都有必要对数据进行拦截,因为任何时候都不要相信用户的输入。 ### 1、实现HandlerInterceptor接口 新建一个MyInterceptor 实现HandlerInterceptor接口 。 ~~~java public class MyInterceptor implements HandlerInterceptor { /** * 1、preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用。 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(new Date() + "--preHandle:" + request.getRequestURL()); StringBuffer url = request.getRequestURL(); if(url.substring(url.lastIndexOf("/")+1,url.length()).equals("GetName")){ System.out.println("------模糊查询方法------"); String nickName = request.getParameter("nickName"); System.out.println("获取的是nickName:"+nickName+",我可以根据获取的值判断是否是sql注入等操作"); } return true; } /** * 2、这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(new Date() + "--postHandle:" + request.getRequestURL()); } /** * 3、该方法将在postHandle请求完成之后,也就是DispatcherServlet渲染了视图执行 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println(new Date() + "--afterCompletion:" + request.getRequestURL()); } } ~~~ > MyInterceptor 中的方法执行顺序为 preHandle – Controller 方法 – postHandle – afterCompletion ,所以拦截器实际上可以对 Controller 方法执行前后进行拦截监控。 > > 最后还有一个非常重要的注意点, preHandle 需要返回布尔类型的值。 preHandle 返回 true 时,对控制器方法的请求才能到达控制器,继而到达 postHandle 和 afterCompletion 方法;如果 preHandle 返回 false ,后面的方法都不会执行。 ### 2、添加Web项目的拦截器 MyInterceptor自定义的拦截器是写好了,但是默认是不生效的,需要在webconfig配置中配置一下才能生效。 addPathPatterns("/**") 表示 所有的请求,都要路由到 拦截器上进行处理。 ~~~java @Configuration public class WebConfig implements WebMvcConfigurer { /** * 添加Web项目的拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { // 对所有访问路径,都通过MyInterceptor类型的拦截器进行拦截 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); } } ~~~ ### 3、查看效果 访问 http://localhost:8080/GetName?nickName=hello 查看后台打印的日志。可以看到 ~~~ ------模糊查询方法------ 获取的是nickName:hello,我可以根据获取的值判断是否是sql注入等操作 Thu Nov 03 12:48:35 CST 2022--postHandle:http://localhost:8080/GetName Thu Nov 03 12:48:35 CST 2022--afterCompletion:http://localhost:8080/GetName Thu Nov 03 12:48:35 CST 2022--preHandle:http://localhost:8080/error Thu Nov 03 12:48:35 CST 2022--postHandle:http://localhost:8080/error Thu Nov 03 12:48:35 CST 2022--afterCompletion:http://localhost:8080/error ~~~
superadmin
2023年11月3日 17:19
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码