用户操作日志 (思路 + 代码)

操作日志详解

实体类
package com.cmfz.entity;

import lombok.Data;
import java.io.Serializable;
import java.util.Date;

@Data
public class AdminLog implements Serializable {

/*    `log_id` int(11) NOT NULL AUTO_INCREMENT,
  `log_date` datetime NULL DEFAULT NULL,
  `admin_id` int(11) NULL DEFAULT NULL,
  `log_content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `log_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `log_ip` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
        */

    private Integer logId;
    private Date logDate;
    private Integer adminId;
    private String logContent;
    private String logType;
    private String logIp;
}
DAO中的方法
添加 批量删除 查询
package com.cmfz.dao;

import com.cmfz.entity.AdminLog;
import java.util.List;

public interface AdminLogDAO {
    void insertAdminLog(AdminLog adminLog);
    void deleteAdminLogAll(Integer[] ids);
    List selectAdminLog();
}

Mapper.xml文件中的方法





    
        
        
        
        
        
        
    

    
        insert into cmfz_admin_log values(default,#{logDate},#{adminId},#{logContent},#{logType},#{logIp})
    

    
        delete from cmfz_admin_log where log_id in
        
            #{logId}
        
    

    


service接口
package com.cmfz.service;

import com.cmfz.entity.AdminLog;
import com.github.pagehelper.PageInfo;

public interface AdminLogService {
    void deleteAdminLogAll(Integer[] ids);
    PageInfo selectAdminLog(Integer page, Integer limit);
}
serviceImpl 实现类
package com.cmfz.service.impl;

import com.cmfz.dao.AdminLogDAO;
import com.cmfz.entity.AdminLog;
import com.cmfz.service.AdminLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AdminLogServiceImpl implements AdminLogService {
    @Autowired
    private AdminLogDAO adminLogDAO;
    @Override
    public void deleteAdminLogAll(Integer[] ids) {
        adminLogDAO.deleteAdminLogAll(ids);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo selectAdminLog(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo<>(adminLogDAO.selectAdminLog());
    }
}
Controller 控制器
package com.cmfz.controller;

import com.cmfz.entity.AdminLog;
import com.cmfz.service.AdminLogService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("adminLog")
public class AdminLogController {
    @Autowired
    private AdminLogService adminLogService;

    @ResponseBody
    @RequestMapping("showAdminLog")
    public Map showAdminLog(Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo adminPageInfo = adminLogService.selectAdminLog(page, limit);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", adminPageInfo.getTotal());
        map.put("data", adminPageInfo.getList());
        return map;
    }

    @ResponseBody
    @RequestMapping("deleteAdminLog")
    public Map deleteAdminLog(Integer[] ids){
        Map map = new HashMap();
        try{
            map.put("isDelAll", true);
            adminLogService.deleteAdminLogAll(ids);
        }catch (Exception e){
            e.printStackTrace();
            map.put("isDelAll", false);
        }
        return map;
    }

}

重点来了

操作日志实现重点

自定义注解类
package com.cmfz.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
    String content() default "";

    String type() default "select";
}
增强类(额外功能类) 中含 日志添加到数据库功能
package com.cmfz.aspect;

import com.cmfz.annotation.LogAnnotation;
import com.cmfz.dao.AdminLogDAO;
import com.cmfz.entity.Admin;
import com.cmfz.entity.AdminLog;
import com.cmfz.util.IPKit;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

@Configuration  //声明当前类为配置类,相当于xml文件
@Aspect
public class LogAop {
    @Autowired
    private AdminLogDAO adminLogDAO;
    /*
    * 创建切入点
    * 创建增强
    *
    * 织入
    * */
    //切入点
    @Pointcut("@annotation(com.cmfz.annotation.LogAnnotation)")
    public void PointCut(){}

    // 日志 后置增强
    //参数为切入点的名字
    //ProceedingJoinPoint 连接点对象,切点对象,(包含切点附近所有的内容) 目标方法
    @After("PointCut()")
    public void logAfter(JoinPoint joinPoint){

        //获取日志数据
        //创建对象
        AdminLog adminLog = new AdminLog();

//-----------------------------------------------------------获取时间-------------
        //获取时间
        adminLog.setLogDate(new Date());

//-----------------------------------------------------------获取Id----------------
        //获取Id
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //获取Request
        HttpServletRequest request = requestAttributes.getRequest();
        //获取Session
        HttpSession session = request.getSession();
        //从session中获取数据
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin != null){
            adminLog.setAdminId(admin.getId());
            //---------------------------------------------------------------获取ip-------------
            //获取ip
            adminLog.setLogIp(IPKit.getIpAddrByRequest(request));

//---------------------------------------------------------------获取方法上注解的内容-------------

            //获取方法的签名对象
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            //获取方法对象
            Method method = signature.getMethod();
            //获取方法上的注解
            LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
            //把注解上的值存入日志表中
            adminLog.setLogType(annotation.type());
            adminLog.setLogContent(annotation.content());

//---------------------------------------------------------------添加到数据库-------------
            //添加到数据库
            adminLogDAO.insertAdminLog(adminLog);
        }else {
            System.out.println("admin为空");
        }

    }

}

在登陆方法上添加注解
@LogAnnotation(content = "管理员登陆",type = "login")

例:

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @RequestMapping("/login")
    
    @LogAnnotation(content = "管理员登陆",type = "login")
    
    public String loginAdmin(String code, String username, String password){
        Session session = SecurityUtils.getSubject().getSession();
        ICaptcha iCaptcha = (ICaptcha) session.getAttribute("iCaptcha");

        if(!iCaptcha.verify(code)){
            return "login";
        }else {
            try{
                // 1.将页面输入的账号密码封装到令牌中
                UsernamePasswordToken token = new UsernamePasswordToken(username, password);
                // 2.获取主体 调用主体的login方法
                Subject subject = SecurityUtils.getSubject();
                subject.login(token);
                return "main";
            }catch (Exception e){
                return "login";
            }
        }
    }

你可能感兴趣的:(用户操作日志 (思路 + 代码))