注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface OpLog {
String value();
}
切面
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.miaoshaproject.annotation.OpLog;
@Component
@Aspect
public class UserOperationAop {
@Autowired
private OperateHistoryService operateHistoryService;
@Before("@annotation(com.xxx.pass.annotation.OpLog)")
private void BeforeController(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
ServletRequestAttributes sra = (ServletRequestAttributes) (RequestContextHolder.getRequestAttributes());
HttpServletRequest request = sra.getRequest();
OpLog annotation = method.getAnnotation(OpLog.class);
String description = annotation.value();
User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
String ip = IPUtils.getIp(request);
String requestMethod = request.getMethod();
String requestUrl = request.getRequestURL().toString();
String queryString = request.getQueryString();
StringBuilder headerBuilder = new StringBuilder();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headerBuilder.append(headerName).append(":").append(headerValue).append(",");
}
String requestHeaders = headerBuilder.toString();
StringBuilder formBuilder = new StringBuilder();
Map<String, String[]> paramterMap = request.getParameterMap();
for (Map.Entry<String, String[]> key : paramterMap.entrySet()) {
String[] values = paramterMap.get(key);
if (values != null && values.length != 0) {
StringBuffer valueBuffer = new StringBuffer();
for (String value : values) {
valueBuffer.append(value).append(",");
}
formBuilder.append(key).append(":").append(valueBuffer);
}
}
String requestParam = formBuilder.toString();
String requestBody = bodyToString(joinPoint);
OperateHistory operateHistory = new OperateHistory();
operateHistory.setUser(loginUser);
operateHistory.setIp(ip);
operateHistory.setDescription(description);
operateHistory.setRequestMethod(requestMethod);
operateHistory.setRequestUrl(requestUrl);
operateHistory.setQueryString(queryString);
operateHistory.setRequestHeaders(requestHeaders);
operateHistory.setRequestParam(requestParam);
operateHistory.setRequestBody(requestBody);
operateHistoryService.insert(OperateHistory);
}
private String bodyToString(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (Annotation[] annotations : parameterAnnotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().isAssignableFrom(RequestBody.class)) {
StringBuffer sb = new StringBuffer();
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
sb.append(arg);
}
return sb.toString();
}
}
}
return null;
}
}