【add】关键接口日志记录功能实现,优化IP获取方式
This commit is contained in:
parent
3b14f26a76
commit
e80b644f62
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,9 @@
|
||||||
package com.chint.infrastructure.config.webconfig;
|
package com.chint.infrastructure.config.webconfig;
|
||||||
|
|
||||||
|
import com.chint.infrastructure.config.LogConfig.CruxLogInterceptor;
|
||||||
import com.chint.infrastructure.config.LogConfig.RequestLoggingInterceptor;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
@ -24,6 +23,14 @@ public class WebConfig implements WebMvcConfigurer {
|
||||||
return new RequestLoggingInterceptor();
|
return new RequestLoggingInterceptor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关键接口日志记录
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public CruxLogInterceptor getCruxLogInterceptor() {
|
||||||
|
return new CruxLogInterceptor();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
registry.addInterceptor(new JwtTokenAdminInterceptor())
|
registry.addInterceptor(new JwtTokenAdminInterceptor())
|
||||||
|
@ -35,6 +42,14 @@ public class WebConfig implements WebMvcConfigurer {
|
||||||
registry.addInterceptor(getMyRequestLoggingInterceptor())
|
registry.addInterceptor(getMyRequestLoggingInterceptor())
|
||||||
.addPathPatterns("/**")
|
.addPathPatterns("/**")
|
||||||
.excludePathPatterns("/location/**", "/**/query/**", "/**/pageQuery/**");
|
.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
|
@Override
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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<CruxSystemLogBasic,Long> {
|
||||||
|
|
||||||
|
}
|
|
@ -39,7 +39,7 @@ public class DataJdbcCreatorUtil {
|
||||||
*/
|
*/
|
||||||
private List<String> getTables() throws SQLException {
|
private List<String> getTables() throws SQLException {
|
||||||
List<String> tables = new ArrayList<String>();
|
List<String> tables = new ArrayList<String>();
|
||||||
tables.add("order_detail");
|
tables.add("crux_system_log");
|
||||||
return tables;
|
return tables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue