优化
This commit is contained in:
@@ -22,7 +22,6 @@ import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -57,9 +56,10 @@ public class BusinessAuditAspect {
|
||||
HttpServletRequest req = request();
|
||||
AuthenticatedUser user = currentUser();
|
||||
String resourceId = evaluateResourceId(pjp, a.resourceId());
|
||||
Long tenantId = resolveTenantId(pjp, a, user, req);
|
||||
sink.publish(new AuditEvent(UUID.randomUUID().toString(), a.event(), a.resourceType(), resourceId, a.description(),
|
||||
req == null ? null : CorrelationContext.requestId(req), req == null ? null : CorrelationContext.traceId(req),
|
||||
user == null ? headerLong(req, "X-Tenant-Id") : user.tenantId(),
|
||||
tenantId,
|
||||
user == null ? headerLong(req, "X-Operator-Tenant-Id") : user.operatorTenantId(),
|
||||
user == null ? null : user.userId(), user == null ? null : user.username(),
|
||||
user == null ? header(req, "X-App-Code") : user.appCode(), user == null ? null : user.clientType(),
|
||||
@@ -81,9 +81,50 @@ public class BusinessAuditAspect {
|
||||
}
|
||||
private String evaluateResourceId(ProceedingJoinPoint p, String expression) {
|
||||
if (expression == null || expression.isBlank()) return null;
|
||||
Object value = evaluate(p, expression);
|
||||
return value == null ? null : value.toString();
|
||||
}
|
||||
private Long resolveTenantId(ProceedingJoinPoint p, BusinessAudit audit, AuthenticatedUser user,
|
||||
HttpServletRequest request) {
|
||||
if (user == null) return headerLong(request, "X-Tenant-Id");
|
||||
if (!"PLATFORM".equals(user.appCode()) || !Long.valueOf(0L).equals(user.tenantId())) {
|
||||
return user.tenantId();
|
||||
}
|
||||
Long explicit = evaluatePositiveLong(p, audit.tenantId());
|
||||
if (explicit != null) return explicit;
|
||||
MethodSignature signature = (MethodSignature) p.getSignature();
|
||||
String[] parameterNames = signature.getParameterNames();
|
||||
if (parameterNames == null) {
|
||||
parameterNames = new DefaultParameterNameDiscoverer().getParameterNames(signature.getMethod());
|
||||
}
|
||||
Object[] arguments = p.getArgs();
|
||||
if (parameterNames != null) {
|
||||
for (int index = 0; index < parameterNames.length && index < arguments.length; index++) {
|
||||
if ("tenantId".equals(parameterNames[index])) {
|
||||
Long candidate = positiveLong(arguments[index]);
|
||||
if (candidate != null) return candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
return user.tenantId();
|
||||
}
|
||||
private Long evaluatePositiveLong(ProceedingJoinPoint p, String expression) {
|
||||
if (expression == null || expression.isBlank()) return null;
|
||||
return positiveLong(evaluate(p, expression));
|
||||
}
|
||||
private Object evaluate(ProceedingJoinPoint p, String expression) {
|
||||
MethodSignature s = (MethodSignature) p.getSignature();
|
||||
var ctx = new MethodBasedEvaluationContext(p.getTarget(), s.getMethod(), p.getArgs(), new DefaultParameterNameDiscoverer());
|
||||
Object v = parser.parseExpression(expression).getValue(ctx); return v == null ? null : v.toString();
|
||||
return parser.parseExpression(expression).getValue(ctx);
|
||||
}
|
||||
private static Long positiveLong(Object value) {
|
||||
if (value == null) return null;
|
||||
try {
|
||||
long candidate = value instanceof Number number ? number.longValue() : Long.parseLong(value.toString());
|
||||
return candidate > 0 ? candidate : null;
|
||||
} catch (NumberFormatException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private static String header(HttpServletRequest r, String n) { return r == null ? null : r.getHeader(n); }
|
||||
private static Long headerLong(HttpServletRequest r, String n) { try { return header(r, n) == null ? null : Long.valueOf(header(r, n)); } catch (NumberFormatException e) { return null; } }
|
||||
|
||||
Reference in New Issue
Block a user