注解形式
- 注解形式灵活,在需要拦截的方法上直接加就可以
- 可以通过传递参数动态实现部分逻辑
- 新开发的功能使用,jar包内的拦截不适用
定义一个注解
package org.a.interfaces.annotation.constant;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
/**
* 拦截非表请求报文和请求地址等信息,插入表
* @author smy
*/
public @interface EnableRequestRecord {
//默认开启
boolean enable () default true;
//接口编码
String interfaceCode() default "";
//外部系统编码
String externalSystemCode() default "";
}
定义拦截的具体实现
package org.a.interfaces.annotation.aspect;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
/**
* 拦截的具体实现
*/
@Component
@Aspect
@Order(1)
public class EnableRequestRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(EnableRequestRecordAspect.class);
//如果注解和拦截是现在同一个包下可以这样写 @Pointcut("@annotation(EnableRequestRecord)")
@Pointcut("@annotation(org.a.interfaces.annotation.constant.EnableRequestRecord)")
public void pointcut(){}
/*
拦截的具体实现
*/
@Around("pointcut()")
public String doAround(ProceedingJoinPoint joinPoint) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String requestURI = request.getRequestURI();
String requestMethod = request.getMethod();
String remoteAddr = request.getRemoteAddr();
// 获得当前访问的class
Class<?> clazz = joinPoint.getTarget().getClass();
// 获得访问的方法名
String methodName = joinPoint.getSignature().getName();
logger.info("===smy===methodName"+methodName);
// 得到方法的参数的类型
Class<?>[] argClass = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
Method method = clazz.getMethod(methodName, argClass);
logger.info("===smy===method"+method);
method.setAccessible(true);
EnableRequestRecord annotation = method.getAnnotation(EnableRequestRecord.class);
//默认开启
boolean enable = annotation.enable();
logger.info("===smy===enable"+enable);
//接口编码
String interfaceCode = annotation.interfaceCode();
logger.info("===smy===interfaceCode"+interfaceCode);
RequestRecord requestRecord = new RequestRecord();
//拿到传入的参数
String arg = (String)joinPoint.getArgs()[0];
//todo 业务逻辑
//继续执行
Object result = joinPoint.proceed();
return result.toString();
}
}
被拦截的方法,需要被拦截的方法上加注解EnableRequestRecord
@PostMapping("/rest/employee/receiver-data")
//注解实现
@EnableRequestRecord(interfaceCode = "EMPLOYEE_SYN",externalSystemCode = "E")
public String EmployeeData(@RequestBody String requestData) {
return "";
}
这样一个简单的注解拦截就实现了。
传统的拦截方法
- 拦截jar包内的方法
- 二开的方法建议使用注解可以传参数
一个实现类搞定
package org.a.gateway.annotation.aspect.itf;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
@Component
@Aspect
@Slf4j
public class EnableRequestRecordAspect {
@Pointcut("execution(* org.a.gateway.api.controller.v1.ApiProductController.saveOrUpdateSpu(..))")
public void saveOrUpdateSpu(){}
@Pointcut("execution(* org.a.gateway.api.controller.v1.ApiProductController.stateUpdate(..))")
public void stateUpdate(){}
@Pointcut("execution(* org.a.gateway.api.controller.v1.ApiProductController.priceUpdate(..))")
public void priceUpdate(){}
@Around("saveOrUpdateSpu() || stateUpdate() || priceUpdate()")
public String doAround(ProceedingJoinPoint joinPoint) throws Throwable {
return String.valueOf("");
}
}
这样就实现了一个传统的拦截。