登录日志的记录

This commit is contained in:
none 2023-02-17 15:45:45 +08:00
parent 635783dd49
commit b44ced67a0
8 changed files with 226 additions and 5 deletions

View File

@ -0,0 +1,13 @@
package xyz.playedu.api.constant;
/**
* @Author 杭州白书科技有限公司
* @create 2023/2/17 15:42
*/
public class BackendLogConstant {
public final static String OPT_LOGIN = "LOGIN";
public final static String MODULE_LOGIN = "LOGIN";
}

View File

@ -0,0 +1,117 @@
package xyz.playedu.api.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
*
* @TableName admin_logs
*/
@TableName(value ="admin_logs")
@Data
public class AdminLog implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 管理员ID
*/
private Integer adminId;
/**
* 模块
*/
private String module;
/**
* 操作指令
*/
private String opt;
/**
* 备注
*/
private String remark;
/**
* ip
*/
private String ip;
/**
* 地址
*/
private String ipArea;
/**
*
*/
private Date createdAt;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
AdminLog other = (AdminLog) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getAdminId() == null ? other.getAdminId() == null : this.getAdminId().equals(other.getAdminId()))
&& (this.getModule() == null ? other.getModule() == null : this.getModule().equals(other.getModule()))
&& (this.getOpt() == null ? other.getOpt() == null : this.getOpt().equals(other.getOpt()))
&& (this.getRemark() == null ? other.getRemark() == null : this.getRemark().equals(other.getRemark()))
&& (this.getIp() == null ? other.getIp() == null : this.getIp().equals(other.getIp()))
&& (this.getIpArea() == null ? other.getIpArea() == null : this.getIpArea().equals(other.getIpArea()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getAdminId() == null) ? 0 : getAdminId().hashCode());
result = prime * result + ((getModule() == null) ? 0 : getModule().hashCode());
result = prime * result + ((getOpt() == null) ? 0 : getOpt().hashCode());
result = prime * result + ((getRemark() == null) ? 0 : getRemark().hashCode());
result = prime * result + ((getIp() == null) ? 0 : getIp().hashCode());
result = prime * result + ((getIpArea() == null) ? 0 : getIpArea().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", adminId=").append(adminId);
sb.append(", module=").append(module);
sb.append(", opt=").append(opt);
sb.append(", remark=").append(remark);
sb.append(", ip=").append(ip);
sb.append(", ipArea=").append(ipArea);
sb.append(", createdAt=").append(createdAt);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}

View File

@ -5,8 +5,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import xyz.playedu.api.constant.BackendLogConstant;
import xyz.playedu.api.domain.AdminLog;
import xyz.playedu.api.domain.AdminUser;
import xyz.playedu.api.event.AdminUserLoginEvent;
import xyz.playedu.api.service.AdminLogService;
import xyz.playedu.api.service.AdminUserService;
import xyz.playedu.api.util.IpUtil;
@ -17,6 +20,9 @@ public class AdminUserLoginListener {
@Autowired
private AdminUserService adminUserService;
@Autowired
private AdminLogService adminLogService;
@Order(1)
@EventListener
public void updateLoginInfo(AdminUserLoginEvent event) {
@ -32,10 +38,17 @@ public class AdminUserLoginListener {
@Order(10)
@EventListener
public void recordLoginIp(AdminUserLoginEvent event) {
public void log(AdminUserLoginEvent event) {
String area = IpUtil.getRealAddressByIP(event.getIp());
log.info("地区:" + area);
AdminLog adminLog = new AdminLog();
adminLog.setAdminId(event.getAdminId());
adminLog.setModule(BackendLogConstant.MODULE_LOGIN);
adminLog.setOpt(BackendLogConstant.OPT_LOGIN);
adminLog.setIp(event.getIp());
adminLog.setIpArea(area);
adminLog.setCreatedAt(event.getLoginAt());
adminLogService.save(adminLog);
}
}

View File

@ -0,0 +1,20 @@
package xyz.playedu.api.mapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.AdminLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author tengteng
* @description 针对表admin_logs的数据库操作Mapper
* @createDate 2023-02-17 15:40:31
* @Entity xyz.playedu.api.domain.AdminLog
*/
@Mapper
public interface AdminLogMapper extends BaseMapper<AdminLog> {
}

View File

@ -0,0 +1,15 @@
package xyz.playedu.api.service;
import org.springframework.stereotype.Service;
import xyz.playedu.api.domain.AdminLog;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author tengteng
* @description 针对表admin_logs的数据库操作Service
* @createDate 2023-02-17 15:40:31
*/
@Service
public interface AdminLogService extends IService<AdminLog> {
}

View File

@ -0,0 +1,22 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.playedu.api.domain.AdminLog;
import xyz.playedu.api.service.AdminLogService;
import xyz.playedu.api.mapper.AdminLogMapper;
import org.springframework.stereotype.Service;
/**
* @author tengteng
* @description 针对表admin_logs的数据库操作Service实现
* @createDate 2023-02-17 15:40:31
*/
@Service
public class AdminLogServiceImpl extends ServiceImpl<AdminLogMapper, AdminLog>
implements AdminLogService{
}

View File

@ -11,8 +11,6 @@ import xyz.playedu.api.mapper.AdminUserMapper;
import org.springframework.stereotype.Service;
import xyz.playedu.api.types.PaginationResult;
import java.util.Date;
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.playedu.api.mapper.AdminLogMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.AdminLog">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="adminId" column="admin_id" jdbcType="INTEGER"/>
<result property="module" column="module" jdbcType="VARCHAR"/>
<result property="opt" column="opt" jdbcType="VARCHAR"/>
<result property="remark" column="remark" jdbcType="VARCHAR"/>
<result property="ip" column="ip" jdbcType="VARCHAR"/>
<result property="ipArea" column="ip_area" jdbcType="VARCHAR"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,admin_id,module,
opt,remark,ip,
ip_area,created_at
</sql>
</mapper>