你的位置:首页 > Java教程

[Java教程]Appfuse:记录操作日志


appfuse的数据维护操作都发生在***form页面,与之对应的是***FormController,在Controller中处理数据的操作是onSubmit方法,既然所有的操作都通过onSubmit,那么只需要拦截onSubmit并记录对应的参数即可作为操作日志。

首先要拦截Controller层的方法就需要在dispatcher-servlet.

<aop:config>    <aop:advisor id="submitTx" advice-ref="txSubmitAdvice"      pointcut="execution(* *..controller.*Controller.onSubmit(..))" order="0" />  </aop:config>  <bean id="txSubmitAdvice" class="com.zcmp.xunji.service.SubmitAdvice"/>

dispatcher-servlet.

接下来在Service中定义自己的AOP拦截器

package com.disappearwind.service;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.aop.AfterReturningAdvice;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.security.core.Authentication;import org.springframework.security.core.context.SecurityContext;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.security.core.userdetails.UserDetails;import com.disappearwind.model.BaseObject;import com.disappearwind.model.Role;import com.disappearwind.model.Syslog;import com.disappearwind.model.User;import java.lang.reflect.Method;import java.util.Date;import java.util.Set;import javax.servlet.http.HttpServletRequest;/** * 对Controller中的onSubmit方法监听,记录用户的操作日志 * * @author mraible */public class SubmitAdvice implements AfterReturningAdvice, MethodInterceptor {  private final Log log = LogFactory.getLog(SubmitAdvice.class);  private UserManager userManager = null;  private GenericManager<Syslog, Long> syslogManager;  @Autowired  public void setSyslogManager(      @Qualifier("syslogManager") GenericManager<Syslog, Long> syslogManager) {    this.syslogManager = syslogManager;  }  @Autowired  public void setUserManager(UserManager userManager) {    this.userManager = userManager;  }  /**   * 在用户的业务操作执行完之后记录操作日志   *   * @param returnValue   *      the user object   * @param method   *      the name of the method executed   * @param args   *      the arguments to the method   * @param target   *      the target class   * @throws Throwable   *       thrown when args[0] is null or not a Base object   */  public void afterReturning(Object returnValue, Method method,      Object[] args, Object target) throws Throwable {    // 方法签名:String onSubmit(AppToken appToken, BindingResult errors,    // HttpServletRequest request,HttpServletResponse response)    try {      if (args[0] instanceof BaseObject) {        BaseObject obj = (BaseObject) args[0];        HttpServletRequest request = (HttpServletRequest) args[2];        // 发生在哪个Model        String modelName = obj.getClass().getSimpleName();        // Model中的数据        String modelData = obj.toString();        // 操作类型,request参数中sava就是Save,request参数中有delete则是Delete        String action = "Save";        if (null != request.getParameter("delete")) {          action = "Delete";        }        // 记录操作日志到数据库        Syslog syslog = new Syslog();        syslog.setModule(modelName + "." + action);        syslog.setContent(modelData);        syslog.setCreateDate(new Date());        syslog.setCreator(getCurrentUserID());        syslogManager.save(syslog);      }    } catch (Exception ex) {      log.error(ex);    }  }  /**   * 获取当前操作的用户   *   * @return 用户ID   */  private Long getCurrentUserID() {    Long userID = 0l;    SecurityContext ctx = SecurityContextHolder.getContext();    if (ctx.getAuthentication() != null) {      Authentication auth = ctx.getAuthentication();      UserDetails user = (UserDetails) auth.getPrincipal();      userID = this.userManager.getUserByUsername(user.getUsername())          .getId();    }    return userID;  }}

SubmitAdvice

注意,其中有一段是获取当前登录用户的ID的getCurrentUserID。

Syslog是自己定义的存储日志的数据结构。