feat: 删除无用代码

dev_1.0.1
zhanglei 2026-07-16 17:30:46 +08:00
parent 04454600a7
commit 1c51ead7dd
2 changed files with 0 additions and 310 deletions

View File

@ -1,208 +0,0 @@
package org.qinan.safetyeval.adapter.config.support;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.*;
@Slf4j
@Aspect
@Component
public class ControllerLogAspect {
/**
* Controller
*/
@Pointcut("execution(* org.qinan.safetyeval.adapter..web..*.*(..))")
public void controllerPointcut() {
}
private final AntPathMatcher pathMatcher = new AntPathMatcher();
@Value("${anonymous.path:}")
private List<String> anonymousUrl;
@Value("${def.logEnable:true}")
private boolean logEnable;
/**
*
*/
@Around("controllerPointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
if (!logEnable) {
return joinPoint.proceed();
}
long startTime = System.currentTimeMillis();
// 获取请求信息
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes != null ? attributes.getRequest() : null;
// 白名单放行
if (request != null && isWhiteUrl(request.getRequestURI())) {
return joinPoint.proceed();
}
// 获取方法信息
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String className = joinPoint.getTarget().getClass().getName();
String methodName = method.getName();
// 记录请求开始日志
StringBuilder sb = logRequest(request, className, methodName, joinPoint.getArgs());
Object result;
try {
// 执行目标方法
result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
// 记录成功响应日志
logResponse(sb, className, methodName, result, endTime - startTime, true);
} catch (Throwable throwable) {
long endTime = System.currentTimeMillis();
// 记录异常日志
logError(sb, className, methodName, throwable, endTime - startTime);
throw throwable;
}
return result;
}
/**
*
*/
private StringBuilder logRequest(HttpServletRequest request, String className, String methodName, Object[] args) {
StringBuilder sb = new StringBuilder();
if (request != null) {
sb.append("\n========================================== Start ==========================================\n");
sb.append("URL : ").append(request.getScheme()).append("://").append(request.getServerName())
.append(":").append(request.getServerPort()).append(request.getRequestURI()).append("\n");
sb.append("HTTP Method : ").append(request.getMethod()).append("\n");
sb.append("Request IP : ").append(HttpUtil.getRemoteAddress(request)).append("\n");
// 获取请求头信息
Map<String, String> headers = getHeaders(request);
sb.append("Request Header : ").append(JSON.toJSONString(headers)).append("\n");
sb.append("Parameter Map : ").append(JSON.toJSONString(request.getParameterMap())).append("\n");
sb.append("Class Method : ").append(className).append(".").append(methodName).append("\n");
// 格式化请求参数
if (args != null && args.length > 0) {
sb.append("Request Args : ").append(formatRequestArgs(args)).append("\n");
} else {
sb.append("Request Args : {}\n");
}
} else {
sb.append("========================================== Start ==========================================\n");
sb.append("Class Method : ").append(className).append(".").append(methodName).append("\n");
if (args != null && args.length > 0) {
sb.append("Request Args : ").append(formatRequestArgs(args)).append("\n");
} else {
sb.append("Request Args : {}\n");
}
}
return sb;
}
/**
*
*/
private void logResponse(StringBuilder sb, String className, String methodName, Object result, long executionTime, boolean success) {
sb.append("Response Args : ").append(result != null ? JSON.toJSONString(result) : "{}").append("\n");
sb.append("Time Cost : ").append(executionTime).append(" ms\n");
sb.append("=========================================== End ===========================================\n");
log.info(sb.toString());
}
/**
*
*/
private void logError(StringBuilder sb, String className, String methodName, Throwable throwable, long executionTime) {
sb.append("Response Args : {\"error\": \"").append(throwable.getMessage()).append("\"}\n");
sb.append("Time Cost : ").append(executionTime).append(" ms\n");
sb.append("=========================================== End ===========================================\n");
log.error(sb.toString());
}
/**
*
*/
private Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> headers = new HashMap<>();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
headers.put(headerName, request.getHeader(headerName));
}
return headers;
}
/**
*
*/
private String formatRequestArgs(Object[] args) {
if (args == null || args.length == 0) {
return "{}";
}
List<Object> safeArgs = new ArrayList<>(args.length);
for (Object arg : args) {
safeArgs.add(formatArg(arg));
}
if (safeArgs.size() == 1) {
return JSON.toJSONString(safeArgs.get(0));
}
return JSON.toJSONString(safeArgs);
}
private Object formatArg(Object arg) {
if (arg == null) {
return null;
}
// if (arg instanceof MultipartFile file) {
// Map<String, Object> meta = new LinkedHashMap<>();
// meta.put("type", "MultipartFile");
// meta.put("originalFilename", StringUtils.defaultString(file.getOriginalFilename()));
// meta.put("size", file.getSize());
// meta.put("contentType", StringUtils.defaultString(file.getContentType()));
// return meta;
// }
if (arg instanceof ServletRequest || arg instanceof ServletResponse) {
return arg.getClass().getSimpleName();
}
return arg;
}
/**
*
* @param url
* @return
*/
private boolean isWhiteUrl(String url) {
if (StringUtils.isBlank(url)) {
return false;
}
for (String s : anonymousUrl) {
if (pathMatcher.match(s, url)) {
return true;
}
}
return false;
}
}

View File

@ -1,102 +0,0 @@
package org.qinan.safetyeval.adapter.config.support;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
public class HttpUtil {
/**
* IP
*
* @param request
* @return IP
*/
public static String getRemoteAddress(HttpServletRequest request) {
return getClientIp(request);
}
/**
* IP
*/
public static String getClientIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值第一个ip才是真实ip
if (ip.contains(",")) {
ip = ip.split(",")[0];
}
}
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("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* unknown IP
*
* @param ip IP
* @return unknown IP
*/
public static String getMultistageReverseProxyIp(String ip) {
// 多级反向代理检测
if (ip != null && ip.indexOf(",") > 0) {
final String[] ips = ip.trim().split(",");
for (String subIp : ips) {
if (!isUnknown(subIp)) {
ip = subIp;
break;
}
}
}
return ip;
}
/**
* HTTP
*
* @param checkString
* @return
*/
public static boolean isUnknown(String checkString) {
return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
}
/**
* 访
*
* @param request
* @param includeContextPath
* @return
*/
private static String getRequestURI(HttpServletRequest request, boolean includeContextPath) {
String requestURI = request.getRequestURI();
if (includeContextPath) {
return requestURI;
} else {
String contextPath = request.getContextPath();
return requestURI.replace(contextPath, "");
}
}
public static String getRequestURI(HttpServletRequest request) {
return getRequestURI(request, false);
}
public static String getFullRequestURI(HttpServletRequest request) {
return getRequestURI(request, true);
}
}