首页>代码>基于Springboot+Mybatis+ SpringMvc+springsecrity+Redis完整网站后台管理系统>/boot-security-master/src/main/java/com/boot/security/server/advice/LogAdvice.java
package com.boot.security.server.advice;

import com.boot.security.server.utils.UserUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.boot.security.server.annotation.LogAnnotation;
import com.boot.security.server.model.SysLogs;
import com.boot.security.server.service.SysLogService;

import io.swagger.annotations.ApiOperation;

/**
 * 统一日志处理
 *
 * @author wpw
 *
 *         2017年8月19日
 */
@Aspect
@Component
public class LogAdvice {

	@Autowired
	private SysLogService logService;

	@Around(value = "@annotation(com.boot.security.server.annotation.LogAnnotation)")
	public Object logSave(ProceedingJoinPoint joinPoint) throws Throwable {
		SysLogs sysLogs = new SysLogs();
        sysLogs.setUser(UserUtil.getLoginUser()); // 设置当前登录用户
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

		String module = null;
		LogAnnotation logAnnotation = methodSignature.getMethod().getDeclaredAnnotation(LogAnnotation.class);
		module = logAnnotation.module();
		if (StringUtils.isEmpty(module)) {
			ApiOperation apiOperation = methodSignature.getMethod().getDeclaredAnnotation(ApiOperation.class);
			if (apiOperation != null) {
				module = apiOperation.value();
			}
		}

		if (StringUtils.isEmpty(module)) {
			throw new RuntimeException("没有指定日志module");
		}
		sysLogs.setModule(module);

		try {
			Object object = joinPoint.proceed();
			sysLogs.setFlag(true);

			return object;
		} catch (Exception e) {
			sysLogs.setFlag(false);
			sysLogs.setRemark(e.getMessage());
			throw e;
        } finally {
            if (sysLogs.getUser() != null) {
                logService.save(sysLogs);
            }
        }

	}
}
最近下载更多
微信网友_6765346083115008  LV1 2023年12月5日
wuying8208  LV15 2023年11月28日
WHY-small  LV15 2023年8月29日
yqyqyqyqy  LV4 2023年7月23日
debris  LV1 2023年5月18日
hihhhh  LV6 2023年4月2日
adongliu666  LV4 2023年3月28日
quyiyi  LV3 2023年3月23日
2754137495  LV6 2023年1月10日
LITIANYU084414  LV11 2022年12月29日
最近浏览更多
MaOuMaOu 4月8日
暂无贡献等级
熬夜孤狼 3月19日
暂无贡献等级
QzLinsy 3月8日
暂无贡献等级
Z-Carpe-Diem  LV1 3月6日
sweetbox  LV10 2月28日
killler 2月28日
暂无贡献等级
252413 1月26日
暂无贡献等级
暂无贡献等级
shigurea 1月5日
暂无贡献等级
JacksonLil 1月5日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友