| | |
| | | package com.oying.service.impl; |
| | | |
| | | import cn.hutool.core.lang.Dict; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.oying.annotation.Log; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | |
| | | public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService { |
| | | |
| | | private final SysLogMapper sysLogMapper; |
| | | // 定义敏感字段常量数组 |
| | | private static final String[] SENSITIVE_KEYS = {"password"}; |
| | | |
| | | @Override |
| | | public PageResult<SysLog> queryAll(SysLogQueryCriteria criteria, Page<SysLog> page) { |
| | |
| | | // 方法路径 |
| | | String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; |
| | | |
| | | // 获取参数 |
| | | JSONObject params = getParameter(method, joinPoint.getArgs()); |
| | | |
| | | // 填充基本信息 |
| | | sysLog.setRequestIp(ip); |
| | | sysLog.setAddress(StringUtils.getCityInfo(sysLog.getRequestIp())); |
| | | sysLog.setMethod(methodName); |
| | | sysLog.setUsername(username); |
| | | sysLog.setParams(JSON.toJSONString(params)); |
| | | sysLog.setParams(getParameter(method, joinPoint.getArgs())); |
| | | sysLog.setBrowser(browser); |
| | | sysLog.setDescription(aopLog.value()); |
| | | |
| | | // 如果没有获取到用户名,尝试从参数中获取 |
| | | if(StringUtils.isBlank(sysLog.getUsername())){ |
| | | sysLog.setUsername(params.getString("username")); |
| | | } |
| | | |
| | | // 保存 |
| | | save(sysLog); |
| | |
| | | /** |
| | | * 根据方法和传入的参数获取请求参数 |
| | | */ |
| | | private JSONObject getParameter(Method method, Object[] args) { |
| | | JSONObject params = new JSONObject(); |
| | | private String getParameter(Method method, Object[] args) { |
| | | List<Object> argList = new ArrayList<>(); |
| | | Parameter[] parameters = method.getParameters(); |
| | | for (int i = 0; i < parameters.length; i++) { |
| | | // 过滤掉 MultiPartFile |
| | |
| | | if (args[i] instanceof HttpServletRequest) { |
| | | continue; |
| | | } |
| | | // 将RequestBody注解修饰的参数作为请求参数 |
| | | //将RequestBody注解修饰的参数作为请求参数 |
| | | RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); |
| | | if (requestBody != null) { |
| | | params.putAll((JSONObject) JSON.toJSON(args[i])); |
| | | } else { |
| | | String key = parameters[i].getName(); |
| | | params.put(key, args[i]); |
| | | argList.add(args[i]); |
| | | } |
| | | } |
| | | // 遍历敏感字段数组并替换值 |
| | | Set<String> keys = params.keySet(); |
| | | for (String key : SENSITIVE_KEYS) { |
| | | if (keys.contains(key)) { |
| | | params.put(key, "******"); |
| | | //将RequestParam注解修饰的参数作为请求参数 |
| | | RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); |
| | | if (requestParam != null) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | String key = parameters[i].getName(); |
| | | if (!StringUtils.isEmpty(requestParam.value())) { |
| | | key = requestParam.value(); |
| | | } |
| | | map.put(key, args[i]); |
| | | argList.add(map); |
| | | } |
| | | } |
| | | // 返回参数 |
| | | return params; |
| | | if (argList.isEmpty()) { |
| | | return ""; |
| | | } |
| | | return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList); |
| | | |
| | | } |
| | | |
| | | @Override |