SpringMvc 拦截器
SpringMvc 拦截器介绍#
- SpringMVC拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似与servlet中的Filter。
- SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor来实现的。
- 在SpringMVC中定义一个Interceptor非常简单,主要有4种方式:
1)实现Spring的HandlerInterceptor接口;
2)继承实现了HandlerInterceptor接口的类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter;
3)实现Spring的WebRequestInterceptor接口;
4)继承实现了WebRequestInterceptor的类;
定义拦截器#
实现HandlerIntercepter接口:#
public class MyHandlerIntercepter1 implements HandlerInterceptor{ //Handler执行前调用 //应用场景:登录认证、身份授权 //返回值为true则是放行,为false是不放行 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return false; } //进入Handler开始执行,并且在返回ModelAndView之前调用 //应用场景:对ModelAndView对象操作,可以把公共模型数据传到前台,可以统一指定视图 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } //执行完Handler之后调用 //应用场景:统一异常处理、统一日志处理 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
配置拦截器#
SpringMvc 拦截器是绑定在HandlerMapping中的。即:如果某个HandlerMapping中配置拦截,则该HandlerMapping映射成功的Handler会使用该拦截器。
针对单个HandlerMapping配置#
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="interceptor" /> </list> </property> </bean> <bean id="interceptor" class="com.cyb.ssm.interceptor.MyHandlerInterceptor" />
全局拦截器配置(推荐)#
SpringMvc的全局拦截器配置,其实是把配置的拦截器注入到每个已初始化的HandlerMapping中了。
<!-- 配置全局mapping的拦截器 --> <mvc:interceptors> <!-- 公共拦截器可以拦截所有请求,而且可以有多个 --> <bean class="com.cyb.ssm.interceptor.MyHandlerInterceptor1" /> <bean class="com.cyb.ssm.interceptor.MyHandlerInterceptor2" /> <!-- 如果有多个拦截器,则按照顺序进行配置 --> <mvc:interceptor> <!-- /**表示所有URL和子URL路径 --> <mvc:mapping path="/test/**" /> <!-- 特定请求的拦截器只能有一个 --> <bean class="com.cyb.ssm.interceptor.MyHandlerInterceptor3" /> </mvc:interceptor> </mvc:interceptors>
多拦截器拦截规则#
如果有多个拦截器,那么配置到springmvc.xml中最上面的拦截器,拦截优先级最高。
CORS实现(Springmvc4.x以上)#
- 某个方法可以跨域访问,在某个方法上使用@CrossOrigin
- 某个Controller类都可以跨域访问,在类上使用@CrossOrigin
- 全局访问,在springmvc.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 处理器类的扫描 --> <context:component-scan base-package="com.cyb.springmvc.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 解决跨域请求 --> <mvc:cors> <mvc:mapping path="/**" /> </mvc:cors> </beans>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?