Interceptor拦截器demo
Interceptor拦截器demo
##接口测试类
@RestController
public class TestController { @RequestMapping(value = "/myInterceptorsTestUrl", method = RequestMethod.GET)
public String myInterceptorsTestUrl(HttpServletRequest request) {
return request.getRequestURL().toString() +"-"+ request.getAttribute("name");
} @RequestMapping(value = "/testUrl", method = RequestMethod.GET)
public String testUrl(HttpServletRequest request) {
return request.getRequestURL().toString() +"-"+ request.getAttribute("name");
} } ##拦截器类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MyInterceptor implements HandlerInterceptor {
private Logger logger = LoggerFactory.getLogger(this.getClass()); @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle>>>拦截器");
//设置编码
request.setCharacterEncoding("UTF-8");
//设置属性值
request.setAttribute("name","hello,interceptor"); //参数
String path = request.getRequestURI(); String reqMethod = request.getMethod();
HandlerMethod hm = (HandlerMethod) handler;
String userAgent = request.getHeader("User-Agent");
String reqIp = getReqIpAddr(request);
String reqUri = request.getServletPath();
System.out.println("path=" + path);
System.out.println("reqMethod=" + reqMethod);
System.out.println("userAgent=" + userAgent);
System.out.println("reqIp=" + reqIp);
System.out.println("reqUri=" + reqUri); //调用
// hm.getMethod().invoke(hm);
String methodName = hm.getMethod().getName();
System.out.println("methodName=" + methodName); String beanName = hm.getBeanType().getName();
System.out.println("beanName=" + beanName); return true; //return false; 不往后续执行
} protected String getReqIpAddr(HttpServletRequest request) {
String ip = null;
try {
ip = request.getHeader("x-forwarded-for");
if (!StringUtils.hasText(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (!StringUtils.hasText(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (!StringUtils.hasText(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
} catch (Exception e) {
logger.error("",e);
}
return ip;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle>>>拦截器");
} @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion>>>拦截器");
}
} //拦截器注册类
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.ArrayList;
import java.util.List; @Configuration
public class WebAPPMvcConfigurer implements WebMvcConfigurer { /**
* 拦截器注册
* @param registry
*
* http://localhost:8080/testUrl
* 页面输出:http://localhost:8080/testUrl-null
*
* http://localhost:8080/myInterceptorsTestUrl
* 页面输出:http://localhost:8080/myInterceptorsTestUrl-hello,interceptor
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//匹配规则
// registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); //按请求地址来匹配,字符集合
List<String> listPath = new ArrayList<>();
listPath.add("/myInterceptorsTestUrl");
registry.addInterceptor(new MyInterceptor()).addPathPatterns(listPath);
} }
或者在applicationContext.xml配置文件中配置
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/myInterceptorsTestUrl"/>
<bean class="com.example.mytest.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors> </beans>
//springboot启动类加载配置文件 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource; @EnableAspectJAutoProxy
@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class MyApplication { public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
} }
Interceptor拦截器demo的更多相关文章
- 模仿Struts2的Interceptor拦截器实现
模仿Struts2的Interceptor拦截器实现 public interface Invocation { public Object invoke(); } public interface ...
- SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- Spring MVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- SpringMvc中Interceptor拦截器用法
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆等. 一. 使用场景 1 ...
- SpringMVC 中的Interceptor 拦截器
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
- SpringBoot-SpringMvc的Interceptor拦截器配置
Interceptor拦截器实现对每一个用户请求处理前后的业务处理,比如我们需要对用户请求进行响应时间的记录,需要记录请求从开始到结束所耗的时间,这时我们就需要用到拦截器了 下面我们以记录请求处理时间 ...
- SpringMVC中使用Interceptor拦截器顺序
一.简介 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验 证,或者是来判断用户是否登陆,或者是像1 ...
- 【tmos】如何在Interceptor拦截器中注入其他数据
光是这样是获取不到weixinConfig内容的 public class WebLoginInterceptor implements HandlerInterceptor { @Autowired ...
- SpringMVC中的Interceptor拦截器及与Filter区别
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- [转]SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
随机推荐
- [FAQ] Mac Mini 怎么让主机不休眠
Mac Mini 的防止休眠设置,在首选项,显示器里. 显示器里找到高级按钮. 然后有个开关是:显示器关闭时,防止自动进入睡眠.打开这个开关即可防止自动睡眠. Link:https://www.cnb ...
- Unity3D OpenVR 虚拟现实 保龄球打砖块游戏开发
据说水哥买了 Valve Index 设备,既然这个设备这么贵,不开发点有(zhi)趣(zhang)游戏就感觉对不起这个设备.本文将来开始着手开发一个可玩性不大,观赏性极强的保龄球打砖块游戏.这仅仅只 ...
- EPAI手绘建模APP介绍
本软件是一个基于OpenCASCADE.android JNI开发的APP.底层用c++实现,UI层用android实现.底层和UI层之间通过JNI接口和json数据格式通信. ...
- 从大数据平台CDP的架构看大数据的发展趋势
CDP(Cloudera Data Platform)是Cloudera 和 HortonWorks 合并后推出的新一代大数据平台 ,并正在逐步停止对原有的大数据平台 CDH 和 HDP 的维护.笔记 ...
- 【python爬虫案例】用python爬豆瓣音乐TOP250排行榜!
目录 一.爬虫对象-豆瓣音乐TOP250 二.python爬虫代码讲解 三.同步视频 四.获取完整源码 一.爬虫对象-豆瓣音乐TOP250 今天我们分享一期python爬虫案例讲解.爬取对象是,豆瓣音 ...
- Mark Lee:Splashtop 如何成为最新的 10 亿美元估值技术独角兽
从左至右:Splashtop联合创始人Rob.Philip.Mark和Thomas Splashtop 刚刚完成了由我们的长期投资者 Sapphire Ventures 领投的 5000 万美元的新融 ...
- 推荐几款火爆的Python在线编辑器
在当今数字化时代,编程已成为一项不可或缺的技能.Python作为一种简单易学且功能强大的编程语言,受到了广大编程爱好者和专业开发人员的青睐.为了方便大家随时随地编写和运行Python代码,市面上涌现了 ...
- 使用systemctl管理服务(nginx)
首先调整好路径信息,修改配置文件vim /usr/lib/systemd/system/nginx.service [Unit]Description=The nginx HTTP and rever ...
- Django性能优化:提升加载速度
title: Django性能优化:提升加载速度 date: 2024/5/20 20:16:28 updated: 2024/5/20 20:16:28 categories: 后端开发 tags: ...
- web component基础概念及使用
概念和使用 作为开发者,我们都知道尽可能多的重用代码是一个好主意.这对于自定义标记结构来说通常不是那么容易 - 想想复杂的HTML(以及相关的样式和脚本),有时您不得不写代码来呈现自定义UI控件,并且 ...