• Spring aop 记录操作日志


    /**
    * 此为系统AOP切面的配置类
    * 在spring-context.xml中引入些配置类
    * @author Sai
    * @since 2016-11-15
    */
    @Configuration
    @EnableAspectJAutoProxy(proxyTargetClass=true)//启用AspectJ自动代理
    @ComponentScan(basePackages = "com.bld.core.supervisor")//默认扫描与配置类相同的包下的@Component注解下的bean,想自动装配AOP切面,必需加上@Component
    public class AopConfig {

    //显示装配切面的bean
    @Bean
    public UserAop userAop(){
    return new UserAop();
    }
    @Bean
    public SupervisorEventsAop supervisorEventsAop(){
    return new SupervisorEventsAop();
    }
    @Bean
    public CpsRewardCaculateAop cpsRewardCaculateAop(){ return new CpsRewardCaculateAop();}
    }

    在spring-context.xml中配置
    <!-- 引入AOP切面配置类 -->
    <bean class = "com.bld.core.sys.config.AopConfig" />


    /**
    * 自定义注解,用来标识方法所处理的业务
    * Created by Administrator on 2017/2/22.
    */
    @Target({ElementType.FIELD, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface SupervisorEvent {
    String description() default "no description";//操作信息
    String moduleId() default "";//菜单id(from SupervisorAuthorize)
    }
    /**
    * 管理员事件aop
    * Created by Administrator on 2017/2/22.
    */
    @Aspect
    @Component
    public class SupervisorEventsAop {
    @Autowired
    private SupervisorEventsMapper supervisorEventsMapper;
    @Autowired
    private ModuleMapper moduleMapper;
    @Autowired
    private SupervisorMapper supervisorMapper;
    @Autowired
    private RoleMapper roleMapper;

    //没有目标对象的用注解标注进行记录
    @After("@annotation(com.bld.common.annotation.SupervisorEvent)")
    public void addManagerEvents(JoinPoint joinPoint){
    String menthodName = joinPoint.getSignature().getName();
    Class c = joinPoint.getSignature().getDeclaringType();
    Object[] args = joinPoint.getArgs();
    Class[] classes = null;
    if (args!=null) {
    classes = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
    classes[i] = args[i].getClass();
    }
    }
    Method method = null;
    try {
    method = c.getMethod(menthodName, classes);
    } catch (NoSuchMethodException e) {
    throw new BusinessException("记录后台管理员事件失败:"+e.getMessage());
    }
    SupervisorEvent supervisorEvent = method.getAnnotation(SupervisorEvent.class);
    if (supervisorEvent != null) {
    String description = supervisorEvent.description();
    String moduleInfo = null;
    if (StringUtil.isNotEmpty(supervisorEvent.moduleId())){
    moduleInfo = "";
    int moduleId = Integer.parseInt(supervisorEvent.moduleId());
    String info = moduleInfo(moduleId,moduleInfo);
    moduleInfo = info.substring(0,info.length()-2);
    }
    Date date = new Date();
    String ip = RequestUtil.getIpAddr();
    MSupervisorEvents mSupervisorEvents = new MSupervisorEvents();
    mSupervisorEvents.setTime(date);
    mSupervisorEvents.setModuleInfo(moduleInfo);
    mSupervisorEvents.setDescrption(description);
    if(null!=ip){
    mSupervisorEvents.setIp(ip);
    }
    int supervisorId = 1;
    if(null!=SecurityUtil.getAdminCurrentUser()){
    supervisorId = Integer.parseInt(SecurityUtil.getAdminCurrentUser().getUserId());
    }
    mSupervisorEvents.setSupervisorId(supervisorId);
    int result = supervisorEventsMapper.insertSelective(mSupervisorEvents);
    if(result!=1){
    throw new BusinessException("保存管理员事件失败!");
    }
    }
    }

    /**
    * 系统设置--》管理员管理
    */
    // //添加管理员
    @After("execution(* com.bld.core.supervisor.service.impl.SupervisorManageServiceImpl.addSupervisor(..))")
    public void addManagerEventsForAddSupervisor(JoinPoint joinPoint){
    //获取目标方法的参数
    Object[] args = joinPoint.getArgs();
    BLDAssert.isEmpty(args[0],"添加管理员service方法第一个参数为null");
    MSupervisor mSupervisors = (MSupervisor) args[0];
    addToDateBase(ADD_SUPERVISOR+"'"+mSupervisors.getName()+"'", SUPERVISOR_LIST, ADD_SUPERVISOR);
    }

    //删除管理员
    @Before("execution(* com.bld.core.supervisor.service.impl.SupervisorManageServiceImpl.deleteSupervisor(..))")
    public void addManagerEventsForDeleteSupervisor(JoinPoint joinPoint){
    supervisorManagerCommon(joinPoint,DELETE_SUPERVISOR);
    }

    //修改管理员
    @After("execution(* com.bld.core.supervisor.service.impl.SupervisorManageServiceImpl.editSupervisor(..))")
    public void addManagerEventsForEditSupervisor(JoinPoint joinPoint){
    supervisorManagerCommon(joinPoint,EDIT_SUPERVISOR);
    }

    //锁定管理员
    @After("execution(* com.bld.core.supervisor.service.impl.SupervisorManageServiceImpl.lockSupervisor(..))")
    public void addManagerEventsForLockSupervisor(JoinPoint joinPoint){
    supervisorManagerCommon(joinPoint,LOCKED_SUPERVISOR);
    }

    //解锁管理员
    @After("execution(* com.bld.core.supervisor.service.impl.SupervisorManageServiceImpl.unLockSupervisor(..))")
    public void addManagerEventsForUnLockSupervisor(JoinPoint joinPoint){
    supervisorManagerCommon(joinPoint,UNLOCKED_SUPERVISOR);
    }

    //重置密码管理员
    @After("execution(* com.bld.core.supervisor.service.impl.SupervisorManageServiceImpl.resetPassword(..))")
    public void addManagerEventsForResetPasswordSupervisor(JoinPoint joinPoint){
    supervisorManagerCommon(joinPoint,RESET_PASSWORD);
    }

    //管理员删除、修改、锁定、解锁、重置密码相同代码抽取
    private void supervisorManagerCommon(JoinPoint joinPoint,String type){
    Object[] args = joinPoint.getArgs();
    BLDAssert.isEmpty(args[0],type+"service方法第一个参数为null");
    int id = (int) args[0];
    MSupervisor mSupervisor = supervisorMapper.selectByPrimaryKey(id);
    addToDateBase(type+"'"+mSupervisor.getName()+"'", SUPERVISOR_LIST, type);
    }

    /**
    * 系统设置--》权限管理
    */
    //添加角色
    @After("execution(* com.bld.core.supervisor.service.impl.RoleManageServiceImpl.addRole(..))")
    public void addManagerEventsForAddRole(JoinPoint joinPoint){
    //获取目标方法的参数
    Object[] args = joinPoint.getArgs();
    BLDAssert.isEmpty(args[0],"添加角色service方法第一个参数为null");
    String name = (String) args[0];
    addToDateBase(ADD_ROLE+"'"+name+"'", ROLE_MANAGE, ADD_ROLE);
    }

    //删除角色
    @Before("execution(* com.bld.core.supervisor.service.impl.RoleManageServiceImpl.deleteRole(..))")
    public void addManagerEventsForDeleteRole(JoinPoint joinPoint){
    roleManagerCommon(joinPoint,DELETE_ROLE);
    }

    //修改角色
    @After("execution(* com.bld.core.supervisor.service.impl.RoleManageServiceImpl.editRole(..))")
    public void addManagerEventsForEditRole(JoinPoint joinPoint){
    roleManagerCommon(joinPoint,EDIT_ROLE);
    }

    //角色删除、修改相同代码抽取
    private void roleManagerCommon(JoinPoint joinPoint,String type){
    Object[] args = joinPoint.getArgs();
    BLDAssert.isEmpty(args[0],type+"service方法第一个参数为null");
    int id = (int) args[0];
    MRole mRole = roleMapper.selectByPrimaryKey(id);
    addToDateBase(type+"'"+mRole.getName()+"'", ROLE_MANAGE, type);
    }

    /**
    * 操作日志管理
    */
    //删除操作日志
    @After("execution(* com.bld.core.supervisor.service.impl.OperationRecordServiceImpl.delete(..))")
    public void addManagerEventsForDeleteRecord(JoinPoint joinPoint){
    Object[] args = joinPoint.getArgs();
    BLDAssert.isEmpty(args[0],"OperationRecordService方法第一个参数为null");
    int code = (int) args[0];
    if(code == 0 || code == 1 ||code == 2 ) {
    String type = "";
    switch (code){
    case 0:
    type = "(本周前)";
    break;
    case 1:
    type = "(本月前)";
    break;
    case 2:
    type = "(全部)";
    break;
    default:
    break;
    }
    addToDateBase(DELETE_RECORD+type, DELETE_OPERATE_RECORD, DELETE_RECORD);
    }

    }

    //添加管理员事件到数据库
    private void addToDateBase(String description, String moduleId, String type) {
    String info = moduleInfo(Integer.parseInt(moduleId),"");
    String moduleInfo = info.substring(0,info.length()-2);
    int supervisorId = 1;
    if(null!=SecurityUtil.getAdminCurrentUser()){
    supervisorId = Integer.parseInt(SecurityUtil.getAdminCurrentUser().getUserId());
    }
    MSupervisorEvents mSupervisorEvents = new MSupervisorEvents();
    mSupervisorEvents.setSupervisorId(supervisorId);
    mSupervisorEvents.setTime(new Date());
    mSupervisorEvents.setIp(RequestUtil.getIpAddr());
    mSupervisorEvents.setModuleInfo(moduleInfo);
    mSupervisorEvents.setDescrption(description);
    int result = supervisorEventsMapper.insertSelective(mSupervisorEvents);
    if(result!=1){
    throw new BusinessException("保存'"+type+"'事件失败!");
    }

    }

    //递归组合菜单信息
    private String moduleInfo(int moduleId,String description){
    MModule mModule = moduleMapper.selectByPrimaryKey(moduleId);
    if (mModule==null){throw new BusinessException("菜单不存在");}
    description = mModule.getModuleName()+"->"+description;
    if(mModule.getParentId()!=0){
    description = moduleInfo(mModule.getParentId(),description);
    }
    return description;
    }

    在要记录的方法上加
    @Transactional(isolation = Isolation.SERIALIZABLE)
    @com.bld.common.annotation.SupervisorEvent(description ="描述",moduleId = "菜单id")
  • 相关阅读:
    使用python将文字写入word文档中
    将图片显示到excel中
    新的写入xlsxwriter和追加写入openpyxl
    oracle 12.2 alter table move online
    主从复制管理和故障处理方法
    MySQL中的权限管理
    windows的CMD如何全屏最大化
    Troubleshooting query v$asm_disk v$asm_diskgroup hang
    library cache锁争用解决
    一则由ORA-1652引起的fixed object相关问题
  • 原文地址:https://www.cnblogs.com/sasss/p/7456994.html
Copyright © 2020-2023  润新知