Search in sources :

Example 1 with Log

use of com.hb0730.boot.admin.annotation.Log in project boot-admin by hb0730.

the class LogAspectj method handleLog.

/**
 * 日志处理
 */
void handleLog(final JoinPoint joinPoint, Object jsonResult, final Exception e) {
    try {
        Log log = getAnnotationLog(joinPoint);
        if (check(joinPoint, log)) {
            return;
        }
        // 当前用户
        OperLogEntity entity = new OperLogEntity();
        // 状态
        entity.setStatus(StatusEnum.SUCCESS.getValue());
        // 操作类型
        entity.setOperType(log.businessType().getValue());
        // 请求用户
        User currentUser = SecurityUtils.getCurrentUser();
        if (null != currentUser) {
            entity.setUsername(currentUser.getUsername());
            entity.setCreateUserId(currentUser.getId());
            entity.setCreateTime(LocalDateTimeUtils.now());
        }
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return;
        }
        // 请求ip
        String ip = "";
        // 请求地址
        String requestUrl = "";
        // 请求方式
        String requestMethod = "";
        if (attributes instanceof ServletRequestAttributes) {
            HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
            ip = ServletUtil.getClientIP(request);
            // 请求地址
            requestUrl = request.getRequestURI();
            requestMethod = request.getMethod();
        }
        entity.setOperIp(ip);
        // 描述
        // 类描述
        String controllerDescription = "";
        ClassDescribe classDescribe = joinPoint.getTarget().getClass().getAnnotation(ClassDescribe.class);
        if (null != classDescribe) {
            controllerDescription = classDescribe.value();
        }
        // 方法描述
        String controllerMethodDescription = getDescribe(log);
        if (log.controllerApiValue()) {
            entity.setDescription(StringUtils.join(controllerDescription, controllerMethodDescription));
        } else {
            entity.setDescription(controllerMethodDescription);
        }
        // 请求地址
        entity.setRequestUrl(requestUrl);
        // 请求方式
        entity.setRequestMethod(requestMethod);
        // 操作方法
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        entity.setOperMethod(className + "." + methodName);
        // 请求参数
        if (log.request()) {
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            String requestParamsValue = getRequestValue(joinPoint, request, log.paramsName());
            entity.setRequestParams(requestParamsValue);
        }
        // 返回参数
        if (log.response()) {
            String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
            entity.setRequestResult(result);
        }
        if (null != e) {
            if (log.requestByError()) {
                String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
                entity.setRequestResult(result);
                String message = ExceptionUtils.getExceptionMessage(e);
                entity.setErrorMessage(StringUtils.substring(message, 0, 2000));
                entity.setStatus(StatusEnum.FAIL.getValue());
            }
        }
        AsyncManager.me().execute(AsyncFactory.recordOperLog(entity));
    } catch (Exception e1) {
        e1.printStackTrace();
        if (null != e) {
            e.printStackTrace();
        }
        // 记录本地异常日志
        LOGGER.error("==前置通知异常==");
        LOGGER.error("异常信息:{}", e1.getMessage());
        if (null != e) {
            throw new BusinessException(e.getMessage());
        } else {
            throw new BusinessException(e1.getMessage());
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) User(com.hb0730.boot.admin.security.model.User) Log(com.hb0730.boot.admin.annotation.Log) OperLogEntity(com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ClassDescribe(com.hb0730.boot.admin.annotation.ClassDescribe) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException)

Example 2 with Log

use of com.hb0730.boot.admin.annotation.Log in project boot-admin by hb0730.

the class IBaseSaveController method save.

/**
 * 保存
 *
 * @param dto 参数
 * @return 是否成功
 */
@PostMapping("/save")
@SuppressWarnings({ "rawtypes", "unchecked" })
@Log(value = "保存", paramsName = { "dto" }, businessType = BusinessTypeEnum.INSERT)
@PreAuthorize("@bootAdmin.hasAnyAuthority(this,'ROLE_ADMINISTRATOR','save')")
default Result<String> save(@RequestBody @Validated DTO dto) {
    ValidatorUtils.validate(dto);
    ISuperBaseService baseService = getBaseService();
    if (null != baseService) {
        baseService.save(dto);
        return R.success("保存成功");
    }
    return R.result(ResponseStatusEnum.PARAMS_REQUIRED_IS_NULL, "service is null");
}
Also used : ISuperBaseService(com.hb0730.boot.admin.domain.service.ISuperBaseService) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(com.hb0730.boot.admin.annotation.Log) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with Log

use of com.hb0730.boot.admin.annotation.Log in project boot-admin by hb0730.

the class IBaseUpdateController method updateById.

/**
 * 根据id修改
 *
 * @param id  id
 * @param dto 修改参数
 * @return 是否成功
 */
@PutMapping("/update/{id}")
@SuppressWarnings({ "rawtypes", "unchecked" })
@Log(value = "修改", paramsName = { "dto" }, businessType = BusinessTypeEnum.UPDATE)
@PreAuthorize("@bootAdmin.hasAnyAuthority(this,'ROLE_ADMINISTRATOR','update')")
default Result<String> updateById(@PathVariable("id") ID id, @Validated @RequestBody DTO dto) {
    ValidatorUtils.validate(dto);
    ISuperBaseService service = getBaseService();
    if (null != service) {
        service.updateById(id, dto);
        return R.success("修改成功");
    }
    return R.result(ResponseStatusEnum.PARAMS_REQUIRED_IS_NULL, "service is null");
}
Also used : ISuperBaseService(com.hb0730.boot.admin.domain.service.ISuperBaseService) Log(com.hb0730.boot.admin.annotation.Log) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with Log

use of com.hb0730.boot.admin.annotation.Log in project boot-admin by hb0730.

the class PostController method export.

/**
 * 导出
 *
 * @param response 响应
 * @param params   请求参数,用于导出过滤
 * @throws ExportExceptions 导出异常
 */
@Override
@PostMapping("/export")
@Log(value = "导出", paramsName = { "params" }, businessType = BusinessTypeEnum.EXPORT)
@PreAuthorize("hasAnyAuthority('ROLE_ADMINISTRATOR','post:export')")
public void export(HttpServletResponse response, @RequestBody PostParams params) throws ExportExceptions {
    List<PostExcelDTO> export = service.export(params);
    HashMap<String, Object> map = Maps.newHashMap();
    try {
        map.put(ExcelConstant.FILE_NAME, "post_excel");
        map.put(ExcelConstant.DATA_LIST, export);
        ExcelUtils.writeWeb(response, map, ExcelTypeEnum.XLS, PostExcelDTO.class);
    } catch (Exception e) {
        e.printStackTrace();
        throw new ExportExceptions("岗位导出失败", e);
    }
}
Also used : PostExcelDTO(com.hb0730.boot.admin.project.system.post.model.dto.PostExcelDTO) ExportExceptions(com.hb0730.boot.admin.exceptions.ExportExceptions) IOException(java.io.IOException) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(com.hb0730.boot.admin.annotation.Log) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with Log

use of com.hb0730.boot.admin.annotation.Log in project boot-admin by hb0730.

the class UserInfoController method restPassword.

/**
 * 重置密码
 *
 * @param id 用户id
 * @return 是否成功
 */
@GetMapping("/rest/password/{id}")
@PreAuthorize("hasAnyAuthority('ROLE_ADMINISTRATOR','user:rest:password')")
@Log(value = "重置密码")
public Result<String> restPassword(@PathVariable("id") Long id) {
    UserInfoEntity entity = service.getById(id);
    if (entity.getIsAdmin() == 1) {
        throw new BusinessException("超级管理员无法重置");
    }
    service.restPassword(id);
    return R.success("重置成功");
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping) Log(com.hb0730.boot.admin.annotation.Log) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

Log (com.hb0730.boot.admin.annotation.Log)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 ISuperBaseService (com.hb0730.boot.admin.domain.service.ISuperBaseService)2 BusinessException (com.hb0730.boot.admin.exceptions.BusinessException)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ClassDescribe (com.hb0730.boot.admin.annotation.ClassDescribe)1 ExportExceptions (com.hb0730.boot.admin.exceptions.ExportExceptions)1 OperLogEntity (com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity)1 PostExcelDTO (com.hb0730.boot.admin.project.system.post.model.dto.PostExcelDTO)1 UserInfoEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity)1 User (com.hb0730.boot.admin.security.model.User)1 IOException (java.io.IOException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1