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的更多相关文章

  1. 模仿Struts2的Interceptor拦截器实现

    模仿Struts2的Interceptor拦截器实现 public interface Invocation { public Object invoke(); } public interface ...

  2. SpringMVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  3. Spring MVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  4. SpringMvc中Interceptor拦截器用法

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆等. 一. 使用场景 1 ...

  5. SpringMVC 中的Interceptor 拦截器

    1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors>  <!-- 日志拦截器 -->  <mvc:interceptor> ...

  6. SpringBoot-SpringMvc的Interceptor拦截器配置

    Interceptor拦截器实现对每一个用户请求处理前后的业务处理,比如我们需要对用户请求进行响应时间的记录,需要记录请求从开始到结束所耗的时间,这时我们就需要用到拦截器了 下面我们以记录请求处理时间 ...

  7. SpringMVC中使用Interceptor拦截器顺序

    一.简介 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验 证,或者是来判断用户是否登陆,或者是像1 ...

  8. 【tmos】如何在Interceptor拦截器中注入其他数据

    光是这样是获取不到weixinConfig内容的 public class WebLoginInterceptor implements HandlerInterceptor { @Autowired ...

  9. SpringMVC中的Interceptor拦截器及与Filter区别

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  10. [转]SpringMVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

随机推荐

  1. 路由跳转报错:Uncaught (in promise) NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated",...

    记录一个路由跳转的报错: 将换成

  2. 函数编程:强大的 Stream API

    函数编程:强大的 Stream API 每博一文案 只要有人的地方,世界就不会是冰冷的,我们可以平凡,但绝对不可以平庸. ------ <平凡的世界> 人活着,就得随时准备经受磨难.他已经 ...

  3. surging版本有哪些?

    surging 一直在升级开发各个版本,以下是各个版本的费用,有需要可以联系或者可以聘请我为架构师,技术顾问,后期会升级多语言版本,推广至海外. 基于surging 的物联网技术架构 平台版本:

  4. 如何在低代码平台中引用 JavaScript ?

    引言 在当今快速发展的数字化时代,企业对业务应用的需求日益复杂且多元.低代码开发平台作为一个创新的解决方案,以直观易用的设计理念,打破了传统的编程壁垒,让非技术人员也能轻松构建功能完备的Web应用程序 ...

  5. VueJs创建网易音乐播放器和vueJs常见错误处理

    学习vuejs之后,总得自己上手写一些完整的应用,才能够更加了解各个结构与组件之间的运用. 下面从最基础的准备工作开始: 用vue-cli搭建vue应用:先确保自己已经安装Node环境. (1)Win ...

  6. leaflet常用插件汇总介绍

    1.Leaflet Ant Path(线条流动效果) 在交通项目.管网应用的项目中,常常需要标注出道路的走向.河流的流向或者管线的流向等等,Leaflet Ant Path能够很好的解决这类问题: g ...

  7. web3.js:使用eth包

    原文在这里 简介 web3-eth包提供了一套强大的功能,可以与以太坊区块链和智能合约进行交互.在本教程中,我们将指导您如何使用web3.js版本4的web3-eth包的基础知识.我们将在整个示例中使 ...

  8. JSON返回结果修改null为"";json字段为null时输出空字符串

    简介 (Introduction): 背景 json字符串返回的需要"",但是却是null,怎么处理呢,如下 结构图 1 [ 2 { 3 "1":{ 4 &qu ...

  9. 让智慧物联赋能高效生产, AIRIOT助力数字化油田转型升级

    ​ 近年来,中国石油行业为了推进工业化和信息化深度融合,充分结合勘探开发.生产科研和经营管理的实际需求,积极通过信息化建设促进油田业务转型升级.在勘探开发与管理的领域中,油气生产物联网系统是一个极其重 ...

  10. 深入Django项目实战与最佳实践

    title: 深入Django项目实战与最佳实践 date: 2024/5/19 21:41:38 updated: 2024/5/19 21:41:38 categories: 后端开发 tags: ...