使用Aop面向切面技术实现记录详细操作日志功能

package com.jjznkj.oa.advice;

import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.Date;



import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;

import com.jjznkj.oa.base.BaseAction;
import com.jjznkj.oa.domain.Account;
import com.jjznkj.oa.domain.OperatorLog;
import com.jjznkj.oa.utills.GetMacAdressUtil;
import com.opensymphony.xwork2.ActionContext;


/**
 * 
 * @author admin
 *
 */
@Component
public class LogAdvice extends BaseAction
{

	public void after(JoinPoint jp) 
	{
		//获取登录后的账号
		Account account = (Account) ActionContext.getContext().getSession().get("account");
	
		if(account != null)
		{
			try
			{
			//获取登录用户的Id
			Long userId = account.getId();
			//获取方法名
			String methodName = jp.getSignature().getName();
			
			//获取操作内容
			String opContent;
			
			opContent = userOptionContent(jp.getArgs(), methodName);
		
			
			//创建日志对象
			OperatorLog operatorLog = new OperatorLog();
			operatorLog.setUserId(userId);
			operatorLog.setCreateDate(new Date());
			operatorLog.setContent(opContent);
			if(methodName.equals("login"))
			{
				operatorLog.setOperation(account.getName()+"进行了登录操作");
			}
			else if (methodName.equals("add"))
			{
				operatorLog.setOperation(account.getName()+"进行了添加操作");
			}else if(methodName.equals("edit")){
				operatorLog.setOperation(account.getName()+"进行了修改操作");
			}
			else if (methodName.equals("delete")) {
				operatorLog.setOperation(account.getName()+"进行了删除操作");
			}
			
			operatorLog.setIp(ServletActionContext.getRequest().getRemoteAddr());
			
//			operatorLog.setMacAddress(GetMacAdressUtil.getMac());
			operatorLog.setMacAddress(GetMacAdressUtil.getLocalMac(InetAddress.getLocalHost()));
			
			operatorLogService.save(operatorLog);
			
			
			//设置当前登录用户对应的日志
			account.setLogId(operatorLog.getId());
			//存入Session
			ActionContext.getContext().getSession().put("account", account);
			
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
		}
	}
	public void before(JoinPoint jPoint)
	{
		//获取登录用户
		
		Account account = (Account) ActionContext.getContext().getSession().get("account");
		if(account != null)
		{
			//获取当前账户对应的日志
			OperatorLog operatorLog = operatorLogService.getById(account.getLogId());
			//设置属性
			operatorLog.setEnddata(new Date());
			
			//更新到数据库中
			operatorLogService.update(operatorLog);
			
			
		}
		
	}
	
	
	
	
	
	
	 /** 
     * 使用Java反射来获取被拦截方法(save、update)的参数值, 
     * 将参数值拼接为操作内容 
     */ 
	private String userOptionContent(Object[] args, String mName) throws Exception
	{
		
		if(args == null)
		{
			return null;
			
		}
		
		StringBuffer rs = new StringBuffer();
		rs.append(mName);
		String className = null;
		int index = 1;
		//遍历参数对象
		
		for(Object info : args)
		{
			//获取对象类型
			className = info.getClass().getName();
			className = className.substring(className.lastIndexOf(".")+1);
			rs.append("[参数"+index+",类型:"+className+",值:");
			
			//获取对象的所有方法
			Method[] methods = info.getClass().getDeclaredMethods();
			
			//遍历方法
			for(Method method : methods)
			{
				String methodName = method.getName();
				//判断是不是get方法
				if(methodName.indexOf("get")==-1)
				{
					//不是get方法
					continue;
				}
				
				Object rsValue = null;
				try
				{
					//调用get方法,获取返回值
					rsValue = method.invoke(info);
					
					if(rsValue == null)
					{
						//没有返回值
						continue;
					}
					
					
				} catch (Exception e)
				{
					// TODO: handle exception
					continue;
				}
				
				//将值加入内容中
				rs.append("("+methodName+":"+rsValue+")");
				
			}
			rs.append("]");
			index++;
			
		}
		
		
		return rs.toString();
		
		
	}








	public String execute() throws Exception
	{
		// TODO Auto-generated method stub
		return null;
	}

}

    
	
    
		
     
			
      
			
      
		
     
		
     
			
      
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
			
      
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      			
			
			
      
			
      
			
      	
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
      
			
      
			
      
			
			
			
		
		
		
     
package com.jjznkj.oa.domain;

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

public class OperatorLog implements Serializable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7037562984067907675L;
	
	private Long id;
	
	private Long userId;
	
	private Date createDate;//日期
	/**
	 * 离开时间 
	 */
	private Date  enddata; 
	
	

	private String content;//日志内容
	private String Ip;
	private String macAddress;
	private String operation;//操作
	//Mac地址

	/**
	 * 所属账号
	 */
	private Account  account;
	
	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public Long getUserId()
	{
		return userId;
	}

	public void setUserId(Long userId)
	{
		this.userId = userId;
	}

	public Date getCreateDate()
	{
		return createDate;
	}

	public void setCreateDate(Date createDate)
	{
		this.createDate = createDate;
	}

	public String getContent()
	{
		return content;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	public String getOperation()
	{
		return operation;
	}

	public void setOperation(String operation)
	{
		this.operation = operation;
	}

	public String getIp()
	{
		return Ip;
	}

	public void setIp(String ip)
	{
		Ip = ip;
	}

	public Account getAccount()
	{
		return account;
	}

	public void setAccount(Account account)
	{
		this.account = account;
	}
	
	public Date getEnddata()
	{
		return enddata;
	}

	public void setEnddata(Date enddata)
	{
		this.enddata = enddata;
	}

	public String getMacAddress()
	{
		return macAddress;
	}

	public void setMacAddress(String macAddress)
	{
		this.macAddress = macAddress;
	}
	
	
	
}


    

你可能感兴趣的:(使用Aop面向切面技术实现记录详细操作日志功能)