添加日志记录

This commit is contained in:
韩鹏辉
2023-09-08 15:50:26 +08:00
parent df24fc2c81
commit ff2f01100f
7 changed files with 175 additions and 61 deletions

View File

@@ -91,6 +91,11 @@
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
<version>1.2</version> <version>1.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies> </dependencies>

View File

@@ -0,0 +1,32 @@
package com.chaozhanggui.admin.system.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author DJH
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OpLog {
/**
* 操作日志code
* @return 操作code
*/
String opCode() default "";
/**
* 操作日志-详情
* @return 操作详情
*/
String opDetail() default "";
/**
* 操作日志名称
* @return 操作名称
*/
String opName() default "";
}

View File

@@ -1,6 +1,7 @@
package com.chaozhanggui.admin.system.controller; package com.chaozhanggui.admin.system.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.admin.system.annotation.OpLog;
import com.chaozhanggui.admin.system.model.CreateAgencyReq; import com.chaozhanggui.admin.system.model.CreateAgencyReq;
import com.chaozhanggui.admin.system.service.AgencyService; import com.chaozhanggui.admin.system.service.AgencyService;
import com.chaozhanggui.admin.system.util.MD5Util; import com.chaozhanggui.admin.system.util.MD5Util;
@@ -42,6 +43,7 @@ public class AgencyController {
* @param agencyReq 机构信息 * @param agencyReq 机构信息
* @return * @return
*/ */
@OpLog(opName = "创建机构", opDetail = "创建机构", opCode = "AGENCY_ADD")
@RequestMapping("addAgency") @RequestMapping("addAgency")
public RespBody addAgency(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId, public RespBody addAgency(@RequestHeader("loginName") String loginName, @RequestHeader("token") String token, @RequestHeader("userId") String userId,
@RequestBody @Valid CreateAgencyReq agencyReq @RequestBody @Valid CreateAgencyReq agencyReq

View File

@@ -3,6 +3,7 @@ package com.chaozhanggui.admin.system.interceptor;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.admin.system.annotation.OpLog;
import com.chaozhanggui.admin.system.util.RedisCst; import com.chaozhanggui.admin.system.util.RedisCst;
import com.chaozhanggui.admin.system.util.RedisUtil; import com.chaozhanggui.admin.system.util.RedisUtil;
import com.chaozhanggui.common.system.config.RespBody; import com.chaozhanggui.common.system.config.RespBody;
@@ -12,12 +13,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Method;
public class LoginInterceptor implements HandlerInterceptor { public class LoginInterceptor implements HandlerInterceptor {
@@ -35,7 +38,6 @@ public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 获取进过拦截器的路径 // 获取进过拦截器的路径
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
String loginName=request.getHeader("loginName"); String loginName=request.getHeader("loginName");
String token=request.getHeader("token"); String token=request.getHeader("token");
String userId=request.getHeader("userId"); String userId=request.getHeader("userId");
@@ -61,6 +63,7 @@ public class LoginInterceptor implements HandlerInterceptor {
outputStream.write(JSONUtil.toJsonStr(new RespBody("999999")).getBytes("UTF-8")); outputStream.write(JSONUtil.toJsonStr(new RespBody("999999")).getBytes("UTF-8"));
return false; return false;
} }
return true; return true;
} }

View File

@@ -0,0 +1,106 @@
package com.chaozhanggui.admin.system.interceptor;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.admin.system.annotation.OpLog;
import com.chaozhanggui.common.system.config.RespBody;
import com.chaozhanggui.common.system.util.IPUtil;
import com.chaozhanggui.dao.system.dao.TbPlussAdminRecodeMapper;
import com.chaozhanggui.dao.system.entity.TbPlussAdminRecodeWithBLOBs;
import com.chaozhanggui.dao.system.entity.TbPlussOperationRecord;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
@Slf4j
@Aspect
@Component
public class OpLogAspect {
@Autowired
TbPlussAdminRecodeMapper tbPlussAdminRecodeMapper;
@Pointcut("execution(* com.chaozhanggui.admin.system.controller..*.*(..)) && @annotation(com.chaozhanggui.admin.system.annotation.OpLog))")
public void executionService() {
}
@AfterReturning(returning="rvt", value = "executionService()")
public void after(JoinPoint point, Object rvt) {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request;
if (attributes != null) {
request = ((ServletRequestAttributes) attributes).getRequest();
} else {
log.warn("未获取到request");
return;
}
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
OpLog annotation = method.getAnnotation(OpLog.class);
String loginName=request.getHeader("loginName");
String token=request.getHeader("token");
String userId=request.getHeader("userId");
if(ObjectUtil.isEmpty(loginName)||ObjectUtil.isEmpty(token)||ObjectUtil.isEmpty(userId)){
log.error("token 信息不存在");
return;
}
String body=processInputArg(point.getArgs());
TbPlussAdminRecodeWithBLOBs tbPlussAdminRecodeWithBLOBs=new TbPlussAdminRecodeWithBLOBs();
tbPlussAdminRecodeWithBLOBs.setUserId(Integer.valueOf(userId));
tbPlussAdminRecodeWithBLOBs.setOperationCode(annotation.opCode());
tbPlussAdminRecodeWithBLOBs.setRemark(annotation.opDetail());
tbPlussAdminRecodeWithBLOBs.setIp(IPUtil.getIpAddress(request));
tbPlussAdminRecodeWithBLOBs.setReqBody(body);
if (rvt instanceof RespBody) {
try {
JSONObject jsonObject = (JSONObject) JSON.toJSON(rvt);
tbPlussAdminRecodeWithBLOBs.setResponseBody("返回结果:"+jsonObject.toString());
} catch (Exception e) {
}
}
tbPlussAdminRecodeWithBLOBs.setCreateTime(new Date());
tbPlussAdminRecodeMapper.insert(tbPlussAdminRecodeWithBLOBs);
}
private String processInputArg(Object[] args) {
StringBuffer sb=new StringBuffer();
sb.append("请求参数:");
if(ObjectUtil.isNotEmpty(args)||args.length>0){
Arrays.stream(args).forEach(it->{
if(it instanceof String||it instanceof Integer||it instanceof Long){
sb.append(it);
sb.append("&");
}else if(it instanceof Object){
sb.append(JSONUtil.toJsonStr(it));
sb.append("&");
}
});
}
return sb.substring(0,sb.length()-1).toString();
}
}

View File

@@ -1,8 +1,13 @@
package com.chaozhanggui.dao.system.entity; package com.chaozhanggui.dao.system.entity;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@Data
public class TbPlussOperationRecord implements Serializable { public class TbPlussOperationRecord implements Serializable {
private Integer id; private Integer id;
@@ -16,73 +21,34 @@ public class TbPlussOperationRecord implements Serializable {
private Date createtime; private Date createtime;
private String ip;
private String extension; private String extension;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOperator() { @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER
return operator; , value = "ip")
} private String ip;
public void setOperator(String operator) { @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER
this.operator = operator == null ? null : operator.trim(); , value = "extension ->> '$.isp'")
} private String isp;
public String getDictvalue() { @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER
return dictvalue; , value = "extension ->> '$.city'")
} private String city;
public void setDictvalue(String dictvalue) { @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER
this.dictvalue = dictvalue == null ? null : dictvalue.trim(); , value = "extension ->> '$.province'")
} private String province;
public Integer getUserid() { @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER
return userid; , value = "extension ->> '$.country'")
} private String country;
public void setUserid(Integer userid) { @TableField(insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER
this.userid = userid; , value = "extension ->> '$.county'")
} private String county;
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark == null ? null : remark.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip == null ? null : ip.trim();
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension == null ? null : extension.trim();
}
} }

View File

@@ -20,8 +20,8 @@
<!-- 数据库链接URL、用户名、密码 --> <!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection connectionURL="jdbc:mysql://60.205.224.68:3306/chaozhanggui?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8" <jdbcConnection connectionURL="jdbc:mysql://101.37.12.135:3306/ysk_test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"
driverClass="com.mysql.cj.jdbc.Driver" password="CZGmysqlroot@123" userId="chaozhanggui"> driverClass="com.mysql.cj.jdbc.Driver" password="CZGmysqlroot@123" userId="ysk_test">
<property name="nullCatalogMeansCurrent" value="true"/> <property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection> </jdbcConnection>
@@ -53,7 +53,7 @@
<!-- 要生成的表tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <!-- 要生成的表tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!-- <table tableName="%" schema="mining" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" ></table>--> <!-- <table tableName="%" schema="mining" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" ></table>-->
<table tableName="tb_pluss_user_maker" domainObjectName="TbPlussUserMaker" <table tableName="tb_pluss_admin_recode" domainObjectName="TbPlussAdminRecode"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" > enableSelectByExample="false" selectByExampleQueryId="false" >
</table> </table>