| | |
| | | package com.lf.server.aspect; |
| | | |
| | | import com.lf.server.entity.sys.OperateEntity; |
| | | import com.lf.server.entity.sys.UsersEntity; |
| | | import com.lf.server.helper.WebHelper; |
| | | import com.lf.server.service.sys.OperateService; |
| | | import com.lf.server.service.sys.TokenService; |
| | | import io.swagger.annotations.Api; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | 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 javax.servlet.http.HttpServletRequest; |
| | | import java.lang.reflect.Method; |
| | | |
| | | /** |
| | |
| | | @Aspect |
| | | @Component |
| | | public class LogAspect { |
| | | @Pointcut("") |
| | | public void logPointCut() { |
| | | @Autowired |
| | | private TokenService tokenService; |
| | | |
| | | @Autowired |
| | | private OperateService operateService; |
| | | |
| | | private final static String SELECT = "select"; |
| | | |
| | | private final static String INSERT = "insert"; |
| | | |
| | | private final static String UPDATE = "update"; |
| | | |
| | | private final static String DELETE = "delete"; |
| | | |
| | | private final static String UPLOAD = "upload"; |
| | | |
| | | private final static String DOWNLOAD = "download"; |
| | | |
| | | private static final Log log = LogFactory.getLog(LogAspect.class); |
| | | |
| | | @Pointcut("@annotation(com.lf.server.aspect.SysLog)") |
| | | public void logPointCut() { |
| | | // |
| | | } |
| | | |
| | | /** |
| | | * 环绕通知 @Around,也可以使用 @Before (前置通知) 或 @After (后置通知) |
| | | */ |
| | | @Around("logPointCut()") |
| | | public Object around(ProceedingJoinPoint point) throws Throwable { |
| | | long beginTime = System.currentTimeMillis(); |
| | | |
| | | // 执行方法 |
| | | Object result = point.proceed(); |
| | | |
| | | // 执行时长(毫秒) |
| | | long time = System.currentTimeMillis() - beginTime; |
| | | // System.out.println("time:"+time) |
| | | |
| | | // 保存日志 |
| | | saveLogAction(point, time); |
| | | saveLog(point, time); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | private void saveLogAction(ProceedingJoinPoint joinPoint, long time) { |
| | | MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| | | Method method = signature.getMethod(); |
| | | /*LogAction logAction = new LogAction(); |
| | | com.landtool.lanbase.common.annotation.LogAction log = method.getAnnotation(com.landtool.lanbase.common.annotation.LogAction.class); |
| | | if(log != null){ |
| | | // 注解上的描述 |
| | | String[] list = log.value().split(","); |
| | | logAction.setLargemodel(list[0]); //大模块 |
| | | logAction.setSmallmodel(list[1]);//小模块 |
| | | logAction.setRemark(list[2]); //备注 |
| | | logAction.setActiontype(list[3]);//操作类型 |
| | | /** |
| | | * 保存日志 |
| | | */ |
| | | private void saveLog(ProceedingJoinPoint joinPoint, long time) { |
| | | try { |
| | | MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| | | Class<?> clazz = joinPoint.getTarget().getClass(); |
| | | Method method = signature.getMethod(); |
| | | |
| | | //logAction.setRemark(log.value()); |
| | | //logAction.setActiontype(log.value()); |
| | | HttpServletRequest req = WebHelper.getRequest(); |
| | | String url = req.getServletPath(); |
| | | |
| | | OperateEntity oe = new OperateEntity(); |
| | | oe.setIp(WebHelper.getIpAddress(req)); |
| | | oe.setUrl(url); |
| | | oe.setExec(time); |
| | | |
| | | // 设置模块 |
| | | Api api = clazz.getAnnotation(Api.class); |
| | | if (api != null) { |
| | | oe.setModular(api.tags()[0]); |
| | | } |
| | | |
| | | // 设置类名\方法名、类别 |
| | | oe.setClazz(clazz.getName() + "." + method.getName()); |
| | | oe.setType(getType(method.getName())); |
| | | |
| | | // 设置备注 |
| | | SysLog sysLog = method.getAnnotation(SysLog.class); |
| | | if (sysLog != null) { |
| | | oe.setBak(sysLog.value()); |
| | | } |
| | | |
| | | UsersEntity ue = tokenService.getCurrentUser(req); |
| | | if (ue != null) { |
| | | oe.setUserid(ue.getId()); |
| | | } |
| | | |
| | | operateService.insertOperate(oe); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getStackTrace()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取操作类型 |
| | | */ |
| | | private static int getType(String methodName) { |
| | | if (methodName.indexOf(SELECT) > -1) { |
| | | return 1; |
| | | } |
| | | if (methodName.indexOf(INSERT) > -1) { |
| | | return 2; |
| | | } |
| | | if (methodName.indexOf(UPDATE) > -1) { |
| | | return 3; |
| | | } |
| | | if (methodName.indexOf(DELETE) > -1) { |
| | | return 4; |
| | | } |
| | | if (methodName.indexOf(UPLOAD) > -1) { |
| | | return 5; |
| | | } |
| | | if (methodName.indexOf(DOWNLOAD) > -1) { |
| | | return 6; |
| | | } |
| | | |
| | | //获取request |
| | | HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); |
| | | String url=request.getServletPath(); |
| | | logAction.setRequesturl(url); |
| | | //设置IP地址 |
| | | logAction.setRequestip(IPUtils.getIpAddr(request)); |
| | | |
| | | //用户名 |
| | | Long userid = ((OrgUser) SecurityUtils.getSubject().getPrincipal()).getUserid(); |
| | | logAction.setUserid(userid); |
| | | |
| | | logAction.setAppid(loginConfig.getAppId()); |
| | | |
| | | //logAction.setLargemodel(loginConfig.getAppFullName()); |
| | | |
| | | |
| | | |
| | | |
| | | //保存系统日志 |
| | | logActionService.save(logAction);*/ |
| | | return 0; |
| | | } |
| | | } |