package com.shlanbao.tzsc.utils.tools;
|
|
import java.lang.reflect.Method;
|
import java.sql.SQLException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
import com.shlanbao.tzsc.base.model.SessionInfo;
|
import com.shlanbao.tzsc.pms.sys.log.beans.LogBean;
|
import com.shlanbao.tzsc.pms.sys.log.service.LogServiceI;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.reflect.MethodSignature;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
/**
|
* @author: sunzhen
|
* @date: 2019/7/31
|
* @time: 18:18
|
* @description: AOP实现日志
|
*/
|
@Component
|
@Aspect
|
public class LogAopAspect {
|
|
@Autowired
|
private LogServiceI logServiceI;// 日志Service
|
@Autowired
|
private HttpServletRequest request;
|
/**
|
* 环绕通知记录日志通过注解匹配到需要增加日志功能的方法
|
*
|
* @param pjp
|
* @return
|
* @throws Throwable
|
*/
|
@Around("@annotation(com.shlanbao.tzsc.utils.tools.LogAnno)")
|
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
|
// 1.方法执行前的处理,相当于前置通知
|
// 获取方法签名
|
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
|
// 获取方法
|
Method method = methodSignature.getMethod();
|
/*// 获取方法参数名称数组
|
String[] parameterNames = methodSignature.getParameterNames();
|
// 获取方法参数值数组
|
Object[] args = pjp.getArgs();*/
|
// 获取方法上面的注解
|
LogAnno logAnno = method.getAnnotation(LogAnno.class);
|
// 获取操作描述的属性值
|
String operateType = logAnno.operateType();
|
// 创建一个日志对象(准备记录日志)
|
LogBean logBean=new LogBean();
|
logBean.setOptname(operateType);// 操作说明
|
|
// 从session中获取登录者信息
|
SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute("sessionInfo");//获取session中的user对象进而获取操作人名字
|
logBean.setName(sessionInfo.getUser().getName());// 设置操作人
|
// 获取IP
|
logBean.setIp(sessionInfo.getIp());
|
// 获取详情
|
Map map = new HashMap();
|
Enumeration paramNames = request.getParameterNames();
|
while (paramNames.hasMoreElements()) {
|
String paramName = (String) paramNames.nextElement();
|
String[] paramValues = request.getParameterValues(paramName);
|
if (paramValues.length == 1) {
|
String paramValue = paramValues[0];
|
if(paramValue.length()>500){
|
paramValue = cutStr(paramValue, 500);
|
}
|
if (paramValue.length() != 0) {
|
map.put(paramName, paramValue);
|
}
|
}
|
}
|
logBean.setParams("方法名:"+method.getName()+" 参数列表:"+map.toString());
|
|
// 配置项目名
|
logBean.setSys("PMS");
|
// 配置DEL(默认为0)
|
logBean.setDel(0L);
|
|
/*// 获取方法参数名称数组
|
String[] parameterNames = methodSignature.getParameterNames();
|
Object[] args = pjp.getArgs();
|
int index = ArrayUtil.indexOf(parameterNames, "id");
|
if(index!=-1){
|
// 获取详情
|
logBean.setParams("方法名:"+method.getName()+" 参数id:"+ args[index]);
|
}else{
|
// 获取详情
|
logBean.setParams("方法名:"+method.getName()+" 参数名:"+ Arrays.toString(parameterNames));
|
}*/
|
|
Object result = null;
|
try {
|
//让代理方法执行
|
result = pjp.proceed();
|
// 2.相当于后置通知(方法成功执行之后走这里)
|
logBean.setSuccess("正常");// 设置操作结果
|
} catch (Exception e) {
|
// 3.相当于异常通知部分
|
logBean.setSuccess("失败");// 设置操作结果
|
throw e;
|
} finally {
|
// 4.相当于最终通知
|
/*String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
logBean.setDate(format);// 设置操作日期*/
|
logServiceI.saveLog(logBean);// 添加日志记录
|
}
|
return result;
|
}
|
|
/**
|
* 返回截取指定长度字节数后的字符串,多余部分用“...”代替
|
*/
|
public static String cutStr(String strs, int length) {
|
int sum = 0;
|
String finalStr = "";
|
if (null == strs || strs.getBytes().length <= length) {
|
finalStr = (strs==null?"":strs);
|
} else {
|
for (int i = 0; i < strs.length(); i++) {
|
String str = strs.substring(i, i + 1);
|
// 累加单个字符字节数
|
sum += str.getBytes().length;
|
if (sum > length) {
|
finalStr = strs.substring(0, i) + "...";
|
break;
|
}
|
}
|
}
|
return finalStr;
|
}
|
|
/**
|
* 获取IP地址的方法
|
* @param request 传一个request对象下来
|
* @return
|
*/
|
public static String getIpAddress(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.getHeader("HTTP_CLIENT_IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
return ip;
|
}
|
}
|