AOP实现API调用简单验证、日志记录、异常处理
1.添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.创建切面类
@Aspect
@Component
public class SjztDynamicApiAspect {
@Resource
private SjztDynamicApiLogService sjztDynamicApiLogService;
@Resource
private SjztDynamicApiUserService sjztDynamicApiUserService;
@Resource
private SjztDynamicApiWhitelistService sjztDynamicApiWhitelistService;
}
3.实现简单API调用认证
@Pointcut("execution(* com.yeyoo.sjzt.platform.controller.rest.SjztUserServiceRestController.exec(..))")
public void apiUserFilter() {
}
@Before(value = "apiUserFilter()")
public void userFilter(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String ak = request.getParameter("ak");
String sn = request.getParameter("sn");
if(ak==null||ak.equals("")||sn==null||sn.equals("")){
throw new DynamicApiException(ResultCode.BAD_REQUEST,"缺少请求参数");
}
String userName = "";
String sk = "";
SjztDynamicApiUser user = sjztDynamicApiUserService.selectApiUser(ak);
if(user==null){
throw new DynamicApiException(ResultCode.UNAUTHORIZED,"用户未授权,请联系服务平台管理员");
}else {
userName = user.getUserName();
sk = user.getSeckey();
}
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
com.yeyoo.dynamic.api.beans.DynamicApiExecRequest reqMap = (DynamicApiExecRequest) getParameter(method, joinPoint.getArgs());
String serviceApi = reqMap.getServiceApi();
SjztDynamicApiWhitelist whitelist = sjztDynamicApiWhitelistService.selectApiWhitelist(serviceApi,userName);
if(whitelist==null){
throw new DynamicApiException(ResultCode.FORBIDDEN,"接口未授权,请联系服务平台管理员");
}
String str = ********;
String localSn = ************;
if(!localSn.equals(sn)){
throw new DynamicApiException(ResultCode.VALIDATE_FAILED,"sn校验失败");
}
}
4.调用日志记录
@Pointcut("execution(* com.yeyoo.sjzt.platform.controller.rest.SjztUserServiceRestController.exec(..))")
public void apiLog() {
}
@AfterReturning(value = "apiLog()", returning = "responseBody")
public void saveApiLog(JoinPoint joinPoint, Object responseBody) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
SjztDynamicApiLog sjztDynamicApiLog = new SjztDynamicApiLog();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
com.yeyoo.sjzt.beans.ResultData resMap = (ResultData) responseBody;
Integer code = resMap.getCode();
com.yeyoo.dynamic.api.beans.DynamicApiExecRequest reqMap = (DynamicApiExecRequest) getParameter(method, joinPoint.getArgs());
String apiName = reqMap.getServiceApi();
sjztDynamicApiLog.setCode(code);
sjztDynamicApiLog.setApiName(apiName);
sjztDynamicApiLog.setMethod(request.getMethod());
sjztDynamicApiLog.setRequestBody(JSON.toJSONString(getParameter(method, joinPoint.getArgs())));
sjztDynamicApiLog.setResponseBody(JSON.toJSONString(responseBody));
sjztDynamicApiLogService.addApiLog(sjztDynamicApiLog);
}
private Object getParameter(Method method, Object[] args) {
List<Object> argList = new ArrayList<>();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
if(requestBody != null){
argList.add(args[i]);
}
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
if (requestParam != null) {
Map<String, Object> map = new HashMap<>();
String key = parameters[i].getName();
if (!StringUtils.isEmpty(requestParam.value())) {
key = requestParam.value();
}
map.put(key, args[i]);
argList.add(map);
}
}
if (argList.size() == 0) {
return null;
} else if (argList.size() == 1) {
return argList.get(0);
} else {
return argList;
}
}
5.异常日志记录
@AfterThrowing(value = "apiLog()", throwing = "exception")
public void saveExceptionLog(JoinPoint joinPoint, DynamicApiException exception) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
SjztDynamicApiLog sjztDynamicApiLog = new SjztDynamicApiLog();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
Integer code = exception.getErrorCode().getCode();
com.yeyoo.dynamic.api.beans.DynamicApiExecRequest reqMap = (DynamicApiExecRequest) getParameter(method, joinPoint.getArgs());
String apiName = reqMap.getServiceApi();
sjztDynamicApiLog.setCode(code);
sjztDynamicApiLog.setApiName(apiName);
sjztDynamicApiLog.setMethod(request.getMethod());
sjztDynamicApiLog.setRequestBody(JSON.toJSONString(getParameter(method, joinPoint.getArgs())));
sjztDynamicApiLog.setResponseBody(JSON.toJSONString(exception.getMessage()));
sjztDynamicApiLogService.addApiLog(sjztDynamicApiLog);
}