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 那 ...
随机推荐
- 京东:Flink SQL 优化实战
简介: 本文着重从 shuffle.join 方式的选择.对象重用.UDF 重用等方面介绍了京东在 Flink SQL 任务方面做的优化措施. 本文作者为京东算法服务部的张颖和段学浩,并由 Apach ...
- [Go] gorm 错误处理 与 链式/Finisher方法
使用 gorm 在调用 Finisher 方法之后,建议都进行错误检查. Finishers 是会立即执行注册回调的方法,然后生成并执行 SQL,比如这些方法: Create, First, Find ...
- dotnet SemanticKernel 入门 调用原生本机技能
本文将告诉大家如何在 SemanticKernel 里面调用原生本机技能,所谓原生本机技能就是使用 C# 代码编写的原生本地逻辑技能,这里的技能可讲的可不是游戏角色里面的技能哈,指的是实现某个功能的技 ...
- dotnet 构建还原失败 NuGet.targets 错误可能原因
我在一次断电关机之后,发现我所有的项目都构建不通过了,提示在 NuGet.targets 文件的第 130 行错误.原因就是存在有某个被项目引用的 NuGet 包被损坏,在进行 NuGet 还原时读取 ...
- WPF 简单实现一个支持删除自身的应用
我准备写一个逗比的应用,然而我担心被小伙伴看到这个应用的文件从而知道是我写的,于是我就需要实现让应用能自删除的功能.核心实现方法就是调用 cmd 传入命令行,等待几秒之后删除文件 应用程序在运行时,是 ...
- dotnet C# 调用委托的 GetInvocationList 的对象分配
本文也叫跟着 Stephen Toub 大佬学性能优化系列,这是我从 Stephen Toub 大佬给 WPF 框架做性能优化学到的知识,在热路径下,也就是频繁调用的模块,如果调用了委托的 GetIn ...
- 优秀的 Modbus 从站(从机、服务端)仿真器、串口调试工具
目录 优秀的 Modbus 从站(从机.服务端)仿真器.串口调试工具 主要功能 软件截图 优秀的 Modbus 从站(从机.服务端)仿真器.串口调试工具 官网下载地址:http://www.redis ...
- 《HelloGitHub》第 97 期
兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...
- Oracle和达梦:根据外键名字查询表名
根据外键名字查询表名 select * from user_cons_columns cl where cl.constraint_name = '外键名';
- Linux基础03-Linux文件操作命令
其实啊,说起计算机操作,大部分情况下就是"增删改查"这四个大字儿,文件操作也是这么回事儿. 就是改文件的时候得用点专门的编辑器,比如那个Vim. 不过Vim这东西,真心不是一两句话 ...