Search in sources :

Example 1 with IamOperationLog

use of com.diboot.iam.entity.IamOperationLog in project diboot by dibo-software.

the class LogAspect method afterReturningHandler.

/**
 * 操作日志处理
 * @param proceedingJoinPoint
 */
@AfterReturning(value = "pointCut()", returning = "returnObj")
public void afterReturningHandler(JoinPoint proceedingJoinPoint, Object returnObj) {
    IamOperationLog operationLog = buildOperationLog(proceedingJoinPoint);
    BaseLoginUser currentUser = IamSecurityUtils.getCurrentUser();
    if (currentUser == null) {
        currentUser = threadLocal.get();
    }
    // 处理返回结果
    int statusCode = 0;
    String errorMsg = null;
    if (returnObj instanceof JsonResult) {
        JsonResult jsonResult = (JsonResult) returnObj;
        statusCode = jsonResult.getCode();
        if (statusCode > 0) {
            errorMsg = jsonResult.getMsg();
        }
    }
    operationLog.setStatusCode(statusCode).setErrorMsg(errorMsg);
    // 异步保存操作日志
    iamAsyncWorker.saveOperationLog(operationLog, currentUser);
}
Also used : BaseLoginUser(com.diboot.iam.entity.BaseLoginUser) IamOperationLog(com.diboot.iam.entity.IamOperationLog) JoinPoint(org.aspectj.lang.JoinPoint) JsonResult(com.diboot.core.vo.JsonResult)

Example 2 with IamOperationLog

use of com.diboot.iam.entity.IamOperationLog in project diboot by dibo-software.

the class LogAspect method afterThrowingHandler.

/**
 * 操作日志处理
 * @param joinPoint
 */
@AfterThrowing(value = "pointCut()", throwing = "throwable")
public void afterThrowingHandler(JoinPoint joinPoint, Throwable throwable) {
    IamOperationLog operationLog = buildOperationLog(joinPoint);
    // 处理返回结果
    int statusCode = Status.FAIL_EXCEPTION.code();
    String errorMsg = null;
    if (throwable != null) {
        errorMsg = throwable.toString();
        StackTraceElement[] stackTraceElements = throwable.getStackTrace();
        if (V.notEmpty(stackTraceElements)) {
            errorMsg += " : " + stackTraceElements[0].toString();
        }
        errorMsg = S.cut(errorMsg, maxLength);
    }
    operationLog.setStatusCode(statusCode).setErrorMsg(errorMsg);
    // 异步保存操作日志
    iamAsyncWorker.saveOperationLog(operationLog, IamSecurityUtils.getCurrentUser());
}
Also used : IamOperationLog(com.diboot.iam.entity.IamOperationLog) JoinPoint(org.aspectj.lang.JoinPoint)

Example 3 with IamOperationLog

use of com.diboot.iam.entity.IamOperationLog in project diboot by dibo-software.

the class LogAspect method buildOperationLog.

/**
 * 构建操作日志对象
 * @param joinPoint
 * @return
 */
private IamOperationLog buildOperationLog(JoinPoint joinPoint) {
    IamOperationLog operationLog = new IamOperationLog();
    // 当前请求信息
    RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
    operationLog.setRequestMethod(request.getMethod()).setRequestUri(request.getRequestURI()).setRequestIp(HttpHelper.getRequestIp(request));
    // 记录url请求参数 及 body参数
    Map<String, Object> paramsMap = new HashMap<>();
    if (V.notEmpty(request.getQueryString())) {
        paramsMap.put("query", request.getQueryString());
    }
    // 补充注解信息
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    Log logAnno = AnnotationUtils.getAnnotation(method, Log.class);
    // 保存requestBody数据
    if (logAnno.saveRequestData()) {
        Object[] bodyParams = joinPoint.getArgs();
        if (V.notEmpty(bodyParams)) {
            for (Object arg : bodyParams) {
                if (arg != null && isSimpleDataType(arg)) {
                    paramsMap.put(arg.getClass().getSimpleName(), arg);
                }
            }
        }
    }
    String paramsJson = null;
    if (V.notEmpty(paramsMap)) {
        paramsJson = JSON.stringify(paramsMap);
    }
    operationLog.setRequestParams(paramsJson);
    String businessObj = logAnno.businessObj();
    if (V.isEmpty(businessObj)) {
        Class clazz = method.getDeclaringClass();
        Class entityClazz = BeanUtils.getGenericityClass(clazz, 0);
        if (entityClazz != null) {
            businessObj = entityClazz.getSimpleName();
        } else {
            log.warn("@Log(operation='{}') 注解未识别到class泛型参数,请指定 businessObj", logAnno.operation());
        }
    }
    String appModule = null;
    // 自动识别appModule
    operationLog.setAppModule(appModule).setBusinessObj(businessObj).setOperation(logAnno.operation());
    return operationLog;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) HashMap(java.util.HashMap) Log(com.diboot.iam.annotation.Log) IamOperationLog(com.diboot.iam.entity.IamOperationLog) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) IamOperationLog(com.diboot.iam.entity.IamOperationLog) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Method(java.lang.reflect.Method) HttpServletRequest(javax.servlet.http.HttpServletRequest)

Aggregations

IamOperationLog (com.diboot.iam.entity.IamOperationLog)3 JoinPoint (org.aspectj.lang.JoinPoint)2 JsonResult (com.diboot.core.vo.JsonResult)1 Log (com.diboot.iam.annotation.Log)1 BaseLoginUser (com.diboot.iam.entity.BaseLoginUser)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1