package com.vincent.rsf.httpaudit.support;
|
|
import com.vincent.rsf.httpaudit.props.HttpAuditProperties;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.nio.charset.Charset;
|
import java.nio.charset.StandardCharsets;
|
import java.util.ArrayList;
|
import java.util.Comparator;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 内外部判定、路径说明、响应截断
|
*/
|
public final class HttpAuditSupport {
|
|
private HttpAuditSupport() {
|
}
|
|
public static String resolveScope(HttpServletRequest request, HttpAuditProperties props) {
|
String path = safePath(request);
|
for (String p : props.getExternalPathPrefixes()) {
|
if (path.startsWith(p)) {
|
return "EXTERNAL";
|
}
|
}
|
return "INTERNAL";
|
}
|
|
public static String resolveFunctionDesc(HttpServletRequest request, HttpAuditProperties props) {
|
String path = safePath(request);
|
Map<String, String> map = props.getPathDescriptions();
|
if (map == null || map.isEmpty()) {
|
return null;
|
}
|
List<String> keys = new ArrayList<>(map.keySet());
|
keys.sort(Comparator.comparingInt(String::length).reversed());
|
for (String k : keys) {
|
if (path.startsWith(k)) {
|
return map.get(k);
|
}
|
}
|
return null;
|
}
|
|
public static String safePath(HttpServletRequest request) {
|
String ctx = request.getContextPath();
|
String uri = request.getRequestURI();
|
if (ctx != null && !ctx.isEmpty() && uri.startsWith(ctx)) {
|
return uri.substring(ctx.length());
|
}
|
return uri != null ? uri : "";
|
}
|
|
public static boolean shouldExclude(HttpServletRequest request, HttpAuditProperties props) {
|
String path = safePath(request);
|
for (String p : props.getExcludePathPrefixes()) {
|
if (p != null && !p.isEmpty() && path.startsWith(p)) {
|
return true;
|
}
|
}
|
String lower = path.toLowerCase();
|
if (lower.endsWith(".js") || lower.endsWith(".css") || lower.endsWith(".ico")
|
|| lower.endsWith(".png") || lower.endsWith(".jpg") || lower.endsWith(".gif")
|
|| lower.endsWith(".woff") || lower.endsWith(".woff2") || lower.endsWith(".map")) {
|
return true;
|
}
|
return false;
|
}
|
|
public static boolean isQueryLike(HttpServletRequest request) {
|
String m = request.getMethod();
|
if ("GET".equalsIgnoreCase(m)) {
|
return true;
|
}
|
String path = safePath(request).toLowerCase();
|
return path.contains("/page") || path.contains("/list") || path.contains("/query");
|
}
|
|
public static String clientIp(HttpServletRequest request) {
|
String xff = request.getHeader("X-Forwarded-For");
|
if (xff != null && !xff.isEmpty()) {
|
int i = xff.indexOf(',');
|
return i > 0 ? xff.substring(0, i).trim() : xff.trim();
|
}
|
String real = request.getHeader("X-Real-IP");
|
if (real != null && !real.isEmpty()) {
|
return real.trim();
|
}
|
return request.getRemoteAddr();
|
}
|
|
public static Charset resolveCharset(HttpServletRequest request) {
|
String enc = request.getCharacterEncoding();
|
if (enc == null || enc.isEmpty()) {
|
return StandardCharsets.UTF_8;
|
}
|
try {
|
return Charset.forName(enc);
|
} catch (Exception e) {
|
return StandardCharsets.UTF_8;
|
}
|
}
|
|
public static String bytesToString(byte[] buf, Charset charset) {
|
if (buf == null || buf.length == 0) {
|
return "";
|
}
|
return new String(buf, charset);
|
}
|
|
public static String truncateForStore(String s, int maxChars) {
|
if (s == null) {
|
return null;
|
}
|
if (s.length() <= maxChars) {
|
return s;
|
}
|
return s.substring(0, maxChars) + "...(truncated,len=" + s.length() + ")";
|
}
|
}
|