从零开始搭建自己的网站二十四:使用注解记录操作日志

对于一个网站来说肯定需要记录操作日志,比如在几点几分,进行了登录,几点几分删除了一篇文章等这些操作日志。

我们使用AOP+注解的方式来进行记录操作日志。

在下面的代码中,需要注意的就是方法上注解要写在第一行

/**
 * 操作前日志,用于注销,获取用户名和ID
 */
@Target(value = {ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemServiceLogBefore {

    /**
     * 操作类型,必填
     */
    String action();

    /**
     * 操作的模块
     */
    String module() default "";

}
@Target(value = {ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemServiceLog {

    /**
     * 操作类型,必填
     */
    String action();

    /**
     * 操作的模块
     */
    String module() default "";

}
/**
 * 切面类
 */
@Aspect
@Component
public class SystemLogAspect {

    private static final Logger logger = Logger.getLogger(SystemLogAspect.class);

    @Autowired
    private LogInfoDao logInfoDao;

    /**
     * Service层切点
     */
    @Pointcut("@annotation(com.dyw.utils.log.SystemServiceLog)")
    public void serviceAspect() {

    }

    /**
     * Service层切点
     */
    @Pointcut("@annotation(com.dyw.utils.log.SystemServiceLogBefore)")
    public void serviceAspectBefore() {

    }

    /**
     * 获取注解参数,记录日志
     *
     * @param joinPoint 切入点参数
     */

    @After("serviceAspect()")
    public void doServiceLog(JoinPoint joinPoint) {
        createLog(joinPoint);
    }

    @Before("serviceAspectBefore()")
    public void serviceAspectBefore(JoinPoint joinPoint) {
        createLog(joinPoint);
    }

    /**
     * 获取 操作指令
     */
    private String getMethodActionAndModule(JoinPoint joinPoint) throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String action = "";
        String module = "";
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                    Annotation[] annotations = method.getAnnotations();
                    try{
                        //用annotations[0],使用注解的时候必须把注解放在第一行
                        SystemServiceLog systemServiceLog = (SystemServiceLog) annotations[0];
                        action = systemServiceLog.action();
                        module = systemServiceLog.module();
                    }catch (Exception e){
                        SystemServiceLogBefore systemServiceLog = (SystemServiceLogBefore) annotations[0];
                        action = systemServiceLog.action();
                        module = systemServiceLog.module();
                    }
                }
            }
        }
        if(StringUtils.isNotBlank(module)){
            return action + "-" + module;
        }
        return action;
    }

    private void createLog(JoinPoint joinPoint) {
        logger.info("日志记录");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        LogInfo logInfo = new LogInfo();
        try {
            //获取操作行为及操作模块
            String am = getMethodActionAndModule(joinPoint);
            logInfo.setOperate(am);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        User user = (User) request.getSession().getAttribute("user");
        //操作时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String operateTime = sdf.format(new Date());
        //判断是pc端还是手机
        String equipment;
        String userAgent = request.getHeader("User-Agent");
        boolean pcFlag = userAgent != null && (userAgent.toUpperCase().contains("WINDOWS") || userAgent.toUpperCase().contains("MACINTOSH"));
        if (pcFlag) {
            //pc端
            equipment = "pc";
        } else {
            equipment = "mobile";
        }
        if (user != null) {
            logInfo.setUserid(String.valueOf(user.getId()));
            logInfo.setUsername(user.getUsername());
        }
        logInfo.setIPAddress(IPUtils.getRemoteIp(request));
        logInfo.setOperatetime(operateTime);
        logInfo.setDevice(equipment);
        logInfoDao.addLogInfo(logInfo);
    }
}
/**
 * 操作常量
 */
public class ActionConstants {

    public static final String LOGIN = "登录";

    public static final String LOGOUT = "注销";

    public static final String INSERT = "新增操作";

    public static final String UPDATE = "更新操作";

    public static final String DELETE = "删除操作";

    public static final String UPLOAD = "上传文件";

    public static final String HANDLE = "办理";
}
/**
 * 模块常量
 */
public class ModuleConstants {

    public static final String USER = "用户";
    public static final String ARTICLE = "文章";
    public static final String CATEGORY = "类别";
    public static final String KEYWORD = "关键字";

}
/**
 * 登录用户
 */
@SystemServiceLog(action = ActionConstants.LOGIN)
@RequestMapping("/loginUser")
@ResponseBody
public String loginUser(HttpServletRequest request, String username, String password) {
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        User login = userService.login(username, password);
        if (login != null) {
            request.getSession().setAttribute("user", login);
            return "success";
        }
    }
    return "fail";
}

/**
 * 注销登录
 */
@SystemServiceLogBefore(action = ActionConstants.LOGOUT)
@RequestMapping("/outLogin")
public String outLogin(HttpServletRequest request) {
    request.getSession().setAttribute("user", null);
    return "redirect:/login/login";
}

欢迎转载,转载请注明出处 http://www.dingyinwu.com/article/69.html

如果文章中有任何问题或者可以改进的地方,请大家多提提意见,我会非常感激。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值