|
| 1 | +package io.github.simpleauth0.audit; |
| 2 | + |
| 3 | +import io.github.simpleauth0.audit.annotation.AuditLog; |
| 4 | +import io.github.simpleauth0.audit.expression.ExpressionAttribute; |
| 5 | +import io.github.simpleauth0.audit.handler.AuditLogStorageHandler; |
| 6 | +import io.github.simpleauth0.core.utils.AnnotationUtils; |
| 7 | +import org.aopalliance.aop.Advice; |
| 8 | +import org.aopalliance.intercept.MethodInterceptor; |
| 9 | +import org.aopalliance.intercept.MethodInvocation; |
| 10 | +import org.springframework.aop.Pointcut; |
| 11 | +import org.springframework.aop.PointcutAdvisor; |
| 12 | +import org.springframework.aop.framework.AopInfrastructureBean; |
| 13 | +import org.springframework.aop.support.AopUtils; |
| 14 | +import org.springframework.core.LocalVariableTableParameterNameDiscoverer; |
| 15 | +import org.springframework.core.Ordered; |
| 16 | +import org.springframework.core.ParameterNameDiscoverer; |
| 17 | +import org.springframework.expression.Expression; |
| 18 | +import org.springframework.expression.ExpressionParser; |
| 19 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 20 | +import org.springframework.lang.NonNull; |
| 21 | +import org.springframework.util.Assert; |
| 22 | + |
| 23 | +import java.lang.reflect.Method; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author: ReLive27 |
| 27 | + * @date: 2025/4/25 22:54 |
| 28 | + */ |
| 29 | +public class AuditLogMethodInterceptor implements Ordered, MethodInterceptor, PointcutAdvisor, AopInfrastructureBean { |
| 30 | + |
| 31 | + private final AuditLogExpressionAttributeRegistry registry = new AuditLogExpressionAttributeRegistry(); |
| 32 | + |
| 33 | + private int order = 100; |
| 34 | + |
| 35 | + private final Pointcut pointcut; |
| 36 | + |
| 37 | + private AuditLogStorageHandler auditLogStorageHandler; |
| 38 | + |
| 39 | + public AuditLogMethodInterceptor(AuditLogStorageHandler<? extends ExpressionAttribute> auditLogStorageHandler) { |
| 40 | + Assert.notNull(auditLogStorageHandler, "auditLogStorageHandler cannot be null"); |
| 41 | + this.auditLogStorageHandler = auditLogStorageHandler; |
| 42 | + this.pointcut = AuditLogMethodPointcuts.forAnnotations(AuditLog.class); |
| 43 | + } |
| 44 | + |
| 45 | + private ParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); |
| 46 | + |
| 47 | + |
| 48 | + @Override |
| 49 | + public Object invoke(MethodInvocation mi) throws Throwable { |
| 50 | + Object returnedObject = null; |
| 51 | + Boolean success = true; |
| 52 | + try { |
| 53 | + returnedObject = mi.proceed(); |
| 54 | + return returnedObject; |
| 55 | + } catch (Exception e) { |
| 56 | + returnedObject = e.getMessage(); |
| 57 | + success = false; |
| 58 | + throw e; |
| 59 | + } finally { |
| 60 | + AuditLogExpressionAttribute attribute = this.registry.getAttribute(mi); |
| 61 | + if (attribute != ExpressionAttribute.NULL_ATTRIBUTE) { |
| 62 | + String[] parameterNames = discoverer.getParameterNames(mi.getMethod()); |
| 63 | + if (parameterNames != null && parameterNames.length > 0) { |
| 64 | + attribute.setParams(parameterNames); |
| 65 | + attribute.setArgs(mi.getArguments()); |
| 66 | + } |
| 67 | + attribute.setResult(returnedObject); |
| 68 | + attribute.setSuccess(success); |
| 69 | + this.auditLogStorageHandler.handler(attribute); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Pointcut getPointcut() { |
| 77 | + return this.pointcut; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Advice getAdvice() { |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean isPerInstance() { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int getOrder() { |
| 92 | + return this.order; |
| 93 | + } |
| 94 | + |
| 95 | + public void setOrder(int ordering) { |
| 96 | + this.order = ordering; |
| 97 | + } |
| 98 | + |
| 99 | + public void setAuditLogStorageHandler(AuditLogStorageHandler auditLogStorageHandler) { |
| 100 | + this.auditLogStorageHandler = auditLogStorageHandler; |
| 101 | + } |
| 102 | + |
| 103 | + private final class AuditLogExpressionAttributeRegistry { |
| 104 | + |
| 105 | + private final ExpressionParser parser = new SpelExpressionParser(); |
| 106 | + |
| 107 | + public final AuditLogExpressionAttribute getAttribute(MethodInvocation mi) { |
| 108 | + Method method = mi.getMethod(); |
| 109 | + Object target = mi.getThis(); |
| 110 | + Class<?> targetClass = (target != null) ? target.getClass() : null; |
| 111 | + return resolveAttribute(method, targetClass); |
| 112 | + } |
| 113 | + |
| 114 | + @NonNull |
| 115 | + AuditLogExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) { |
| 116 | + Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); |
| 117 | + AuditLog auditLog = findPostFilterAnnotation(specificMethod); |
| 118 | + if (auditLog == null) { |
| 119 | + return AuditLogExpressionAttribute.NULL_ATTRIBUTE; |
| 120 | + } |
| 121 | + Expression operatorExpression = parser.parseExpression(auditLog.operator()); |
| 122 | + return new AuditLogExpressionAttribute(operatorExpression, auditLog); |
| 123 | + } |
| 124 | + |
| 125 | + private AuditLog findPostFilterAnnotation(Method method) { |
| 126 | + AuditLog auditLog = AnnotationUtils.findUniqueAnnotation(method, AuditLog.class); |
| 127 | + return (auditLog != null) ? auditLog |
| 128 | + : AnnotationUtils.findUniqueAnnotation(method.getDeclaringClass(), AuditLog.class); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + public static final class AuditLogExpressionAttribute extends ExpressionAttribute { |
| 134 | + |
| 135 | + private static final AuditLogExpressionAttribute NULL_ATTRIBUTE = new AuditLogExpressionAttribute(null, null); |
| 136 | + |
| 137 | + private final AuditLog auditLog; |
| 138 | + |
| 139 | + private Object result; |
| 140 | + |
| 141 | + private Boolean success; |
| 142 | + |
| 143 | + private AuditLogExpressionAttribute(Expression expression, AuditLog auditLog) { |
| 144 | + super(expression); |
| 145 | + this.auditLog = auditLog; |
| 146 | + } |
| 147 | + |
| 148 | + public void setResult(Object result) { |
| 149 | + this.result = result; |
| 150 | + } |
| 151 | + |
| 152 | + public void setSuccess(Boolean success) { |
| 153 | + this.success = success; |
| 154 | + } |
| 155 | + |
| 156 | + public Object getResult() { |
| 157 | + return result; |
| 158 | + } |
| 159 | + |
| 160 | + public Boolean getSuccess() { |
| 161 | + return success; |
| 162 | + } |
| 163 | + |
| 164 | + public AuditLog getAuditLog() { |
| 165 | + return auditLog; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | + |
0 commit comments