对功能的监控,操作信息记录(参考:本例是对用户的操作记录)

package com.zc.spring.aop;
import com.alibaba.fastjson.JSON;
import com.zc.entity.sys.User;
import com.zc.entity.sys.UserLog;
import com.zc.service.sys.UserOperationService;
import com.zc.spring.filter.CommonConstant;
import com.zc.util.DateUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 用户操作切面配置
* 功能说明: 用户操作后,记录操作信息
*
* @author caoliang
* @date 2018/2/24
*/
@Aspect
@Component
public class UserOperationAop {
@Autowired
private UserOperationService userOperationService;
/**
* 日志
*/
private final static Logger logger = LoggerFactory.getLogger(UserOperationAop.class);
/**
* 判断是否新增信息
*/
public static Map<String, LinkedHashMap<String, Object>> userBeanMap = new HashMap<>(16);
 
@Pointcut("execution(* com.zc.controller.sys.UserController.insertuser(..))")
public void insertUser() {
// 新增账户
}
@Pointcut("execution(* com.zc.controller.sys.UserController.updateuser(..))")
public void updateUser() {
// 修改账号
}
//注解到具体的方法
@Pointcut("execution(* com.zc.controller.sys.UserController.deleteuser(..))")
public void deleteUser() {
// 删除账号
}
 
 
//实现对具体任务的日志监控
@Around("insertUser()")
public void insert(ProceedingJoinPoint pjp) throws Throwable {
UserLog userLog = new UserLog();
userLog.setType("新增");
doAround(pjp, userLog);
}
@Around("updateUser()")
public void update(ProceedingJoinPoint pjp) throws Throwable {
UserLog userLog = new UserLog();
userLog.setType("修改");
doAround(pjp, userLog);
}
 
 
@Around("deleteUser()")
public void delete(ProceedingJoinPoint pjp) throws Throwable {
UserLog userLog = new UserLog();
userLog.setType("删除");
//具体的操作
doAround(pjp, userLog);
}
 
 
 
public Object doAround(ProceedingJoinPoint pjp, UserLog userLog) throws Throwable {
Object result = null;
try {
// 将操作记录插入日志表中
if (insertOperationLog(pjp, userLog)) {
// 执行controller中的上报工单方法
result = pjp.proceed();
} else {
Map<String, Object> map = new HashMap<>();
map.put(CommonConstant.EXECSTATE, "false");
map.put(CommonConstant.EXECMSG, "插入失败");
return JSON.toJSONString(map);
}
} catch (Throwable throwable) {
logger.error("用户操作记录插入拦截器报错:\n", throwable);
}
return result;
}
/**
* 插入用户操作日志
*
* @return
* @throws Exception
*/
private boolean insertOperationLog(ProceedingJoinPoint pjp, UserLog userLog) throws Exception {
try {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
HttpSession session=request.getSession();
User a =this.getSessionUser(session);
String operator= String.valueOf(a.getUserid());
User u = getRequestWorkOrderBean(pjp);
String addTime = DateUtil.getStringFromDate(new Date(), null);
String remarks=operator+userLog.getType()+"一条信息"+"用户ID为"+u.getUserid();
UserLog user = new UserLog();
user.setOperatorDate(addTime);
user.setUserid(u.getUserid());
user.setOperator(operator);
user.setType(userLog.getType());
user.setRemarks(remarks);
userOperationService.insertUser(user);
} catch (Exception e) {
logger.error("workOrderCheckIfTheSame执行报错:\n", e);
}
return true;
}
/**
* 获取请求参数中的信息
*
* @param pjp
* @return
*/
private User getRequestWorkOrderBean(ProceedingJoinPoint pjp) {
User bean = null;
String userids = "";
try {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
if (request == null) {
return null;
}
Object[] params = pjp.getArgs();
for (Object param : params) {
System.out.println("123====================" + params[1]);
if (param instanceof User) {
bean = (User) param;
}
if (param instanceof String[]) {
System.out.println("123====================");
String[] a = (String[]) param;
userids = a[0];
}
 
}
//请求的参数
if (bean == null) {
bean = new User();
bean.setUserid(Long.parseLong(userids));
 
}
} catch (Exception e) {
logger.error("解析请求参数时异常:\n", e);
}
return bean;
}
protected User getSessionUser(HttpSession session) {
return (User) session.getAttribute(CommonConstant.CUR_USER);
}
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值