springboot 中拦截器的使用
来源:互联网 发布:特征向量组成的矩阵 编辑:程序博客网 时间:2024/06/14 18:47
第一步:创建一个实现 HandlerInterceptor 的类。
import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class WebInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("============== request before =============="); return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("============== request =============="); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("============== request completion =============="); }}
说明:
1. preHandle 表示方法请求前的处理,若其返回值为FALSE,就中断请求目标方法了,只有返回值为TRUE时才继续执行方法内容。
2. postHandle 是在目标方法执行完之后执行的。
第二步:注册拦截器。创建一个配置类并继承 WebMvcConfigurerAdapter。重写 addInterceptors 方法。
import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter{ public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new WebInterceptor()); System.out.println("=========== 拦截器注册完毕 ==========="); }}
第三步:编写普通的controller 和方法。
import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class WebController { @RequestMapping("/interceptor") public String interceptor(Model model) { model.addAttribute("hello", "hello"); System.out.println("model : " + model); return "inderceptor"; }}
最后:执行controller 方法即可在控制台看到效果。
============== request before ==============model : {hello=hello}============== request ============================ request completion==============
阅读全文
0 0
- springboot 中拦截器的使用
- SpringBoot拦截器的使用
- SpringBoot拦截器的使用
- 在SpringBoot中使用 拦截器
- 在SpringBoot中使用 拦截器
- springboot使用拦截器
- 使用springBoot的拦截器功能介绍
- SpringBoot的拦截器中依赖注入为null
- SpringBoot中实现拦截器, 并实现对404和500等错误的拦截。
- springboot 配置使用拦截器 interceptor
- SpringBoot学习笔记之拦截器使用
- springboot的监听器,拦截器与过滤器
- SpringBoot中实现拦截器级别的URl访问过快拦截,并利用JPA实现IP黑名单的功能。
- springboot 拦截器配置
- SpringBoot--实现拦截器
- springboot添加拦截器
- springboot添加拦截器
- SpringBoot拦截器
- 设计模式之策略模式
- 文章标题
- python自动化之路-day1
- Java练习题17 窗口程序“浏览器”
- 排序:快速排序与归并排序
- springboot 中拦截器的使用
- Full Binary Tree Picture——hihoCoder177
- LeetCode216. Combination Sum III
- 数据挖掘系列(10)——卷积神经网络算法的一个实现
- hdu-1727-Hastiness(字符处理)
- 【转载】解决使用tomcat7-maven-plugin插件时 “Application already exists at path”的问题
- 快速搭建视频直播平台
- 《白话深度学习与Tensorflow》学习笔记(7)RBM限制玻尔兹曼机
- 逻辑电路,零和一的实验