|
|
@@ -1,185 +0,0 @@
|
|
|
-package com.fdkankan.manage.interceptor;
|
|
|
-
|
|
|
-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.bean.MenuBean;
|
|
|
-import com.fdkankan.manage.entity.OperLog;
|
|
|
-import com.fdkankan.manage.entity.SysUser;
|
|
|
-import com.fdkankan.manage.service.ISysUserService;
|
|
|
-import com.fdkankan.redis.constant.RedisKey;
|
|
|
-import com.fdkankan.redis.util.RedisUtil;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-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.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 params = this.getParams(pjp, request);
|
|
|
-
|
|
|
- //获取操作路径
|
|
|
- String requestPath = this.getRequestPath(uri);
|
|
|
-
|
|
|
- //放行
|
|
|
- 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)StpUtil.getExtra("userId");
|
|
|
- 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);
|
|
|
- mongoTemplate.insert(operLog);
|
|
|
-
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- private String getRequestPath(String uri){
|
|
|
- String hget = redisUtil.hget(RedisKey.MANAGE_MENU, uri);
|
|
|
- assert StringUtils.isNotBlank(hget);
|
|
|
- MenuBean menuBean = JSON.parseObject(hget, MenuBean.class);
|
|
|
- if(Objects.isNull(menuBean)){
|
|
|
- return null;
|
|
|
- }
|
|
|
- LinkedList<String> menuList = new LinkedList<>();
|
|
|
- this.getRequestPathHandler(menuList, menuBean);
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- menuList.forEach(str->sb.append("->").append(str));
|
|
|
- return sb.substring(2);
|
|
|
- }
|
|
|
-
|
|
|
- private void getRequestPathHandler(LinkedList<String> list, MenuBean menuBean){
|
|
|
- list.addFirst("[" + menuBean.getName() + "]");
|
|
|
- if(Objects.isNull(menuBean.getParent())){
|
|
|
- return;
|
|
|
- }
|
|
|
- this.getRequestPathHandler(list, menuBean.getParent());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|