SpringMVC拦截器
拦截器类似于Filter过滤器,它是springMVC特有的,它可以预处理和后处理,我们可以定义一些拦截器来实现特定的业务。
过滤器与拦截器本质区别:
(1)拦截器时AOP思想的具体应用(一个横切面,直接切进请求响应中去)。
(2)拦截器时spring MVC特有的。
(3)拦截器只会拦截 访问控制器的方法,如果访问静态资源如:.jsp/html/css/image/js 时,它不会去拦截,而Filter过滤器无论什么都会去拦截。
自定义拦截器需要两步:
第一步:编写自定义类实现 HandlerInterceptor 接口,且必须重写方法;
第二步:在配置类中,注册拦截器,实现 WebMvcConfigurer接口,重写对应的方法;关于配置类 我在这边文章有记录:https://www.jb51.net/article/204128.htm
(1)public boolean preHandle() {}
请求前处理的逻辑 - 前置。
方法返回值:返回布尔值,返回true表示可以执行后续代码,返回false程序会终止。
(2)public void postHandle(){}
请响应前处理的逻辑 - 后置。
方法返回值:无返回值。
目录:
package com.lxc.springboot.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class MyInterceptor implements HandlerInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(LogInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
s
// 拦截前的操作
System.out.println("-----------前置拦截-----------");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// 拦截后的操作
System.out.println("------------后置拦截------------");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 可以做一些清理工作
}
}
注册拦截器:
package com.lxc.springboot.config;
import com.lxc.springboot.intercetor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
@Resource
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addInterceptor():注册拦截器,参数是一个拦截器
// addPathPatterns(): 路径映射,哪些路径需要被拦截,
@Component // 增加这个注解,让spring能扫描到这个类
public class LogInterceptor implements HandlerInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(LogInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
LOG.info("【全局拦截器】");
LOG.info("*********** InterceptorLog日志开始 *********** ");
LOG.info("* 请求地址: {}, 方法: {}", request.getRequestURL().toString(), request.getMethod());
LOG.info("* 远程地址: {}, 域名: {}, 端口: {}", request.getRemoteAddr(), request.getRemoteHost(), request.getRemotePort());
long startTime = System.currentTimeMillis();
request.setAttribute("boot-responseTime", startTime);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// request.getAttribute("boot-responseTime") 返回的是Object
long startTimed = (long) request.getAttribute("boot-responseTime");
LOG.info(" *********** InterceptorLog 结束,耗时: {} ms *********** ", System.currentTimeMillis() - startTimed);
}
}
在配置类中注册拦截器:
package com.lxc.springboot.config;
import com.lxc.springboot.intercetor.LogInterceptor;
import com.lxc.springboot.intercetor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
@Resource
private MyInterceptor myInterceptor;
@Resource
private LogInterceptor logInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
}
测试:
到此这篇关于简单总结SpringMVC拦截器的使用方法的文章就介绍到这了,更多相关SpringMVC拦截器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!