SpringBoot实现增删改、登录相关的⽇志记录
在SpringBoot中实现对项⽬中的各个操作记录操作⽇志,此⽅法是最简单的⽅法,这样的做法需要对项⽬中的命名规则进⾏规范,⽐如添加⽤add* 编辑⽤ update* 删除⽤delete*
package com.ljq.bookshop.aop;
import flect.Method;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import com.ljq.bookshop.pojo.LogInfo;
import com.ljq.bookshop.pojo.UserInfo;
import com.ljq.bookshop.service.LogInfoService;
import com.ljq.bookshop.util.IpUtil;
@Aspect
@Component
public class LogAspect {
@Autowired
private LogInfoService logInfoService;
/
**
* 添加业务逻辑⽅法切⼊点
*/
@Pointcut("execution(* com.ljq.bookshop.service.*.add*(..))")
public void addServiceCall() {
}
/**
* 修改业务逻辑⽅法切⼊点
*/
@Pointcut("execution(* com.ljq.bookshop.service.*.update*(..))")
public void updateServiceCall() {
}
/**
* 删除业务逻辑⽅法切⼊点
* 此处拦截要拦截到具体的莫⼀个模块
* 如deleteUser⽅法。则删除user的时候会记录⽇志
*/
@Pointcut("execution(* com.ljq.bookshop.service.*.delete*(..))")
public void deleteServiceCall() {
}
/**
* 管理员添加操作⽇志(后置通知)
* @param joinPoint
* @param rtv
* @throws Throwable
*/
@AfterReturning(value="addServiceCall()", argNames="rtv", returning="rtv")
public void insertServiceCallCalls(JoinPoint joinPoint, Object rtv) throws Throwable{
public void insertServiceCallCalls(JoinPoint joinPoint, Object rtv) throws Throwable{
HttpServletRequest request = ((ServletRequestAttributes) RequestAttributes()).getRequest();    UserInfo currentUser = (UserInfo) Session().getAttribute("user");
//判断参数
Args() == null){
return;
}
//获取⽅法名
//String methodName = Signature().getName();
String className = Args()[0].getClass().getName();
//获取操作内容
className = className.substring(className.lastIndexOf(".") + 1);
String opContent = Args(), "添加");
//创建⽇志对象
LogInfo log = new LogInfo();
log.LowerCase());
if(currentUser==null) {
log.setUserName("user");
}else {
log.Username());
}
//操作内容
log.setContent(opContent);
//操作
log.setOperation("添加");
log.IpAddr(request));
logInfoService.insertLog(log);
}
/**
* 管理员修改操作⽇志(后置通知)
* @param joinPoint
* @param rtv
* @throws Throwable
*/
@AfterReturning(value="updateServiceCall()", argNames="rtv", returning="rtv")
public void updateServiceCallCalls(JoinPoint joinPoint, Object rtv) throws Throwable{
HttpServletRequest request = ((ServletRequestAttributes) RequestAttributes()).getRequest();    UserInfo currentUser = (UserInfo) Session().getAttribute("user");
//判断参数
Args() == null){
return;
}
//获取⽅法名
String className = Args()[0].getClass().getName();
className = className.substring(className.lastIndexOf(".") + 1);
//获取操作内容
String opContent = Args(), "修改");
LogInfo log = new LogInfo();
log.LowerCase());
if(currentUser==null) {
log.setUserName("user");
}else {
log.Username());
}
//操作
log.setContent(opContent);
log.setOperation("修改");
log.IpAddr(request));
//添加⽇志
//添加⽇志
logInfoService.insertLog(log);
}
/**
* 管理员删除操作⽇志(后置通知)
* @param joinPoint
* @param rtvspringboot aop
* @throws Throwable
*/
@AfterReturning(value="deleteServiceCall()", argNames="rtv", returning="rtv")
public void deleteServiceCallCalls(JoinPoint joinPoint, Object rtv) throws Throwable{
HttpServletRequest request = ((ServletRequestAttributes) RequestAttribut
es()).getRequest();    UserInfo currentUser = (UserInfo) Session().getAttribute("user");
//判断参数
Args() == null){
return;
}
//获取⽅法名
String className = Target().getClass().getName().toLowerCase();
try {
className=className.substring(0,className.indexOf("serviceimpl"));
className = className.substring(className.lastIndexOf(".") + 1);
}catch(Exception e) {
e.printStackTrace();
}
//获取操作内容
String opContent = "ID:"+Args()[0].toString()+"->删除";
LogInfo log = new LogInfo();
log.setModule(className);
if(currentUser==null) {
log.setUserName("user");
}else {
log.Username());
}
/
/操作
log.setContent(opContent);
log.setOperation("删除");
log.IpAddr(request));
//添加⽇志
logInfoService.insertLog(log);
}
/**
* 使⽤Java反射来获取被拦截⽅法(insert、update)的参数值,将参数值拼接为操作内容
*/
private String adminOptionContent(Object[] args, String type) throws Exception {
if (args == null) {
return null;
}
StringBuffer sb = new StringBuffer();
Object info = args[0];
String className = Class().getName();
className = className.substring(className.lastIndexOf(".") + 1);
sb.append(type+className+" 属性名和值:");
// 获取对象的所有⽅法
Method[] methods = Class().getDeclaredMethods();
// 遍历⽅法,判断get⽅法
for (Method method : methods) {
String methodName = Name();
// 判断是不是get⽅法
// 判断是不是get⽅法
if (methodName.indexOf("get") == -1) {
continue;// 不处理
}
Object rsValue = null;
try {
// 调⽤get⽅法,获取返回值
rsValue = method.invoke(info);
if (rsValue == null) {
continue;
}
} catch (Exception e) {
continue;
}
// 将值加⼊内容中
sb.append(" " + methodName.substring(3) + "-->" + rsValue + "  ");
}
String();
}
}
登录⽇志需要在登录后添加如下代码:
Username(), request, session);
以下是具体的代码
/**
* 保存⽇志信息
* @param userName
* @param request
* @param session
* @throws Exception
*/
private void saveLog(String userName, HttpServletRequest request, HttpSession session) throws Exception {  // 加⼊登陆⽇志
String uri = RequestURI();
String ServletContext().getContextPath();
uri = ve(uri, contextPath+"/");
LogInfo log = new LogInfo();
log.setUserName(userName);
log.IpAddr(request));
log.setOperation("登录");
log.setContent(uri);
logInfoService.insertLog(log);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。