【Java】spring boot 通过aop 记录操作日志 操作数据内容 解决requestbody无法获取数据
上码
1. 引入aop依赖
<dependency><groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2. 注解类
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 "";//功能描述
}
3切面方法
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* 切面处理类,操作日志处理
*/
@Aspect
@Component
public class OperLogAspect {
@Autowired
private TSysLogService logService;
private Logger logger = LoggerFactory.getLogger(getClass());
private String params ="";
@Pointcut("@annotation(org.student.aspect.OperLog)")
public void operLogPoinCut() {
}
@Before("@annotation(org.student.aspect.OperLog)")
public void beforeController(JoinPoint joinPoint){
Object[] o = joinPoint.getArgs();
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
Map<String, String> rtnMap = converMap(request.getParameterMap());
//通过判断contenttype 区分数据类型
if(request.getContentType().equals("application/json")){
params = JSONObject.toJSONString(o[0]);
}else {
params = JSON.toJSONString(rtnMap);
}
request.setAttribute("body",o[0]);
}
@AfterReturning(value = "operLogPoinCut()", returning = "keys")
public void saveOperLog(JoinPoint joinPoint, Object keys) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = (HttpServletRequest) requestAttributes
.resolveReference(RequestAttributes.REFERENCE_REQUEST);
//TUser loginUser = userService.getLoginUser(request); //获取用户信息
TSysLog operlog = new TSysLog();
try {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
OperLog opLog = method.getAnnotation(OperLog.class);
if (opLog != null) {
String operModul = opLog.operModul();
String operType = opLog.operType();
String operDesc = opLog.operDesc();
operlog.setOperMod(operModul); // 模块
operlog.setOperType(operType); // 类型
operlog.setOperDesc(operDesc); // 描述
}
String className = joinPoint.getTarget().getClass().getName();
String methodName = method.getName();
methodName = className + "." + methodName;
operlog.setOperMethod(methodName);//请求方法名
operlog.setOperRequParam(params);//请求数据
operlog.setOperRespParam(JSON.toJSONString(keys));//响应数据
//operlog.setUserId(loginUser.getId());
//operlog.setAccount(loginUser.getAccount());
//operlog.setDepartId(loginUser.getDepartId());
operlog.setOperIp(getRemoteAddr(request));//请求ip
operlog.setOperUri(request.getRequestURI());
logService.insertLog(operlog);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 转换request 请求参数
*
*/ public Map<String, String> converMap(Map<String, String[]> paramMap) {
Map<String, String> rtnMap = new HashMap<String, String>();
for (String key : paramMap.keySet()) {
rtnMap.put(key, paramMap.get(key)[0]);
}
return rtnMap;
}
public static String getRemoteAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
4.使用案例
@RequestMapping(value = "/saveInfo", method = RequestMethod.POST)@OperLog(operDesc = "功能描述", operType ="操作类型", operModul = "操作模块名称")
public ResponseResult saveInfo(@RequestBody TStudent student) {
ResponseResult<Integer> result = studentService.saveInfo(student);
return result;
}
5.表结构及类
import lombok.Data;import java.util.Date;
@Data
public class TSysLog {
private Long id;
private String operMod;
private String operType;
private String operDesc;
private String operRequParam;
private String operRespParam;
private String account;
private Long userId;
private Long departId;
private String operUri;
private String operIp;
private Date createTime;
private String operVar;
private String operExtra;
private String operMethod;
}
以上是 【Java】spring boot 通过aop 记录操作日志 操作数据内容 解决requestbody无法获取数据 的全部内容, 来源链接: utcz.com/a/91453.html