这节记录下如何使用aop或者使用interceptor实现用户请求的是否带token,本文只是简单检查用户请求是否带参数token,并不对token的正确性进行验证。通常要从后台缓存中进行token校验。
第一种方式:拦截器方式
第一步:新建一个拦截器package,名称为interceptor,新建一个拦截器。
java">@Component
public class TokenInterceptor implements HandlerInterceptor {
Logger logger = LoggerFactory.getLogger(TokenInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
logger.info("preHandle执行中");
String token = request.getHeader("token");
if(StringUtils.isBlank(token)){
throw new BussinessException("无token,参数错误");
}
if(!token.equals("ceshi")){
throw new BussinessException("token参数错误");
};
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info("postHandle执行中");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.info("afterCompletion执行中");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
第二步:在WebMvcConfigurer配置类中注册拦截器
java">
@Override
public void addInterceptors(InterceptorRegistry registry) {
WebMvcConfigurer.super.addInterceptors(registry);
registry.addInterceptor(tokenInterceptor);//默认所有的url拦截
}
第二种方式:使用AOP
编写一个aop类,添加上注解@Aspect和@Component。重写下面的方法
java">@Aspect
@Component
public class LoggerAspect {
private static final Logger logger = LoggerFactory.getLogger(LoggerAspect.class);
@Pointcut("execution(* cn.wcyf.wcai.controller.front.*.*(..))") // 定义切点
public void pointcut() {}
@Before("pointcut()")
public void before() {
logger.info("before方法中: ");
}
@After("pointcut()")
public void after() {
logger.info("after方法中: " );
}
@AfterReturning("pointcut()")
public void afterReturn() {
logger.info("AfterReturning方法中: " );
}
@AfterThrowing("pointcut()")
public void threw() {
logger.info("AfterThrowing方法中: " );
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
logger.info("Around方法开始: " + proceedingJoinPoint.getSignature());
try {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(attrs!=null){
HttpServletRequest request = attrs.getRequest();
String token = request.getHeader("token");
if(StringUtils.isBlank(token)){
throw new BussinessException("参数错误,token为空");
}
if(!token.equals("ceshi")){
throw new BussinessException("token错误,这是aop测试");
}
}
Object result = proceedingJoinPoint.proceed(); // 调用目标方法
logger.info("Around方法结束: " + proceedingJoinPoint.getSignature() + ", 返回值: " + result);
return result;
} catch (Throwable ex) {
logger.info("Around方法捕获异常: " + proceedingJoinPoint.getSignature() + ", 异常: " + ex.getMessage());
throw ex; // 重新抛出异常,以便Spring可以正确处理
}
}
}
Object result = proceedingJoinPoint.proceed(); // 调用目标方法
这个代码可以执行调用目标方法,在这段代码前面可以实现前置通知,在这段代码后可以实现后置通知。