|
@@ -0,0 +1,197 @@
|
|
|
|
+package com.fdkankan.manage.aop;
|
|
|
|
+
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import cn.hutool.extra.servlet.ServletUtil;
|
|
|
|
+import cn.hutool.http.ContentType;
|
|
|
|
+import cn.hutool.http.useragent.UserAgent;
|
|
|
|
+import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.fdkankan.common.constant.ServerCode;
|
|
|
|
+import com.fdkankan.manage.config.SaTokenConfigure;
|
|
|
|
+import com.fdkankan.manage.entity.OperLog;
|
|
|
|
+import com.fdkankan.manage.entity.SysUser;
|
|
|
|
+import com.fdkankan.manage.service.ISysUserService;
|
|
|
|
+import com.fdkankan.redis.util.RedisUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
|
+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.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+@Aspect
|
|
|
|
+@Slf4j
|
|
|
|
+public class VisitLogInterceptor {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ISysUserService userService;
|
|
|
|
+
|
|
|
|
+ @Value("${server.servlet.context-path:null}")
|
|
|
|
+ private String contextPath;
|
|
|
|
+
|
|
|
|
+ // 切入点表达式
|
|
|
|
+ @Pointcut("execution(public * com.fdkankan.manage.controller..*.*(..))")
|
|
|
|
+ public void privilege() {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Around("privilege()")
|
|
|
|
+ public Object around(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
|
+
|
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
+
|
|
|
|
+ //获取客户端ip
|
|
|
|
+ String clientIP = ServletUtil.getClientIP(request);
|
|
|
|
+
|
|
|
|
+ //获取uri
|
|
|
|
+ String uri = request.getRequestURI();
|
|
|
|
+ if(StrUtil.isNotEmpty(contextPath)){
|
|
|
|
+ uri = uri.replaceFirst(contextPath, "");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取请求方式
|
|
|
|
+ String method = request.getMethod();
|
|
|
|
+
|
|
|
|
+ //获取浏览器信息
|
|
|
|
+ String browser = this.getBrowser(request);
|
|
|
|
+ //获取操作路径
|
|
|
|
+ String requestPath = this.getRequestPath(uri);
|
|
|
|
+ //获取参数
|
|
|
|
+ String params = this.getParams(pjp, request);
|
|
|
|
+
|
|
|
|
+ //放行
|
|
|
|
+ Object result = pjp.proceed();
|
|
|
|
+ String resultStr = JSON.toJSONString(result);
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(resultStr);
|
|
|
|
+ String msg = "操作失败";
|
|
|
|
+ if(jsonObject.getInteger("code")== null || jsonObject.getInteger("code").equals(ServerCode.SUCCESS.code())){
|
|
|
|
+ msg = "操作成功";
|
|
|
|
+ }
|
|
|
|
+ //获取用户信息 如果已登录,从token中获取用户信息,如果是登录接口,查询数据库获取用户信息
|
|
|
|
+ Long userId =null;
|
|
|
|
+ String userName =null;
|
|
|
|
+ String nickName =null;
|
|
|
|
+ try {
|
|
|
|
+ userId = Long.valueOf(StpUtil.getExtra("userId").toString());
|
|
|
|
+ userName = (String)StpUtil.getExtra("userName");
|
|
|
|
+ nickName = (String)StpUtil.getExtra("nickName");
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ JSONObject paramObj = JSONObject.parseObject(params);
|
|
|
|
+ userName = paramObj.getString("userName");
|
|
|
|
+ SysUser sysUser = userService.getByUserName(userName);
|
|
|
|
+ if(sysUser != null){
|
|
|
|
+ userId = sysUser.getId();
|
|
|
|
+ nickName = sysUser.getNickName();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //写入mongodb
|
|
|
|
+ OperLog operLog = new OperLog();
|
|
|
|
+ operLog.setUserId(userId);
|
|
|
|
+ operLog.setUserName(userName);
|
|
|
|
+ operLog.setNickName(nickName);
|
|
|
|
+ operLog.setRequestPath(requestPath);
|
|
|
|
+ operLog.setUri(uri);
|
|
|
|
+ operLog.setMethod(method);
|
|
|
|
+ operLog.setParams(params);
|
|
|
|
+ operLog.setIp(clientIP);
|
|
|
|
+ operLog.setBrowser(browser);
|
|
|
|
+ operLog.setCreateTime(Calendar.getInstance().getTime());
|
|
|
|
+ operLog.setResult(msg);
|
|
|
|
+ operLog.setOperationType("manage");
|
|
|
|
+ mongoTemplate.insert(operLog);
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getRequestPath(String uri) {
|
|
|
|
+ if(uri.contains("/login")){
|
|
|
|
+ return "登录";
|
|
|
|
+ }
|
|
|
|
+ JSONObject jsonObject = SaTokenConfigure.manageMenuUrl.get(uri);
|
|
|
|
+
|
|
|
|
+ if(StringUtils.isEmpty(jsonObject)){
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
|
+ getMenuName(list,jsonObject.getString("id"));
|
|
|
|
+ Collections.reverse(list);
|
|
|
|
+ StringBuilder requestPath = new StringBuilder();
|
|
|
|
+ for (String path : list) {
|
|
|
|
+ requestPath.append("[").append(path).append("]").append("->");
|
|
|
|
+ }
|
|
|
|
+ int i = requestPath.lastIndexOf("->");
|
|
|
|
+ if(i < 0){
|
|
|
|
+ return requestPath.toString();
|
|
|
|
+ }
|
|
|
|
+ return requestPath.substring(0,i);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private int getMenuName( List<String> list,String menuId){
|
|
|
|
+ JSONObject jsonObject = SaTokenConfigure.manageMenuId.get(menuId);
|
|
|
|
+ if(org.springframework.util.StringUtils.isEmpty(jsonObject)){
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ list.add( jsonObject.getString("name"));
|
|
|
|
+ String parentId = jsonObject.getString("parentId");
|
|
|
|
+ if(!StringUtils.isEmpty(parentId)){
|
|
|
|
+ return getMenuName(list,parentId);
|
|
|
|
+ }
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private String getParams(JoinPoint pjp, HttpServletRequest request){
|
|
|
|
+
|
|
|
|
+ // 获取参数名称
|
|
|
|
+ String[] parameterNamesArgs = ((MethodSignature) pjp.getSignature()).getParameterNames();
|
|
|
|
+ //获取请求参数值
|
|
|
|
+ Object[] args = pjp.getArgs();
|
|
|
|
+
|
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
|
+ String contentType = request.getContentType();
|
|
|
|
+ if(ContentType.JSON.getValue().equals(contentType)){
|
|
|
|
+ return JSON.toJSONString(args[0]);
|
|
|
|
+ }else{
|
|
|
|
+ for (int i = 0; i < args.length; i++) {
|
|
|
|
+ if(args[i] instanceof MultipartFile){
|
|
|
|
+ paramMap.put(parameterNamesArgs[i], ((MultipartFile) args[i]).getOriginalFilename());
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ paramMap.put(parameterNamesArgs[i], args[i]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return JSON.toJSONString(paramMap);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getBrowser(HttpServletRequest request){
|
|
|
|
+ String userAgentStr = request.getHeader("User-Agent");
|
|
|
|
+ UserAgent userAgent = UserAgentUtil.parse(userAgentStr);
|
|
|
|
+ String browserType = userAgent.getBrowser().toString();
|
|
|
|
+ String browserVersion = userAgent.getVersion();
|
|
|
|
+ String browserFormat = "%s(版本%s)";
|
|
|
|
+ return String.format(browserFormat, browserType, browserVersion);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|