先写拦截器代码
package com.dlcloud.oa.erp.aop; import com.alibaba.fastjson.JSON; import com.dlcloud.oa.common.constant.BehaviorEnum; import com.dlcloud.oa.erp.admin.entity.AdminUser; import com.dlcloud.oa.erp.work.entity.OperateLog; import com.dlcloud.oa.utils.BaseUtil; import com.dlcloud.oa.utils.BehaviorEnumlUtils; import com.dlcloud.oa.utils.R; import com.jfinal.aop.Interceptor; import com.jfinal.aop.Invocation; import com.jfinal.core.Controller; import com.jfinal.log.Log; import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; import java.util.Map; public class BehaviorLogInterceptor implements Interceptor { @Override public void intercept(Invocation invocation) { invocation.invoke(); try { Controller controller = invocation.getController(); HttpServletRequest request = controller.getRequest(); String methodName = invocation.getMethodName();//方法名称 String url = request.getRequestURL().toString();//请求的url BehaviorEnum behaviorEnum = null;//拦截协议 if (StringUtils.isNoneBlank(url)) { behaviorEnum = BehaviorEnumlUtils.getBehaviorEnumByUrl(url); } if (null != behaviorEnum) {//找到拦截协议 String addr = request.getRemoteAddr(); String ip = request.getRemoteHost(); int port = request.getRemotePort(); AdminUser userInfo = BaseUtil.getUser();//用户信息 userInfo.getUserId();//用户id userInfo.getRoles();//用户角色 String paramContent = getRequestBody(invocation, controller, request);//请求参数 OperateLog operateLog = new OperateLog(); operateLog.setRealname(userInfo.getRealname());//用户姓名 operateLog.setMethod(methodName);//方法名称 // paramContent=2000paramContent = new HashMap (); paramContent.put("传递参数", request.getParameterMap()); if (StringUtils.isNoneBlank(jsonData)) { paramContent.put("发送参数", jsonData); } return JSON.toJSONString(paramContent); } }
——————————————————————————————————
然后在配置类里面配置一下就齐活了