import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
@Aspect
@Component
public class LogAspect {
@Around("@annotation(com.公司名.ActionDescriptionAttribute)")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object res = null;
long time = System.currentTimeMillis();
try {
res = joinPoint.proceed();
time = System.currentTimeMillis() - time;
return res;
} finally {
try {
//方法执行完成后增加日志
addOperationLog(joinPoint,res,time);
}catch (Exception e){
System.out.println("LogAspect 操作失败:" + e.getMessage());
e.printStackTrace();
}
}
}
@Autowired
HttpServletRequest httpServletRequest;
@Autowired
AuditLogService auditLogService;
private void addOperationLog(JoinPoint joinPoint, Object res, long time){
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
OperationLog operationLog = new OperationLog();
Object[] args = joinPoint.getArgs();
String token = httpServletRequest.getHeader("Authorization");
String appid = httpServletRequest.getHeader("appid");
// ApplicationCertificateInputDTO inputString = (ApplicationCertificateInputDTO) args[2];
operationLog.accessId = appid;
operationLog.accessToken = token ;
operationLog.appSource = HttpUtil.getIpAddr(httpServletRequest);
//operationLog.setCertificateNumber(infoString.toString());
// operationLog.data = JSON.toJSONString(inputString.getInfo());
operationLog.appOprDatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" ).format(new Date());
operationLog.result = JSON.toJSONString(res);
operationLog.timeOfUse = time;
ActionDescriptionAttribute actionDescriptionAttribute =signature.getMethod().getAnnotation(ActionDescriptionAttribute.class);
if (actionDescriptionAttribute != null) {
operationLog.appInterfaceName = actionDescriptionAttribute.name();
String name = signature.getMethod().getName();
operationLog=saveLogByMethodName(joinPoint,operationLog,name);
}
//TODO 这里保存日志
System.out.println("记录日志:" + operationLog.toString());
// operationLogService.insert(operationLog);
auditLogService.auditLog(operationLog);
//System.out.println(signature.getMethod().getName());
}
private OperationLog saveLogByMethodName(JoinPoint joinPoint,OperationLog operationLog,String methodName){
Object[] args = joinPoint.getArgs();
switch (methodName){
case "getApplicatCertificate":
InfoString infoString=(InfoString)args[2];
operationLog.data=JSON.toJSONString(infoString.getInfo());
break;
case "ApplicatCertificate":
ApplicationCertificateInputDTO inputString = (ApplicationCertificateInputDTO) args[2];
operationLog.data = JSON.toJSONString(inputString.getInfo());
break;
case "GetCertificateList":
ShowInputDTO info=(ShowInputDTO)args[1];
operationLog.data = JSON.toJSONString(info.getInfo());
break;
case "getDctCertificateInfo":
ShowCertificationInputDTO param=(ShowCertificationInputDTO)args[1];
operationLog.data = JSON.toJSONString(param.getCertNum());
break;
case "getHosptCertificateInfo":
ShowCertificationInputDTO param1=(ShowCertificationInputDTO)args[1];
operationLog.data = JSON.toJSONString(param1.getCertNum());
break;
case "getNurseCertificateInfo":
ShowCertificationInputDTO param2=(ShowCertificationInputDTO)args[1];
operationLog.data = JSON.toJSONString(param2.getCertNum());
break;
case "getQrCode":
GetqrCodeInputDTO getqrCodeInputDTO=(GetqrCodeInputDTO)args[1];
operationLog.data = JSON.toJSONString(getqrCodeInputDTO);
break;
case "getInfoByqr":
InfoByqrInputDTO inputDTO=(InfoByqrInputDTO)args[0];
operationLog.data=JSON.toJSONString(inputDTO);
break;
case "whetherCouldApplication":
JudgCertificateInputDTO judgCertificateInputDTO=(JudgCertificateInputDTO)args[1];
operationLog.data=JSON.toJSONString(judgCertificateInputDTO);
break;
}
return operationLog;
}
}
import java.lang.annotation.*;
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ActionDescriptionAttribute {
String name();
}
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
public class HttpUtil {
/**
* @Description: 获取客户端IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if(ip.equals("127.0.0.1")){
//根据网卡取本机配置的IP
InetAddress inet=null;
try {
inet = InetAddress.getLocalHost();
} catch (Exception e) {
e.printStackTrace();
}
ip= inet.getHostAddress();
}
}
// 多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(ip != null && ip.length() > 15){
if(ip.indexOf(",")>0){
ip = ip.substring(0,ip.indexOf(","));
}
}
return ip;
}
}
aop java操作日志
最新推荐文章于 2023-01-18 02:41:14 发布