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());
}
}
}
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");
}
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");
}
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);
}
}
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("重置成功");
}
Aggregations