记录后台操作人员的登陆、退出、进入了哪个界面、增加、删除、修改等操作
在数据库中建立一张SYSLOG表,使用Sping 的AOP实现日志管理,在Sping.xml中配置
<!-- Spring 获取配置文件中定义的bean -->
<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 开启切面编程功能 -->
<context:component-scan base-package="com.geeboo.wxbus.service.impl,com.geeboo.wxbus.interceptor"/>
<context:annotation-config/>
然后在interceptor包下创建一个MyInterceptor类,里面有anyMethod ()。doBefore(),doAfterReturning(),doAfterThrowing(),
doAfter(),doAround()等方法。
在doAround()中执行判断用户进行进入那个类并且那个方法,然后添加日志记录。
// 调用方法名称
String methodName =pjp.getSignature().getName();
//获取进入的类名
StringclassName=
pjp.getSignature().getDeclaringTypeName();
className =
className.substring(className.lastIndexOf(".") 1).trim();
package com.geeboo.wxbus.interceptor; import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Pattern; import net.sf.json.JSONObject; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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.jeecgframework.core.util.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.geeboo.wxbus.entity.FeedbackInfo; import com.geeboo.wxbus.entity.SnackCollect;
import com.geeboo.wxbus.entity.SysLog;
import com.geeboo.wxbus.entity.Tuser;
import com.geeboo.wxbus.entity.UserSuggest;
import com.geeboo.wxbus.pageModel.FeedbackInfoPage;
import com.geeboo.wxbus.pageModel.SnackCollectPage;
import com.geeboo.wxbus.pageModel.User;
import com.geeboo.wxbus.pageModel.UserSuggestPage;
import com.geeboo.wxbus.service.FeedbackInfoServiceI;
import com.geeboo.wxbus.service.SnackCollectServiceI;
import com.geeboo.wxbus.service.SysLogServiceI;
import com.geeboo.wxbus.service.UserServiceI;
import com.geeboo.wxbus.service.UserSuggestServiceI; @Aspect
@Component
public class MyInterceptor { private SysLogServiceI logServiceI;//日志
private UserServiceI userService;//用户
private FeedbackInfoServiceI feedbackInfoService;//反馈
private SnackCollectServiceI snackCollectService;//商店
private UserSuggestServiceI userSuggestService;//用户建议 public UserServiceI getUserService() {
return userService;
}
@Autowired
public void setUserService(UserServiceI userService) {
this.userService = userService;
}
public FeedbackInfoServiceI getFeedbackInfoService() {
return feedbackInfoService;
}
@Autowired
public void setFeedbackInfoService(FeedbackInfoServiceI feedbackInfoService) {
this.feedbackInfoService = feedbackInfoService;
}
public SnackCollectServiceI getSnackCollectService() {
return snackCollectService;
}
@Autowired
public void setSnackCollectService(SnackCollectServiceI snackCollectService) {
this.snackCollectService = snackCollectService;
}
public UserSuggestServiceI getUserSuggestService() {
return userSuggestService;
}
@Autowired
public void setUserSuggestService(UserSuggestServiceI userSuggestService) {
this.userSuggestService = userSuggestService;
}
public SysLogServiceI getLogServiceI() {
return logServiceI;
}
@Autowired
public void setLogServiceI(SysLogServiceI logServiceI) {
this.logServiceI = logServiceI;
} /**
* 第一个* 代表任意的返回类型 (..) 所有参数
*/
@Pointcut("execution(* com.geeboo.wxbus.service.impl.*.*(..))")
private void anyMethod() {
}; // 声明一个切入点 @Before("anyMethod() && args(object)")
public void doBefore(Object object) {
//System.out.println("前置通知" + object);
} @AfterReturning(pointcut = "anyMethod()", returning = "name")
public void doAfterReturning(String name) {
//System.out.println("后置通知:" + name);
} @AfterThrowing("anyMethod()")
public void doAfterThrowing() {
//System.out.println("例外通知");
} @After("anyMethod() && args(object)" )
public void doAfter(Object object) {
//System.out.println("最终通知"+object);
} @Around("anyMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable { // 要执行pip.proceed方法 // 调用方法名称
String methodName = pjp.getSignature().getName();
//获取进入的类名
String className = pjp.getSignature().getDeclaringTypeName();
className = className.substring(className.lastIndexOf(".") + 1).trim(); if(className.equals("SysLogServiceImpl")||className.equals("TotalMsgServiceImpl")||className.equals("TotalUserServiceImpl")||className.equals("MessageServiceImpl")){ //如果是日志的就不用
return pjp.proceed();
}
// 调用参数
Object[] args = pjp.getArgs();
Object object = null;
// System.out.println("==============进去的方法"+methodName);
if(Pattern.matches("(add|update|delete)[\\S]*",
methodName)) { String logMsg="";//日志消息内容 Tuser user=(Tuser) ContextHolderUtils.getSession().getAttribute("USER_SESSION");//获取用户名
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=sdf.format(new Date());//获取当前时间 //System.out.println("进入的类名"+className); JSONObject msg = new JSONObject();
for (Object temp : args) {
Class<? extends Object> paramClazz = temp.getClass();
String classType = paramClazz.getName();
if (classType.equals("java.lang.String")) {
msg.put("key", temp);
} else if (classType.equals("java.util.HashMap")) {
msg.putAll((HashMap<?, ?>) temp);
} else if (classType.startsWith("com.")) {
try {
Field[] f = paramClazz.getDeclaredFields();
for (Field field : f) {
String fieldName = field.getName();
field.setAccessible(true);
msg.put(fieldName, field.get(temp));
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
//用户管理
if(className.equals("UserServiceImpl")){
if(user!=null){
logMsg="用户名:"+user.getCname()+"-在-"+"操作用户";
}else{
logMsg="";
}
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("update")){ //修改之前获取原来的数据
User u=new User();
u.setCid(msg.getString("cid").toString());//获取删除的主键
Tuser lastUser=userService.get(u);
msg1.put("user", lastUser);
logMsg=logMsg+"-进入修改!修改前数据为:"+msg1+"****************修改后数据为:"+msg;
}else if(methodName.equals("delete")){
User u=new User();
u.setCid(msg.getString("key").toString());//获取删除的主键
Tuser lastUser=userService.get(u);
msg1.put("user", lastUser);
logMsg=logMsg+"-进入删除操作值为:"+msg1;
} }
//反馈管理
else if(className.equals("FeedbackInfoServiceImpl")){
logMsg=user.getCname()+"-在-"+"操作反馈信息";
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("update")){ //进入修改界面
FeedbackInfoPage f=new FeedbackInfoPage();
//System.out.println("反馈信息"+msg);
f.setFeedbackId(msg.getString("feedbackId"));
FeedbackInfo lastFeebackInfo=feedbackInfoService.get(f);
msg1.put("feebackInfo", lastFeebackInfo);
}else if(methodName.equals("delete")){
FeedbackInfoPage f=new FeedbackInfoPage();
//System.out.println("反馈信息"+msg);
f.setFeedbackId(msg.getString("key"));
FeedbackInfo lastFeebackInfo=feedbackInfoService.get(f);
msg1.put("feebackInfo", lastFeebackInfo);
}
if(methodName.equals("update")){
logMsg=logMsg+"-进入修改!修改前数据为:"+msg1+"****************修改后数据为:"+msg;
}else if(methodName.equals("delete")){
logMsg=logMsg+"-进入删除操作值为:"+msg1;
}
}
//用户建议管理
else if(className.equals("UserSuggestServiceImpl")){
logMsg=user.getCname()+"-在-"+"操作用户建议";
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("delete")){
UserSuggestPage suggest=new UserSuggestPage();
//System.out.println("用户建议"+msg);
suggest.setSuggestId(msg.getString("key"));
UserSuggest lastSuggest=userSuggestService.get(suggest);
msg1.put("suggest", lastSuggest);
logMsg=logMsg+"-进入删除操作值为:"+msg1;
} }
//店家管理
else if(className.equals("SnackCollectServiceImpl")){
logMsg=user.getCname()+"-在-"+"操作商店";
JSONObject msg1 = new JSONObject();//保存在JSON中然后添加进去
if(methodName.equals("update")){
SnackCollectPage snackCollect=new SnackCollectPage();
//System.out.println("店家管理"+msg);
snackCollect.setSnackCollectId(msg.getString("snackCollectId"));
SnackCollect lastSnack=snackCollectService.get(snackCollect);
msg1.put("snack", lastSnack);
logMsg=logMsg+"-进入修改!修改前数据为:"+msg1+"****************修改后数据为:"+msg;
//System.out.println("店家管理修改后的数据"+logMsg);
}else if(methodName.equals("delete")){
SnackCollectPage snackCollect=new SnackCollectPage();
//System.out.println("店家管理"+msg);
snackCollect.setSnackCollectId(msg.getString("key"));
SnackCollect lastSnack=snackCollectService.get(snackCollect);
msg1.put("snack", lastSnack);
logMsg=logMsg+"-进入删除操作值为:"+msg1;
}
}
if(methodName.equals("add")){ //进入获取添加方法
logMsg=logMsg+"-进入添加操作值为:"+msg;
} SysLog log=new SysLog();
log.setLogId(UUID.randomUUID().toString());
log.setLogMsg(logMsg);
log.setLogTime(time);
logServiceI.add(log);
}
return pjp.proceed();
} }