package cn.com.rivercloud.log.aspect;
import cn.com.rivercloud.log.domain.SysLogs;
import cn.com.rivercloud.log.service.SysLogsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author shichangle
* date
*/
@Component
@Aspect
@Slf4j
public class LogAspect {
@Autowired
private SysLogsService sysLogsService;
private long currentTime = 0L;
/**
* 配置切入点
* 通过注解
*/
@Pointcut("@annotation(cn.com.rivercloud.log.aop.log.Log)")
public void logPointCut(){}
private final String pointCut= "execution(* cn.com....自己写))";
/**
* 配置切入点
* 通过目录
*/
@Pointcut(pointCut)
public void logPointCut(){}
// @Around("logPointCut()")
// public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
// Object result;
//
// currentTime = System.currentTimeMillis();
// result = joinPoint.proceed();
// SysLogs sysLogs = new SysLogs("Info",System.currentTimeMillis()-currentTime);
// //存储信息
// String username = getUsername();
// String ip = getIp();
//
// sysLogsService.save(username,ip,joinPoint,sysLogs);
// return result;
// }
@Before(value = "logPointCut()")
public void logBefore(JoinPoint joinPoint){
System.out.println("前置通知");
System.out.println(joinPoint);
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
System.out.println(arg);
}
}
@AfterReturning(value = "logPointCut()",returning = "keys")
public void logAfterReturning(JoinPoint joinPoint,Object keys){
Map<String,Object> map = JSON.parseObject(keys.toString());
String success = map.get("success").toString();
if(success.equals("true")){
currentTime = System.currentTimeMillis();
SysLogs sysLogs = new SysLogs("Info",System.currentTimeMillis()-currentTime);
//存储信息
String username = getUsername();
String ip = getIp();
sysLogsService.save(username,ip,(ProceedingJoinPoint)joinPoint,sysLogs);
}else{
SysLogs log = new SysLogs("ERROR",System.currentTimeMillis()-currentTime);
String username = getUsername();
String ip = getIp();
sysLogsService.save(username,ip,(ProceedingJoinPoint) joinPoint,log);
}
}
// /**
// * 配置异常通知
// * @param joinPoint
// * @param e
// */
// @AfterThrowing(pointcut = "logPointCut()",throwing = "e")
// public void logAfterThrowing(JoinPoint joinPoint,Throwable e){
// SysLogs log = new SysLogs("ERROR",System.currentTimeMillis()-currentTime);
// String username = getUsername();
// String ip = getIp();
// sysLogsService.save(username,ip,(ProceedingJoinPoint) joinPoint,log);
// }
private String getUsername() {
Object principal = SecurityUtils.getSubject().getPrincipal();
return principal.toString();
}
private String getIp(){
Subject subject = SecurityUtils.getSubject();
String host = subject.getSession().getHost();
return host;
}
}
package cn.com.rivercloud.log.aop.log;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author shichangle
* date
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
String value() default "";
}