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 那 ...
随机推荐
- Serverless 工程实践 | 细数 Serverless 的配套服务
简介: 上文说到云计算的十余年发展让整个互联网行业发生了翻天覆地的变化,Serverless 作为云计算的产物,或者说是云计算在某个时代的表现,被很多人认为是真正意义上的云计算,关于"Se ...
- LlamaIndex 起步教程(本地模型)
提示:确保您已先按照自定义安装步骤操作. 这是一个著名的"五行代码"起步示例,使用本地 LLM(大语言模型)和嵌入模型.我们将使用 BAAI/bge-small-en-v1.5 作 ...
- git fatal detected dubious ownership in repository 的解决方法
我换了一台电脑,将旧电脑的硬盘换到新电脑上:我装了双系统,切换到另一个系统时:我发现了 git 代码仓库无法执行 git 命令,不断报错 fatal: detected dubious ownersh ...
- WPF 简单聊聊如何使用 DrawGlyphRun 绘制文本
在 WPF 里面,提供的使用底层的方法绘制文本是通过 DrawGlyphRun 的方式,此方法适合用在需要对文本进行精细控制的定制化控件上.此方法特别底层而让调用方法比较复杂,本文告诉大家一些简单的使 ...
- 2018-8-10-win10-uwp-如何开始写-uwp-程序
title author date CreateTime categories win10 uwp 如何开始写 uwp 程序 lindexi 2018-08-10 19:16:50 +0800 201 ...
- 手把手搭建WebSocket多人在线聊天室(SpringBoot+WebSocket)
前言 本文中搭建了一个简易的多人聊天室,使用了WebSocket的基础特性. 源代码来自老外的一篇好文: https://www.callicoder.com/spring-boot-websocke ...
- 使用Lagent AgentLego 搭建智能体-书生浦语大模型实战营第二期第6节作业
书生浦语大模型实战营第二期第6节作业 对于这个作业,这里只给出截图,不给详细过程,因为确实没有什么好写的,会做Demo那个作业就会做这个作业.具体的步骤可以查看官方教程. 基础作业 完成 Lagent ...
- docker / compose 的安装 和 体验
文档 官网文档 视频 视频 简介 课程内容 1.Docker Compose 容器编排 2.Docker Swarm #集群 热扩容 需要在阿里上买服务器,至少冲100+以上的人民币 文档: 集群方式 ...
- Go语言的包(package)
包名是从$GOPATH/src/后开始计算的,使用/进行路径分隔. 想要被别的包调用标识符都要的首字母大. 单行导入和多行导入. 导入包不想使用内部的标识符,需要使用匿名导入. 每个包导入的时候会自动 ...
- 手机自适应的单位rem,与自适应网页的区别
一个网站有的会分为pc站和移动站,有的网站只有pc站,而现在更多的是自适应的站点.现在针对自适应的网页设计有很多模板,如bootstrap,它会让你轻松定制一个只适应网站,而现在大多数的网站并不是靠程 ...