2020.06.29 ssm项目整合练习中LogAop日志类
作者:互联网
package com.aojie.controller;
import com.aojie.domain.SysLog;
import com.aojie.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
/**
* @author aojie
* @Function
* @create 2020-06-29 21:27
*/
@Component
@Aspect
public class LogAop {
/* 需要先在web.xml文件中配置
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
*/
@Autowired
private HttpServletRequest request;
@Autowired
private SysLogService service;
//开始时间
private Date visitTime;
//访问的类
private Class clazz;
//访问的方法
private Method method;
//主要是获取开始时间和执行类以及执行方法
@Before("execution(* com.aojie.controller.*.*(..))")
public void doBefore(JoinPoint jp) throws NoSuchMethodException {
visitTime=new Date();//当前时间就是开始时间
//java.lang.Object getTarget() :获取连接点所在的目标对象
clazz=jp.getTarget().getClass();//具体要访问的类
//Signature getSignature() :获取连接点的方法签名对象;
String methodName=jp.getSignature().getName();//获取访问的方法名
Object[] args = jp.getArgs();//获取访问方法的参数
//获取具体执行方法的Method对象
if (args==null||args.length==0){
method=clazz.getMethod(methodName);//只能获取无参的方法
}else {
Class[] calssArgs=new Class[args.length];
for (int i = 0; i < args.length; i++) {
calssArgs[i]=args[i].getClass();
}
clazz.getMethod(methodName,calssArgs);
}
}
@After("execution(* com.aojie.controller.*.*(..))")
public void doAfter(JoinPoint jp) throws Exception {
long time=new Date().getTime()-visitTime.getTime();//获取访问时长
String url="";
//获取URL
if (clazz != null&&method!=null&&clazz!= LogAop.class) {
//获取类上的注解值
RequestMapping classAnnotation = (RequestMapping)clazz.getAnnotation(RequestMapping.class);
if (classAnnotation!=null){
String[] classValue = classAnnotation.value();
//获取方法上的注解值
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if (methodAnnotation!=null){
String[] methodValue = methodAnnotation.value();
url=classValue[0]+methodValue[0];
//获取访问的ip
String ip = request.getRemoteAddr();
//获取操作的用户
SecurityContext context= SecurityContextHolder.getContext();//从上下文中获取当前登录的用户
User user = (User) context.getAuthentication().getPrincipal();
String username = user.getUsername();
//将日志相关信息封装到syslog类中
SysLog sysLog = new SysLog();
sysLog.setExecutionTime(time);//执行时长
sysLog.setIp(ip);
sysLog.setMethod("类名:"+clazz.getName()+"方法名:"+method.getName());
sysLog.setUrl(url);
sysLog.setUsername(username);
sysLog.setVisitTime(visitTime);
//调用service
service.save(sysLog);
}
}
}
}
}
标签:sysLog,29,springframework,2020.06,ssm,获取,org,import,clazz 来源: https://www.cnblogs.com/aojie/p/13210889.html