spring中拦截器主要分两种,一个是HandlerInterceptor,一个是MethodInterceptor
一、HandlerInterceptor
HandlerInterceptor是springMVC项目中的拦截器,它拦截的目标是请求的地址,比MethodInterceptor先执行。
1、创建我们自己的拦截器类并实现 HandlerInterceptor 接口或继承HandlerInterceptorAdapter。
2、创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。
3、实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)。
package com.qicheshetuan.backend.web.interceptor; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Created by 15117 on 2018/4/27.
*/
public class TestInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("在控制器执行前调用 ");
Boolean flag = true;
if(flag){
System.out.println(request.getMethod());
return true;
}else{
System.out.println(request.getMethod());
return false;
}
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("在后端控制器执行后调用 ");
super.postHandle(request, response, handler, modelAndView);
} @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("整个请求执行完成后调用 ");
super.afterCompletion(request, response, handler, ex);
}
}
package com.qicheshetuan.backend.web.interceptor; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /**
* Created by 15117 on 2018/4/27.
*/
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
二、MethodInterceptor
MethodInterceptor是AOP项目中的拦截器,它拦截的目标是方法,即使不是controller中的方法。实现MethodInterceptor拦截器大致也分为两种,一种是实现MethodInterceptor接口,另一种利用AspectJ的注解或配置。
基于注解的AspectJ方式
package com.qicheshetuan.backend.web.interceptor; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; /**
* Created by 15117 on 2018/4/27.
*/
@Component
@Aspect
public class AspectJInterceptor { @Around("execution(* com.qicheshetuan.backend.web.controller.AutoCommunityController.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("方法执行前");
Object proceed = pjp.proceed();
System.out.println("方法执行后");
return proceed;
}
}
package com.qicheshetuan.backend.web.interceptor; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* Created by 15117 on 2018/4/27.
*/
@Component
@Aspect
public class AspectJInterceptor { @Pointcut("execution(* com.qicheshetuan.backend.web.controller..*.*(..))")
public void execMethod(){ }
@Before("execMethod()")
public void beforeMethod(JoinPoint joinPoint){
System.out.println("方法前执行");
System.out.println(joinPoint.getSignature().getName());
}
}
备注:
近日,发现Spring 5.0 以后WebMvcConfigurerAdapter会过时,新实现
(一)
@Configuration
public class TestConfig implements WebMvcConfigurer {
. . .
}
(二、推荐)
@Configuration
public class TestConfig extends WebMvcConfigurationSupport {
. . .
}
由于spring boot 2.x依赖的spring 5.x版本,使用spring 5.x时,静态资源也会执行自定义的拦截器
所有导致静态资源不可访问的问题
解决方法:
在添加自定义拦截器是,过滤掉静态资源路径