From e80b644f62f9079a88870fe692275136b2bc5dc4 Mon Sep 17 00:00:00 2001 From: nixj Date: Thu, 27 Jun 2024 08:49:40 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90add=E3=80=91=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=9E=E7=8E=B0=EF=BC=8C=E4=BC=98=E5=8C=96IP?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/LogConfig/CruxLogInterceptor.java | 72 +++++++++++++++++++ .../config/webconfig/WebConfig.java | 19 ++++- .../manage/entity/CruxSystemLogBasic.java | 53 ++++++++++++++ .../JdbcCruxSystemLogBasicRepository.java | 15 ++++ .../manage/util/DataJdbcCreatorUtil.java | 2 +- .../com/chint/manage/util/IpAddressUtil.java | 64 +++++++++++++++++ 6 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/chint/infrastructure/config/LogConfig/CruxLogInterceptor.java create mode 100644 src/main/java/com/chint/manage/entity/CruxSystemLogBasic.java create mode 100644 src/main/java/com/chint/manage/mapper/JdbcCruxSystemLogBasicRepository.java create mode 100644 src/main/java/com/chint/manage/util/IpAddressUtil.java diff --git a/src/main/java/com/chint/infrastructure/config/LogConfig/CruxLogInterceptor.java b/src/main/java/com/chint/infrastructure/config/LogConfig/CruxLogInterceptor.java new file mode 100644 index 00000000..22746f10 --- /dev/null +++ b/src/main/java/com/chint/infrastructure/config/LogConfig/CruxLogInterceptor.java @@ -0,0 +1,72 @@ +package com.chint.infrastructure.config.LogConfig; + +import com.chint.domain.aggregates.user.User; +import com.chint.infrastructure.util.BaseContext; +import com.chint.manage.entity.CruxSystemLogBasic; +import com.chint.manage.mapper.JdbcCruxSystemLogBasicRepository; +import com.chint.manage.util.IpAddressUtil; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Objects; + +@Slf4j +@Component +public class CruxLogInterceptor implements HandlerInterceptor { + + @Autowired + private JdbcCruxSystemLogBasicRepository jdbcCruxSystemLogBasicRepository; + @Autowired + private IpAddressUtil ipAddressUtil; + + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + // 在请求处理之前进行拦截 + // 获取请求IP + String clientIP = ipAddressUtil.getIpAddress(request); + // 获取请求URL + String url = request.getRequestURL().toString(); + // 获取请求方法 + String method = request.getMethod(); + // 获取url中的请求参数 + String params = request.getQueryString() == null ? "" : request.getQueryString(); + //请求体数据 + String requestBody = ""; + if ("POST".equals(method)) { + requestBody = ContentCachingWrapperFilter.getRequestBody(request); + } + requestBody = requestBody.replaceAll("\\s+", ""); + + //如果有,获取用户 + User user = BaseContext.getCurrentUser(); + String employeeNo = ""; + String name = ""; + if (!Objects.isNull(user)) { + employeeNo = user.getEmployeeNo(); + name = user.getName(); + } + LocalDateTime now = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + String accessTime = now.format(formatter); + + //构建对象 + CruxSystemLogBasic systemLog = CruxSystemLogBasic.builder() + .clientIp(clientIP) + .url(url) + .method(method) + .params(params) + .requestBody(requestBody) + .employeeNo(employeeNo) + .name(name) + .accessTime(accessTime).build(); + jdbcCruxSystemLogBasicRepository.save(systemLog); + + } +} \ No newline at end of file diff --git a/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java b/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java index 7cbded34..f7ca609f 100644 --- a/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java +++ b/src/main/java/com/chint/infrastructure/config/webconfig/WebConfig.java @@ -1,10 +1,9 @@ package com.chint.infrastructure.config.webconfig; +import com.chint.infrastructure.config.LogConfig.CruxLogInterceptor; import com.chint.infrastructure.config.LogConfig.RequestLoggingInterceptor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -24,6 +23,14 @@ public class WebConfig implements WebMvcConfigurer { return new RequestLoggingInterceptor(); } + /** + * 关键接口日志记录 + */ + @Bean + public CruxLogInterceptor getCruxLogInterceptor() { + return new CruxLogInterceptor(); + } + @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new JwtTokenAdminInterceptor()) @@ -35,6 +42,14 @@ public class WebConfig implements WebMvcConfigurer { registry.addInterceptor(getMyRequestLoggingInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/location/**", "/**/query/**", "/**/pageQuery/**"); + //关键接口日志记录 + registry.addInterceptor(getCruxLogInterceptor()) + //报表 + .addPathPatterns("/manage/consumption/export","/manage/standard/export","/manage/order/export", + //公告 + "/system/announcement/save","/system/announcement/update", + //权限 + "/users/add/role","/users/edit/role"); } @Override diff --git a/src/main/java/com/chint/manage/entity/CruxSystemLogBasic.java b/src/main/java/com/chint/manage/entity/CruxSystemLogBasic.java new file mode 100644 index 00000000..5d47bce2 --- /dev/null +++ b/src/main/java/com/chint/manage/entity/CruxSystemLogBasic.java @@ -0,0 +1,53 @@ +package com.chint.manage.entity; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.core.mapping.Table; +import java.io.Serial; +import java.io.Serializable; + +/** + * + * @author xx CruxSystemLogBasic.java + * + **/ +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Table("crux_system_log") +public class CruxSystemLogBasic implements Serializable { +@Serial +private static final long serialVersionUID = 1L; + @Id + private Integer id; + + /**员工SF号**/ + private String employeeNo; + + /**员工姓名**/ + private String name; + + /**访问时间**/ + private String accessTime; + + /**请求IP**/ + private String clientIp; + + /**请求路径**/ + private String url; + + /**请求方法**/ + private String method; + + /**参数**/ + private String params; + + /**请求体**/ + private String requestBody; + + +} diff --git a/src/main/java/com/chint/manage/mapper/JdbcCruxSystemLogBasicRepository.java b/src/main/java/com/chint/manage/mapper/JdbcCruxSystemLogBasicRepository.java new file mode 100644 index 00000000..7eb878a5 --- /dev/null +++ b/src/main/java/com/chint/manage/mapper/JdbcCruxSystemLogBasicRepository.java @@ -0,0 +1,15 @@ +package com.chint.manage.mapper; +import com.chint.manage.entity.CruxSystemLogBasic; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + + +/** + * + * @author xx JdbcCruxSystemLogBasicRepository数据库操作接口类 + * + **/ +@Repository +public interface JdbcCruxSystemLogBasicRepository extends CrudRepository { + +} \ No newline at end of file diff --git a/src/main/java/com/chint/manage/util/DataJdbcCreatorUtil.java b/src/main/java/com/chint/manage/util/DataJdbcCreatorUtil.java index 0b6b23d6..0d7a1b40 100644 --- a/src/main/java/com/chint/manage/util/DataJdbcCreatorUtil.java +++ b/src/main/java/com/chint/manage/util/DataJdbcCreatorUtil.java @@ -39,7 +39,7 @@ public class DataJdbcCreatorUtil { */ private List getTables() throws SQLException { List tables = new ArrayList(); - tables.add("order_detail"); + tables.add("crux_system_log"); return tables; } diff --git a/src/main/java/com/chint/manage/util/IpAddressUtil.java b/src/main/java/com/chint/manage/util/IpAddressUtil.java new file mode 100644 index 00000000..32fabd21 --- /dev/null +++ b/src/main/java/com/chint/manage/util/IpAddressUtil.java @@ -0,0 +1,64 @@ +package com.chint.manage.util; + + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** +* 获取IP工具类 + * 如果是IP白名单限制应该直接使用request.getRemoteAddr() + * 这个无法伪造 +* @Author:nxj +* @Date:2020/6/24 9:20 +*/ +@Component +public class IpAddressUtil { + + private static final String LOCAL = "0:0:0:0:0:0:0:1"; + + + public String getIpAddress(HttpServletRequest request) { + String ip = null; + + //X-Forwarded-For:Squid 服务代理 + String ipAddresses = request.getHeader("X-Forwarded-For"); + String unknown = "unknown"; + if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) { + //Proxy-Client-IP:apache 服务代理 + ipAddresses = request.getHeader("Proxy-Client-IP"); + } + + if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) { + //WL-Proxy-Client-IP:weblogic 服务代理 + ipAddresses = request.getHeader("WL-Proxy-Client-IP"); + } + + if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) { + //HTTP_CLIENT_IP:有些代理服务器 + ipAddresses = request.getHeader("HTTP_CLIENT_IP"); + } + + if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) { + //X-Real-IP:nginx服务代理 + ipAddresses = request.getHeader("X-Real-IP"); + } + + //有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP + if (ipAddresses != null && ipAddresses.length() != 0) { + ip = ipAddresses.split(",")[0]; + } + + //还是不能获取到,最后再通过request.getRemoteAddr();获取 + if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) { + ip = request.getRemoteAddr(); + } + + if(LOCAL.equals(ip)){ + return "127.0.0.1"; + } + return ip; + } +}