一、基于springaop实现用户操作的日志记录

原文:https://www.jianshu.com/p/d0bbdf1974bd

代码内容为我自己的,并且把依赖的包也录入进来。

一、引入aop依赖

<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

二、创建一个实体类

 

package com.sj56.breakfast.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class UserLog implements Serializable {

//id
private long id;
//操作用户用户名
private String userName;
private long userId;
//用户操作
private String operation;
//方法名
private String method;
//ip
private String ip;
//操作时间
private Date createTime;

}

 

三、自定义一个注解

package com.sj56.breakfast.config.annotation;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserOperation {

String value() default "";
}

四、创建AOP切面类

package com.sj56.breakfast.config.aspect;

import com.sj56.breakfast.config.annotation.UserOperation;
import com.sj56.breakfast.entity.UserLog;
import com.sj56.breakfast.service.userlog.UserLogService;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
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 javax.servlet.http.HttpServletRequest;
import javax.xml.ws.spi.http.HttpContext;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;

@Aspect
@Component
public class UserLogAspect {

@Autowired
private UserLogService userLogService;

@Autowired
private UserLog userLog;

//在注解的位置切入代码
@Pointcut("@annotation( com.sj56.breakfast.config.annotation.UserOperation )")
public void loginPointCut(){

}

@After("loginPointCut()")
public void logSave(JoinPoint pjp){
System.out.println("保存日志切面~~~~");

//获取方法
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Method method = methodSignature.getMethod();
userLog.setMethod(method.getName());

//获取操作
UserOperation userOperation = method.getAnnotation(UserOperation.class);
if (userOperation != null){
String value = userOperation.value();
userLog.setOperation(value);
}

//获取ip
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();

String ip = request.getRemoteAddr();
userLog.setIp(ip);

//获取用户
String userId = request.getParameter("userId");
System.out.println("用户id:"+userId);
if (userId == null || userId == ""){
userId = "0";
}
//可通过用户id查询用户name
userLog.setUserId(Long.valueOf(userId));

//操作时间
userLog.setCreateTime(new Date());

//保存日志
userLogService.saveLog(userLog);


}
}

 

 

五、拦截实例

//添加一个店铺
@UserOperation(value = "保存一个店铺")
@RequestMapping("/save")
public BFResponse shopSave(Shop shop, @RequestParam HashMap map){
System.out.println("保存店铺map-->:"+map);
shopService.shopSave(shop);
return BFResponseUtils.success();
}

数据困user_name列为了方便随便设置了一个字段插入

 

 
 

 

转载于:https://www.cnblogs.com/phhblog/p/11153184.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值