1.实体类
import lombok.*; import net.ruifeng.app.cloud.domain.base.AbstractEntityDefault; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; @Getter @Setter @Entity @Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "rf_operate_log") public class OperateLog extends AbstractEntityDefault { private static final long serialVersionUID = -4346566599206393643L; @Column(name = "execMethod",columnDefinition = "varchar(200) COMMENT '请求控制器方法'") private String execMethod; @Column(name = "requestUri",columnDefinition = "varchar(300) COMMENT '请求路径'") private String requestUri; @Column(name = "requestParam",columnDefinition = "text COMMENT '请求参数全部'") private String requestParam; @Column(name = "requestParamSimple",columnDefinition = "text COMMENT '请求参数,头300字符'") private String requestParamSimple; @Column(name = "requestHeader",columnDefinition = "varchar(300) COMMENT '请求业务头信息'") private String requestHeader; @Column(name = "respText",columnDefinition = "mediumtext COMMENT '响应内容'") private String respText; @Column(name = "handleTime",columnDefinition = "int(11) COMMENT '请求业务头信息'") private Integer handleTime; @Column(name = "clientInfo",columnDefinition = "varchar(300) COMMENT '请求客户端信息'") private String clientInfo; @Column(name = "opUser",columnDefinition = "varchar(30) COMMENT '操作用户'") private String opUser; @Column(name = "opUserId",columnDefinition = "varchar(10) COMMENT '操作用户ID'") private String opUserId; @Column(name = "opTime",columnDefinition = "varchar(30) COMMENT '操作时间'") private String opTime; @Column(name = "opCliectIp",columnDefinition = "varchar(30) COMMENT '操作用户IP'") private String opCliectIp; } |
2.保存数据
2.1在请求拦截器中 OperateLog operateLog = OperateLog.builder().requestUri(request.getRequestURI() + (StringUtils.isNotBlank(request.getQueryString()) ? "?" + request.getQueryString() : "")) .execMethod(method.getBeanType().getName() + "." + method.getMethod().getName()) .requestParam(JSONObject.wrap(request.getParameterMap()).toString()) .clientInfo(request.getHeader("Client")).opCliectIp(IpKit.getIpAddr(request)) .requestHeader(JSONObject.wrap(header).toString()) .requestParamSimple(apiLog.getRequestParam().length() > 300 ? apiLog.getRequestParam().substring(0, 300) : apiLog.getRequestParam()).build(); ThreadLocalKit.set(Enums.ThreadLocalType.OperateLog, operateLog); operateLog.setOpCliectIp(IpKit.getIpAddr(request)); 2.2在请求切面中 OperateLog operateLog = ThreadLocalKit.get(Enums.ThreadLocalType.OperateLog); if(operateLog != null){ operateLog.setRespText(result.toString()); operateLog.setHandleTime((int) (System.currentTimeMillis() - time.get())); operateLogService.getDao().save(operateLog); } |
3.工具类ThreadLocalKit
import lombok.extern.slf4j.Slf4j; import net.ruifeng.app.cloud.core.Enums; import java.util.concurrent.ConcurrentHashMap; /** * ThreadLocal工具类 */ @Slf4j public class ThreadLocalKit { private static final ThreadLocal<ConcurrentHashMap<String, ?>> threadLocal = new ThreadLocal<>(); @SuppressWarnings("unchecked") public static <T> void set(Enums.ThreadLocalType type, T value) { ConcurrentHashMap<String, T> map = (ConcurrentHashMap<String, T>) threadLocal.get(); if (map == null) { map = new ConcurrentHashMap<>(); } map.put(type.name(), value); threadLocal.set(map); } @SuppressWarnings("unchecked") public static <T> T get(Enums.ThreadLocalType type) { ConcurrentHashMap<String, T> map = (ConcurrentHashMap<String, T>) threadLocal.get(); if (map == null) { map = new ConcurrentHashMap<>(); } return map.get(type.name()); } public static void clear() { threadLocal.remove(); } } |
4.工具类IpKit
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest; import java.net.*; import java.util.Enumeration; import java.util.List; @Slf4j public class IpKit { /** * 获取真实IP */ public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); // 判断是否为反向代理,多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? // 是取X-Forwarded-For中第一个非unknown的有效IP字符串 if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) { String[] tempArray = ip.split(","); for (String val : tempArray) { if (!"unknown".equalsIgnoreCase(val)) { ip = val.replaceAll("\\s", ""); break; } } } 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("x-forvarded-for"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("x-real-ip"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { try { ip = request.getRemoteAddr(); } catch (Exception e) { log.warn(e.getMessage(), e); ip = "unknown"; } } return ip; } /** * 获取本机地址 */ public static String getLocalIp() { String ip = null; try { Enumeration<?> allNetInterfaces = NetworkInterface.getNetworkInterfaces(); while (allNetInterfaces.hasMoreElements()) { NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses(); for (InterfaceAddress add : InterfaceAddress) { InetAddress Ip = add.getAddress(); if (Ip != null && Ip instanceof Inet4Address) { ip = Ip.getHostAddress(); } } } } catch (SocketException e) { log.warn(e.getMessage(), e); } return ip; } /** * 格式化Mac地址 * * @param addr 源地址 */ public static String formatMacAddr(String addr) { if (StringUtils.isBlank(addr)) { return addr; } String mac = addr.replaceAll("(.{2})", "$1:").toUpperCase(); return mac.substring(0, mac.length() - 1); } } |