拦截器基本操作
声明一个拦截器类需要实现HandlerInterceptor接口
package com.cn.sola.intercepteor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class Myinterceptor implements HandlerInterceptor{
private Logger log = LoggerFactory.getLogger(Myinterceptor.class);
//操作前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HandlerMethod handlermethod = (HandlerMethod) handler;
log.info("【****** MyInterceptor.preHandle ******】" + handlermethod.getBean().getClass().getSimpleName());
// 如果返回false表示不继续请求,如果返回true表示继续请求
return true;
}
//正在处理
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
HandlerMethod handlermethod = (HandlerMethod) handler;
log.info("【****** MyInterceptor.postHandle ******】" + handlermethod.getBean().getClass().getCanonicalName());
log.info("【****** MyInterceptor.postHandle ******】" + request.getServletPath());
}
//操作后
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
log.info("【****** MyInterceptor.afterCompletion ******】" + "拦截处理完毕");
}
}
在Config配置类注册一下基本拦截器
package com.cn.sola.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.cn.sola.intercepteor.Myinterceptor;
@SuppressWarnings("deprecation")
@Configuration
public class MyWebApplicationConfig extends WebMvcConfigurerAdapter{// 定义MVC配置
@Override
public void addInterceptors(InterceptorRegistry registry) {//注解拦截器
registry.addInterceptor(new Myinterceptor()).addPathPatterns("/**");//拦截器路径
super.addInterceptors(registry);
}
}
运行即可
拦截器小应用 可以获取每个方法的执行时间
package com.cn.sola.intercepteor;
import java.util.Timer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class Myinterceptor implements HandlerInterceptor{
private Logger log = LoggerFactory.getLogger(Myinterceptor.class);
private Long times;
private Long timee;
//操作前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
times=System.currentTimeMillis();
HandlerMethod handlermethod = (HandlerMethod) handler;
log.info("【****** MyInterceptor.preHandle ******】" + handlermethod.getBean().getClass().getSimpleName());
// 如果返回false表示不继续请求,如果返回true表示继续请求
return true;
}
//正在处理
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
HandlerMethod handlermethod = (HandlerMethod) handler;
log.info("【****** MyInterceptor.postHandle ******】" + handlermethod.getBean().getClass().getCanonicalName());
log.info("【****** MyInterceptor.postHandle ******】" + request.getServletPath());
}
//操作后
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
log.info("【****** MyInterceptor.afterCompletion ******】" + "拦截处理完毕");
timee = System.currentTimeMillis();
System.out.println(timee-times+"ms");
}
}
AOP拦截器
首先添加依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
编写一个切面类
package com.cn.sola.config;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class ServiceAspect {//此时定义一个业务层拦截处理
private Logger log = LoggerFactory.getLogger(ServiceAspect.class);
//切面表达式 环绕
@Around("execution(* com.cn.sola..service..*.*(..))")
public Object arroundInvoke(ProceedingJoinPoint ponit) throws Throwable{
//获取传递参数数组
Object[] args = ponit.getArgs();
this.log.info("【****** Service-Before ******】执行参数" + Arrays.toString(args));
//执行放行
Object object = ponit.proceed(args);
this.log.info("【****** Service-After ******】执行参数" + object);
return object;
}
}