cl
8 小时以前 c4bba32b20f0869b45ed14be04543869dd91ee6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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.getEffectiveExcludePrefixes()) {
            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() + ")";
    }
 
    /** maxChars&lt;0 不截断 */
    public static String storeWithCharLimit(String s, int maxChars) {
        if (s == null) {
            return null;
        }
        if (maxChars < 0) {
            return s;
        }
        return truncateForStore(s, maxChars);
    }
 
    public static boolean overCharLimit(String s, int maxChars) {
        return s != null && maxChars >= 0 && s.length() > maxChars;
    }
 
    public static String truncateUriForStore(String uri, int maxLen) {
        if (uri == null) {
            return "";
        }
        if (maxLen <= 0 || uri.length() <= maxLen) {
            return uri;
        }
        return uri.substring(0, maxLen - 3) + "...";
    }
}