| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package com.ydtech.aop;
- import com.alibaba.fastjson.JSONObject;
- import com.ydtech.core.page.HttpResult;
- import com.ydtech.modules.admin.model.SysLog;
- import com.ydtech.modules.admin.service.SysLogService;
- import com.ydtech.security.utils.JwtTokenUtils;
- import io.swagger.annotations.ApiOperation;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.*;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.servlet.http.HttpServletRequest;
- import java.lang.reflect.Method;
- import java.util.Date;
- /**
- * 日志切面类
- * AOP切面,记录方法的调用,入参以及出參
- */
- @Aspect
- @Component
- public class LogAspect {
- private final static Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
- @Autowired
- private GlobalExceptionHandler exceptionHandle;
- @Autowired
- private SysLogService sysLogService;
- @Pointcut("execution(public * com.ydtech.modules.*.controller.*.*(..)) ")
- public void log() {
- }
- @Before("log()")
- public void doBefore(JoinPoint joinPoint) {
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- HttpServletRequest request = attributes.getRequest();
- //请求人 url method ip class_method
- SysLog sysLog = new SysLog(JwtTokenUtils.getUsernameFromRequest(request), request.getRequestURL().toString(), request.getMethod(), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), request.getRemoteAddr(), new Date());
- try {
- //获取注解注释
- String annotationValue = getAnnotationValue(joinPoint);
- sysLog.setDescription(annotationValue);
- //获取请求参数
- Object[] args = joinPoint.getArgs();
- for (Object obj : args) {
- if (null != obj && (obj.getClass().getName().startsWith("com.ydtech.modules") || obj instanceof String)) {
- sysLog.setParams(JSONObject.toJSON(obj).toString());
- }
- }
- if (request.getMethod().equals("GET")) {
- sysLog.setParams(request.getQueryString());
- }
- sysLog.preInsert();
- sysLogService.insert(sysLog);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- @Around("log()")
- public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
- HttpResult result = null;
- try {
- } catch (Exception e) {
- return exceptionHandle.exceptionGet(e);
- }
- if (result == null) {
- return proceedingJoinPoint.proceed();
- } else {
- return result;
- }
- }
- @AfterReturning(pointcut = "log()", returning = "object")//打印输出结果
- public void doAfterReturing(Object object) {
- if (object != null) {
- LOGGER.info("response={}", object.toString());
- }
- }
- private String getAnnotationValue(JoinPoint joinPoint) throws ClassNotFoundException {
- //反射获取类
- Class<?> clazz = Class.forName(joinPoint.getSignature().getDeclaringTypeName());
- //通过方法名反射获取method
- Method[] declaredMethods = clazz.getDeclaredMethods();
- //请求方法名
- String requestName = joinPoint.getSignature().getName();
- for (Method method : declaredMethods) {
- String name = method.getName();
- //获取方法上的注解
- if (name.equals(requestName)) {
- ApiOperation annotation = method.getAnnotation(ApiOperation.class);
- if (annotation != null) {
- return annotation.value();
- }
- }
- }
- return null;
- }
- }
|