xyh
2021-06-28 a9b61bbd2611a69a653b7cbf661271cea69a4deb
springcloud_k8s_panzhihuazhihuishequ/service_api/src/main/java/com/panzhihua/serviceapi/aop/OperLogAspect.java
@@ -51,12 +51,6 @@
    public void operLogPoinCut() {
    }
//    /**
//     * 设置操作异常切入点记录异常日志 扫描所有controller包下操作
//     */
//    @Pointcut("execution(* com.hyd.zcar.cms.controller..*.*(..))")
//    public void operExceptionLogPoinCut() {
//    }
    /**
     * 正常返回通知,拦截用户操作日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行
@@ -76,13 +70,13 @@
        String userInfo = request.getHeader(TokenConstant.TOKEN_USERINFO);
        boolean empty = ObjectUtils.isEmpty(userInfo);
        if (empty) {
            log.error("操作日志获取登录用户信息失败【{}】",joinPoint);
            log.error("操作日志获取登录用户信息失败【{}】", joinPoint);
            return;
        }
        byte[] bytes = AES.parseHexStr2Byte(userInfo);
        byte[] decrypt = AES.decrypt(bytes, Constants.AES_KEY);
        userInfo=new String(decrypt);
        LoginUserInfoVO loginUserInfoVO= JSONObject.parseObject(userInfo,LoginUserInfoVO.class);
        userInfo = new String(decrypt);
        LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(userInfo, LoginUserInfoVO.class);
        SysOperLogVO operlog = new SysOperLogVO();
        try {
@@ -96,8 +90,10 @@
            if (opLog != null) {
                String operModul = opLog.operModul();
                int operType = opLog.operType();
                operlog.setTitle(operModul); // 操作模块
                operlog.setBusinessType(operType); // 操作类型
                operlog.setTitle(operModul);
                // 操作模块
                operlog.setBusinessType(operType);
                // 操作类型
            }
            // 获取请求的类名
            String className = joinPoint.getTarget().getClass().getName();
@@ -105,82 +101,41 @@
            String methodName = method.getName();
            methodName = className + "." + methodName;
            operlog.setMethod(methodName); // 请求方法
            operlog.setMethod(methodName);
            // 请求方法
            if (joinPoint.getArgs().length > 0) {
                for (Object o : joinPoint.getArgs()) {
                    if (o instanceof HttpServletRequest || o instanceof HttpServletResponse) {
                        continue;
                    }
                    operlog.setOperParam(JSONObject.toJSONString(o)); // 请求参数
                    operlog.setOperParam(JSONObject.toJSONString(o));
                    // 请求参数
                }
            }
            operlog.setJsonResult(JSONObject.toJSONString(keys)); // 返回结果
            operlog.setJsonResult(JSONObject.toJSONString(keys));
            // 返回结果
            operlog.setAccount(loginUserInfoVO.getAccount());
            operlog.setOperName(loginUserInfoVO.getName()); // 请求用户名称
            operlog.setOperIp(IPUtil.getIpAddress(request)); // 请求IP
            operlog.setOperUrl(request.getRequestURI()); // 请求URI
            operlog.setOperName(loginUserInfoVO.getName());
            // 请求用户名称
            operlog.setOperIp(IPUtil.getIpAddress(request));
            // 请求IP
            operlog.setOperUrl(request.getRequestURI());
            // 请求URI
            operlog.setRequestMethod(request.getMethod());
            operlog.setOperLocation(IPUtil.getIpBelongAddress(request));
            operlog.setOperTime(new Date()); // 创建时间
            operlog.setOperTime(new Date());
            // 创建时间
            operlog.setAccount(loginUserInfoVO.getAccount());
            Long communityId = loginUserInfoVO.getCommunityId();
            operlog.setCommunityId(null==communityId?0:communityId);
            operlog.setCommunityId(null == communityId ? 0 : communityId);
            userService.addOperLog(operlog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    /**
//     * 异常返回通知,用于拦截异常日志信息 连接点抛出异常后执行
//     *
//     * @param joinPoint 切入点
//     * @param e         异常信息
//     */
//    @AfterThrowing(pointcut = "operExceptionLogPoinCut()", throwing = "e")
//    public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
//        // 获取RequestAttributes
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        // 从获取RequestAttributes中获取HttpServletRequest的信息
//        HttpServletRequest request = (HttpServletRequest) requestAttributes
//                .resolveReference(RequestAttributes.REFERENCE_REQUEST);
//
//        ExceptionLog excepLog = new ExceptionLog();
//        try {
//            // 从切面织入点处通过反射机制获取织入点处的方法
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//            // 获取切入点所在的方法
//            Method method = signature.getMethod();
//            excepLog.setExcId(UuidUtil.get32UUID());
//            // 获取请求的类名
//            String className = joinPoint.getTarget().getClass().getName();
//            // 获取请求的方法名
//            String methodName = method.getName();
//            methodName = className + "." + methodName;
//            // 请求的参数
//            Map<String, String> rtnMap = converMap(request.getParameterMap());
//            // 将参数所在的数组转换成json
//            String params = JSON.toJSONString(rtnMap);
//            excepLog.setExcRequParam(params); // 请求参数
//            excepLog.setOperMethod(methodName); // 请求方法名
//            excepLog.setExcName(e.getClass().getName()); // 异常名称
//            excepLog.setExcMessage(stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace())); // 异常信息
//            excepLog.setOperUserId(UserShiroUtil.getCurrentUserLoginName()); // 操作员ID
//            excepLog.setOperUserName(UserShiroUtil.getCurrentUserName()); // 操作员名称
//            excepLog.setOperUri(request.getRequestURI()); // 操作URI
//            excepLog.setOperIp(IPUtil.getRemortIP(request)); // 操作员IP
//            excepLog.setOperVer(operVer); // 操作版本号
//            excepLog.setOperCreateTime(new Date()); // 发生异常时间
//
//            exceptionLogService.insert(excepLog);
//
//        } catch (Exception e2) {
//            e2.printStackTrace();
//        }
//
//    }
    /**
     * 转换request 请求参数
@@ -188,21 +143,22 @@
     * @param paramMap request获取的参数数组
     */
    public Map<String, String> converMap(Map<String, String[]> paramMap) {
        Map<String, String> rtnMap = new HashMap<String, String>();
        Map<String, String> rtnMap = new HashMap<>(6);
        for (String key : paramMap.keySet()) {
            rtnMap.put(key, paramMap.get(key)[0]);
        }
        return rtnMap;
    }
    private  String getPostData(HttpServletRequest request) {
    private String getPostData(HttpServletRequest request) {
        StringBuffer data = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine()))
            while (null != (line = reader.readLine())) {
                data.append(line);
            }
        } catch (IOException e) {
        } finally {
        }