package integration.configuration;
import cn.hutool.core.collection.CollUtil;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import integration.advice.ControllerAdvice;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Configuration
public class AdviceConfiguration {
private String expression;
[
{
"package": "com.xx.xx.xxxxxx.xxxxxxx.controller",
"class": [
"XXXXXXXController",
"XXXXXXXXController"
]
}
]
@ApolloJsonValue("${advice.expression:}")
private void setExpression(List<Map<String, Object>> value) {
if (CollUtil.isEmpty(value)) return;
StringBuilder sb = new StringBuilder();
for (Map<String, Object> item : value) {
List<String> clazz = (List<String>) item.get("class");
for (String s : clazz) {
sb.append("execution(* ");
sb.append(item.get("package")).append(".");
sb.append(s);
sb.append(".*(..)) || ");
}
}
expression = sb.substring(0, sb.length() - 4);
}
private final Map<String, Boolean> aa = new HashMap<>();
@Bean
@ConditionalOnExpression("${advice.switch:false}")
public AspectJExpressionPointcutAdvisor config() {
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.setExpression(expression);
advisor.setAdvice(new ControllerAdvice());
return advisor;
}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import integration.util.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class ControllerAdvice implements MethodInterceptor {
@Value("${isPrd:false}")
private boolean isEncryption;
private final ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
long start = System.currentTimeMillis();
String requestUrl = getRequestUrl();
Map<String, String> headers = LoginUtil.getHeaders();
Object[] args = invocation.getArguments();
log.info("Controller.LogAspect url:{} headers:{}, param:{} ", requestUrl, headers, printObj(args));
Object result = invocation.proceed();
log.info("Controller.LogAspect url:{} cost:{}ms result:{} ", requestUrl, System.currentTimeMillis() - start, printObj(result));
return result;
}
private String getRequestUrl(){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return attributes == null ? "" : attributes.getRequest().getRequestURI();
}
private Object printObj(Object o){
try {
return isEncryption ? Base64.getEncoder().encodeToString(objectMapper.writeValueAsBytes(o)) : o;
} catch (JsonProcessingException e) {
return e.getMessage();
}
}
}