前提引入aop依赖完成(具体不介绍)
实操
创建自定义接口类 Loggable
import java.lang.annotation.*;
/**
* @Author hw
* @Date 2024/4/7 9:22
* @PackageName:xxxxxxxxx
* @ClassName: Loggable
* @Description: 登录日志
* @Version 1.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface Loggable {
/**
* 说明
*/
String operatorDetails() default "";
/**
* 操作
*/
String operation();
/**
* 操作位置
*/
String operationAddress();
/**
* 操作模块
*/
String operatorModule();
}
定义切面类
/**
* @Author hw
* @Date 2024/3/1 9:28
* @PackageName:xxxxxxxxx
* @ClassName: LoggableAspect
* @Description: 登录模块的aop
* @Version 1.0
*/
@Aspect
@Slf4j
@Component
public class LoggableAspect {
@Autowired
private EipSysLogLoginServiceImpl eipSysLogLoginService;
@Pointcut("@annotation(com.redxun.eip.annotation.Loggable)")
public void loggableAspect() {
}
@AfterReturning("execution(* *.*(..)) && @annotation(loggable)")
public void logAfterReturningMethod(JoinPoint joinPoint, Loggable loggable) throws Exception{
log.info("==========执行业务操作日志留痕===============");
EipSysLogLogin eipSysLogLogin = new EipSysLogLogin();
// 操作 操作位置 操作内容 操作模块
eipSysLogLogin.setOperator(loggable.operation());
eipSysLogLogin.setOperatorAddress(loggable.operationAddress());
eipSysLogLogin.setOperatorDetails(loggable.operatorDetails());
eipSysLogLogin.setOperatorModule(loggable.operatorModule());
eipSysLogLoginService.saveLoginLog(joinPoint,eipSysLogLogin);
}
}
Service层写具体的代码
/**
* description:
* 保存登录日志操作
* @author: hw
* @since: 1.0.0
* @date: 2024/4/7 10:35
* @Param joinPoint:
* @Param eipSysLogLogin:
* @return: void
*/
public void saveLoginLog(JoinPoint joinPoint, EipSysLogLogin eipSysLogLogin) {
EipEhrEmp eipEhrEmp = (EipEhrEmp) redisTemplate.opsForHash().get(EipConstant.USER_PERMISSION, SecurityGetUserUtils.getUserByWorkId());
if (ObjectUtil.isNotEmpty(eipEhrEmp)) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 主键
eipSysLogLogin.setId(IdGenerator.getIdStr());
// 记录时间
eipSysLogLogin.setRecordTime(new Date());
// 操作人
eipSysLogLogin.setOperatorWorkId(eipEhrEmp.getWorkId());
eipSysLogLogin.setOperatorName(eipEhrEmp.getName());
// 创建时间、更新时间
eipSysLogLogin.setCreateTime(new Date());
eipSysLogLogin.setUpdateTime(new Date());
// 创建人 更新人
eipSysLogLogin.setCreateBy(eipEhrEmp.getWorkId());
eipSysLogLogin.setUpdateBy(eipEhrEmp.getWorkId());
eipSysLogLogin.setCreateByName(eipEhrEmp.getName());
eipSysLogLogin.setUpdateByName(eipEhrEmp.getName());
// 逻辑删除(1-有效 0-删除)
eipSysLogLogin.setDelete(1);
// ip地址
String ipAddr = IpUtil.getIpAddr(request);
eipSysLogLogin.setIp(ipAddr);
// MAC 地址
eipSysLogLogin.setMac(MacAddressUtils.getMacAddressByIp(ipAddr));
// 主机名
eipSysLogLogin.setHostname(request.getHeader("Host"));
// 登录地区 需要具体的api
eipSysLogLogin.setLoginAddress(request.getLocale().toString());
// 浏览器
eipSysLogLogin.setBrowser(BrowserUtils.getBrowser(request));
// 操作系统
eipSysLogLogin.setOperatorSystem(OperatingSystemUtils.getOperatingSystem(request));
// 创建部门,公司
eipSysLogLogin.setCreateDepId(eipEhrEmp.getDeptId());
eipSysLogLogin.setCompanyId(eipEhrEmp.getCompanyId());
// 获取当年的年份
LocalDate currentDate = LocalDate.now();
int currentYear = currentDate.getYear();
eipSysLogLoginMapper.saveEipSysLogLogin(eipSysLogLogin,currentYear);
}
}