|
@@ -0,0 +1,96 @@
|
|
|
+package com.xxh.cloud.basic.framework.modules.aop.aspect;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
+import com.baomidou.mybatisplus.annotation.TableId;
|
|
|
+import com.xxh.cloud.basic.framework.modules.log.entity.dos.LogOperation;
|
|
|
+import com.xxh.cloud.basic.framework.modules.log.service.LogOperationService;
|
|
|
+import com.xxh.cloud.framework.common.user.AuthContextUtils;
|
|
|
+import com.xxh.cloud.framework.common.user.LoginUser;
|
|
|
+import com.xxh.cloud.framework.common.utils.IpUtils;
|
|
|
+import io.swagger.annotations.ApiModelProperty;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+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.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+/**
|
|
|
+ * API安全AOP
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+@Primary
|
|
|
+@Component
|
|
|
+public class OperationLogAspect {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private LogOperationService logOperationService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * APIWrap pointcut
|
|
|
+ */
|
|
|
+ @Pointcut("@within(com.xxh.cloud.basic.framework.modules.aop.annotation.OperationLog) || @annotation(com.xxh.cloud.basic.framework.modules.aop.annotation.OperationLog)")
|
|
|
+ public void apiWrapPointcut() {
|
|
|
+ // Do nothing because of pointcut
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AOP 之前插入
|
|
|
+ *
|
|
|
+ * @param joinPoint {@link JoinPoint}
|
|
|
+ */
|
|
|
+ @Before("apiWrapPointcut()")
|
|
|
+ public void before(JoinPoint joinPoint) {
|
|
|
+ MethodSignature sign = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = sign.getMethod();
|
|
|
+ System.out.println("----------AOP 之前插入-----------");
|
|
|
+
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ for (Object obj : args) {
|
|
|
+ LoginUser loginUser= AuthContextUtils.getCurrentLoginUser();
|
|
|
+ // 操作日志entity
|
|
|
+ LogOperation entity = new LogOperation();
|
|
|
+ entity.setId(IdUtil.getSnowflake(3,3).nextIdStr());
|
|
|
+ entity.setOperation(method.getName());
|
|
|
+ entity.setRequestParams(JSONObject.toJSONString(obj));
|
|
|
+ entity.setOpIp(IpUtils.getIpAddr());
|
|
|
+ entity.setStatus("1");
|
|
|
+ // 请求参数和地址
|
|
|
+ getRequestUri(entity);
|
|
|
+ if (loginUser != null) {
|
|
|
+ entity.setUserId(loginUser.getId());
|
|
|
+ }
|
|
|
+ System.out.println(JSONObject.toJSONString(entity));
|
|
|
+ logOperationService.save(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getRequestUri(LogOperation entity) {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ //拼接请求地址
|
|
|
+ stringBuilder.append(request.getRequestURI());
|
|
|
+
|
|
|
+ //参数不为空则拼接参数
|
|
|
+ if (!request.getParameterMap().isEmpty()) {
|
|
|
+ stringBuilder.append(JSONUtil.toJsonStr(request.getParameterMap()));
|
|
|
+ }
|
|
|
+ //请求地址
|
|
|
+ entity.setRequestUri(stringBuilder.toString());
|
|
|
+ entity.setRequestMethod(request.getMethod());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|