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
package com.vincent.rsf.httpaudit.service;
 
import com.vincent.rsf.httpaudit.entity.HttpAuditLog;
import com.vincent.rsf.httpaudit.model.HttpAuditDecision;
import com.vincent.rsf.httpaudit.props.HttpAuditProperties;
import com.vincent.rsf.httpaudit.support.HttpAuditSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
 
import java.util.Date;
 
/**
 * 出站 HTTP 审计落库(RestTemplate / Feign 共用)
 */
@Slf4j
@RequiredArgsConstructor
public class HttpAuditOutboundRecorder {
 
    private final HttpAuditAsyncRecorder recorder;
    private final HttpAuditProperties props;
    private final Environment environment;
    private final HttpAuditRuleService ruleService;
 
    public boolean isAuditEnabled() {
        return props.isEnabled();
    }
 
    public HttpAuditDecision decideOutbound(String url, String method, String requestBody) {
        return ruleService.decideOutbound(url, method, requestBody == null ? "" : requestBody);
    }
 
    public void saveOutbound(String functionDesc, String url, String method, String reqText,
                             HttpAuditDecision dec, Integer httpStatus, String resText, long startTimeMs, Throwable ex) {
        if (!dec.isAudit()) {
            return;
        }
        try {
            int reqMax = dec.getRequestMaxChars() != null ? dec.getRequestMaxChars() : props.getDefaultRequestStoreChars();
            int resMax = dec.getResponseMaxChars() != null ? dec.getResponseMaxChars() : props.getMaxResponseStoreChars();
            String reqStored = HttpAuditSupport.storeWithCharLimit(reqText, reqMax);
            String resStored = HttpAuditSupport.storeWithCharLimit(resText, resMax);
            int truncated = HttpAuditSupport.overCharLimit(resText, resMax) ? 1 : 0;
            int ok = (ex == null && httpStatus != null && httpStatus >= 200 && httpStatus < 400) ? 1 : 0;
            String errMsg = null;
            if (ex != null) {
                String s = ex.toString();
                errMsg = s.length() > 4000 ? s.substring(0, 4000) + "..." : s;
            }
            String appName = environment.getProperty("spring.application.name", "unknown");
            String uriStored = HttpAuditSupport.truncateUriForStore(url, 512);
            HttpAuditLog entity = new HttpAuditLog()
                    .setServiceName(appName)
                    .setScopeType("EXTERNAL")
                    .setUri(uriStored)
                    .setIoDirection("OUT")
                    .setMethod(method)
                    .setFunctionDesc(functionDesc)
                    .setQueryString(null)
                    .setRequestBody(reqStored)
                    .setResponseBody(resStored)
                    .setResponseTruncated(truncated)
                    .setHttpStatus(httpStatus)
                    .setOkFlag(ok)
                    .setSpendMs((int) Math.min(Integer.MAX_VALUE, System.currentTimeMillis() - startTimeMs))
                    .setClientIp(null)
                    .setErrorMessage(errMsg)
                    .setCreateTime(new Date())
                    .setDeleted(0);
            recorder.save(entity);
        } catch (Throwable t) {
            log.debug("http-audit 出站组装审计失败:{}", t.getMessage());
        }
    }
}