一、xml配置
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> <bean id="logAspect" class="com.ucfgroup.framework.web.app.aspect.LogAspect" /> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut id="pointcut" expression="execution(* com.ucfgroup.framework.web.app.*.controller.*.*(..))" /> <aop:aspect order="1" ref="logAspect"> <!-- 前置通知 --> <aop:before method="logManager" pointcut-ref="pointcut" /> </aop:aspect> </aop:config>
二、java代码
package com.ucfgroup.framework.web.app.aspect; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.util.StringUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.ucfgroup.framework.utils.WebUtil; import com.ucfgroup.framework.web.app.personal.entity.PUserZt; import com.ucfgroup.framework.web.app.usrmgmt.dao.TOperLogMapper; import com.ucfgroup.framework.web.app.usrmgmt.entity.TOperLog; import com.ucfgroup.framework.web.app.usrmgmt.entity.TuUser; import com.ucfgroup.framework.web.log.model.OperLog; public class LogAspect { @Autowired private TOperLogMapper tOperLogMapper; protected Logger LOG = Logger.getLogger(getClass()); public void logManager(JoinPoint joinPoint) throws Exception { // 记录日志失败不影响业务继续 try { // // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); HttpSession session = request.getSession(); PUserZt zt = (PUserZt) session.getAttribute("ZTModel"); TuUser tuUser = (TuUser) session.getAttribute("USER_INFO"); // check if user is login Authentication auth = SecurityContextHolder.getContext().getAuthentication(); // 记录日志失败不影响业务继续 String method = joinPoint.getSignature().getName(); TOperLog tOperLog = new TOperLog(); if (null != tuUser) { tOperLog.setUsername(tuUser.getUsername()); } if (null != zt) { // log.setBranchNo(zt.getBranchNo()); tOperLog.setClientId(zt.getZtid()); } tOperLog.setBrowserAgent(request.getHeader("user-agent")); tOperLog.setIpAddress(WebUtil.getIpAddr(request)); tOperLog.setMethod(StringUtils.hasText(method) ? method.replace("do", "").toUpperCase() : ""); tOperLog.setOperationTime(new Date(System.currentTimeMillis())); tOperLog.setReqMsg(getReqMsg(request)); tOperLog.setResUrl(request.getServletPath()); if ("GET".equals(tOperLog.getMethod()) || "HEAD".equals(tOperLog.getMethod()) || "OPTIONS".equals(tOperLog.getMethod())) { tOperLog.setLogType(OperLog.ACCESS_LOG); } else { tOperLog.setLogType(OperLog.UPDATE_LOG); } tOperLogMapper.insert(tOperLog); } catch (Throwable e) { LOG.error("Save log failed: " + e.getMessage(), e); } } private String getReqMsg(HttpServletRequest request) { Map<String, String> paraMap = getReqParams(request); StringBuilder parameterStr = new StringBuilder(); boolean first = true; for (Entry<String, String> entry : paraMap.entrySet()) { if (!first) { parameterStr.append("&"); } else { first = false; } String value = entry.getValue(); parameterStr.append(entry.getKey()).append("="); // 隐藏 password 域 if (entry.getKey().toLowerCase().contains("password")) { parameterStr.append("********"); } else { parameterStr.append(value); } } return parameterStr.toString(); } private Map<String, String> getReqParams(HttpServletRequest request) { Map<String, String> reqParams = new HashMap<String, String>(); // trim the parameters Iterator<String> it = request.getParameterMap().keySet().iterator(); while (it.hasNext()) { String key = it.next(); // 密码不做trim() if (key.toLowerCase().contains("password")) { reqParams.put(key, request.getParameter(key)); } else { reqParams.put(key, request.getParameter(key).trim()); } } return reqParams; } }
三、总结
Aop的内容非常丰富,由于时间原因,我先将亲测可行的代码记录下来,以后我会慢慢丰富其内容的,毕竟知识越辨越明。