1.自定义
package cc.mrbird.common.annotation; 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)
public @interface Log {
String value() default "";
}
2.实现类
package cc.mrbird.common.aspect; import cc.mrbird.common.config.FebsProperties;
import cc.mrbird.common.util.HttpContextUtils;
import cc.mrbird.common.util.IPUtils;
import cc.mrbird.system.domain.SysLog;
import cc.mrbird.system.domain.User;
import cc.mrbird.system.service.LogService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.shiro.SecurityUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; /**
* AOP 记录用户操作日志
*
* @author MrBird
* @link https://mrbird.cc/Spring-Boot-AOP%20log.html
*/
@Aspect
@Component
public class LogAspect { private Logger log = LoggerFactory.getLogger(this.getClass()); @Autowired
private FebsProperties febsProperties; @Autowired
private LogService logService; @Pointcut("@annotation(cc.mrbird.common.annotation.Log)")
public void pointcut() {
// do nothing
} @Around("pointcut()")
public Object around(ProceedingJoinPoint point) throws JsonProcessingException {
Object result = null;
long beginTime = System.currentTimeMillis();
try {
// 执行方法
result = point.proceed();
} catch (Throwable e) {
log.error(e.getMessage());
}
// 获取request
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
// 设置IP地址
String ip = IPUtils.getIpAddr(request);
// 执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
if (febsProperties.isOpenAopLog()) {
// 保存日志
User user = (User) SecurityUtils.getSubject().getPrincipal();
SysLog log = new SysLog();
log.setUsername(user.getUsername());
log.setIp(ip);
log.setTime(time);
logService.saveLog(point, log);
}
return result;
}
}
3.引入
@Log("获取部门信息")
@RequestMapping("dept")
@RequiresPermissions("dept:list")
public String index() {
return "system/dept/dept";
}