首页>代码>Spring MVC4.3.5+MyBatis3.4.2+Apache Shiro1.3.2整合开发高仿小米商城的后台用户管理系统>/mi-cms/src/main/java/com/chen/common/aop/SysLogAspect.java
package com.chen.common.aop;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.chen.common.SysLog;
import com.chen.common.security.AuthorizingUser;
import com.chen.common.util.ServletUtils;
import com.chen.entity.Log;
import com.chen.service.ILogService;
/**
*
* 类名称:SysLogAspect
* 类描述:SysLogAspect 系统日记记录
* 创建人:chenguoji
* 创建时间:2017-10-25 上午10:47:15
*
*/
@Aspect
@Component
public class SysLogAspect {
private static final Logger LOGGER = LogManager.getLogger(SysLogAspect.class);
/**
* 开始时间
*/
private long startTime = 0L;
/**
* 结束时间
*/
private long endTime = 0L;
@Autowired
private ILogService logService;
@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void cutController() {}
@Before("cutController()")
public void doBeforeInServiceLayer(JoinPoint joinPoint) {
LOGGER.debug("doBeforeInServiceLayer");
startTime = System.currentTimeMillis();
}
@After("cutController()")
public void doAfterInServiceLayer(JoinPoint joinPoint) {
LOGGER.debug("doAfterInServiceLayer");
}
@Around("cutController()")
public Object recordSysLog(ProceedingJoinPoint joinPoint) throws Throwable {
// 请求的方法名
String strMethodName = joinPoint.getSignature().getName();
// 请求的类名
String strClassName = joinPoint.getTarget().getClass().getName();
// 请求的参数
Object[] params = joinPoint.getArgs();
StringBuffer bfParams = new StringBuffer();
Enumeration<String> paraNames = null;
HttpServletRequest request = null;
if (params != null && params.length > 0) {
request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
paraNames = request.getParameterNames();
String key;
String value;
while (paraNames.hasMoreElements()) {
// 遍历请求参数
key = paraNames.nextElement();
value = request.getParameter(key);
bfParams.append(key).append("=").append(value).append("&");
}
if (StringUtils.isBlank(bfParams)) {
// 如果请求参数为空,返回url路径后面的查询字符串
bfParams.append(request.getQueryString());
}
}
String strMessage = String.format("[类名]:%s,[方法]:%s,[参数]:%s", strClassName, strMethodName, bfParams.toString());
LOGGER.info(strMessage);
// 环绕通知 ProceedingJoinPoint执行proceed方法的作用是让目标方法执行,这也是环绕通知和前置、后置通知方法的一个最大区别。
Object result = null;
try {
result = joinPoint.proceed();
endTime = System.currentTimeMillis();
LOGGER.debug("doAround>>>result={},耗时:{}", result, endTime - startTime);
} catch (Exception e) {
e.printStackTrace();
}
// 判断是否需要日记记录
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SysLog syslog = method.getAnnotation(SysLog.class);
if (syslog == null || syslog.isLog()) {
Subject currentUser = SecurityUtils.getSubject();
AuthorizingUser user = (AuthorizingUser) currentUser.getPrincipal();
if (null != user) {
Log sysLog = new Log();
sysLog.setCreateTime(new Date());
sysLog.setUserId(user.getUserId());
sysLog.setOptContent(strMessage);
sysLog.setUserIp(ServletUtils.getIpAddr());
sysLog.setUrl(request.getRequestURI());
sysLog.setMethod(request.getMethod());
sysLog.setUserAgent(request.getHeader("User-Agent"));
sysLog.setSpendTime((int) (endTime - startTime));
logService.insert(sysLog);
}
}
return result;
}
}
最近下载更多
微信网友_6248713511227392 LV11
2022年12月5日
秋枫花落叶 LV2
2022年10月24日
liuxiao2 LV16
2022年10月13日
微信网友_5966087716769792 LV9
2022年10月11日
hkxyyz LV6
2022年5月11日
893213895 LV18
2021年12月16日
543666826 LV34
2021年11月20日
tanglitao LV6
2021年10月19日
2715406348 LV3
2021年5月21日
啊啊DVD v但是 LV4
2021年3月29日

最近浏览