OperLog.java
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperLog {
String operModul() default "";
String operType() default "";
String operDesc() default "";
}
OperLogAspect.java
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import xxx.xxx.xxx.commons.OprLogConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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 org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.UUID;
@Aspect
@Component
@Slf4j
public class OperLogAspect {
@Autowired
LogService logService;
@Autowired
UserService userService;
@Pointcut("@annotation(com.xxx.xxx.OperLog)")
public void operLogPoinCut() {
}
@Pointcut("execution(* com.xxx.xxx.controller..*.*(..))")
public void operExceptionLogPoinCut() {
}
@AfterReturning(value = "operLogPoinCut()", returning = "keys")
public void saveOperLog(JoinPoint joinPoint, Object keys) {
getLog(joinPoint, null);
}
@AfterThrowing(pointcut = "operExceptionLogPoinCut()", throwing = "e")
public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
getLog(joinPoint, e);
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void getLog(JoinPoint joinPoint, Throwable e) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
Object[] args = joinPoint.getArgs();
Class<?> clazz = joinPoint.getTarget().getClass();
Signature signature = joinPoint.getSignature();
MethodSignature ms = (MethodSignature) signature;
Method targetMethod = null;
try {
targetMethod = clazz.getDeclaredMethod(ms.getName(), ms.getParameterTypes());
} catch (Exception exception) {
exception.printStackTrace();
}
OperLog olog = targetMethod.getAnnotation(OperLog.class);
if(olog == null) {
return;
}
String IP = IpUtils.getIpAddr(request);
LogPojo log = new LogPojo();
try {
String userId = JwtUtil.getUserId(request.getHeader("token"));
if(!StringUtils.isEmpty(userId)) {
QueryWrapper<User> qw = new QueryWrapper<>();
qw.select("user_name", "name").eq("user_name", userId);
User user = userService.getOne(qw);
log.setName(user.getName());
log.setUserName(user.getUsername());
}
} catch (Exception exception) {
System.err.println("没有获取到用户信息!");
}
log.setId(UUID.randomUUID().toString());
log.setModel(olog.operModul());
log.setType(olog.operType());
log.setDescibes(olog.operDesc());
log.setIp(IP);
log.setAddress(AddressUtils.getRealAddressByIP(IP));
log.setUrl(request.getServletPath());
log.setParams(JSONObject.toJSONString(args));
if(e != null) {
log.setError(e.getMessage());
log.setStatus(OprLogConst.fail);
} else {
log.setStatus(OprLogConst.OK);
}
logService.save(log);
}
}
OprLogConst.java
public class OprLogConst {
public static final String ADD = "插入";
public static final String UPDATE = "更新";
public static final String SELECT = "查询";
public static final String DELETE = "删除";
public static final String EXPORT = "导出";
public static final String IMPORT = "导入";
public static final Integer OK = 200;
public static final Integer fail = 500;
}
示例
@OperLog(operModul = "字典管理", operType = OprLogConst.ADD, operDesc = "保存/修改类别字典信息")
@RequestMapping("/saveParams")
@ResponseBody
public RespBean saveParams(Params params) {
return null;
}