背景
公司的系统管理项目等需要添加用户对配置信息,组织权限,登录信息等内容做操作记录的登记,用于做审计使用。
实现方式
- 直接使用业务代码添加对应代码(太多冗余代码,使用不方便)
- 使用aop切面+注解的方式快捷的记录信息(本文使用的就是这种方式)
代码大纲
注: gradle或者maven引入springboot的相关包和spring-boot-starter-aop
添加类:
自定义注解类:AuditLogAspect
AOP切面类:VinceAspectj
自己的业务类:xxxService
自己的controller:xxxController
主要代码
注解类:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AuditLogAspect {
/** 根据自己的需要添加注解类内容*/
String operation();//操作内容
String oerationType();//操作类型
String logType();//日志类型
String appType();//应用类型
}
切面类
@Aspect
@Component
public class VinceAspectj {
private Logger logger = LoggerFactory.getLogger(VinceAspectj.class);
@Autowired private AuditLogService auditLogService;
// @Pointcut("execution(public * com.my.provider.*.*.service.*.*(..))")
//使用自定义注解类的,进入切面 @Pointcut("@annotation(com.my.provider.system.annotation.AuditLogAspect)")
public void auditLogPointCut() {}
@AfterReturning(returning = "tag", pointcut = "auditLogPointCut()")
public void auditLog(JoinPoint joinPoint, Object tag) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Object[] agrs = joinPoint.getArgs();
String name = null;
// 如果传参包含类且包含name属性 取出name的内容
for (Object object : agrs) {
if (object == null) {
continue;
}
Integer nameIndex = object.toString().indexOf("name=");
if (nameIndex > 0) {
name = object.toString().substring(nameIndex + "name=".length()).split(",")[0];
}
}
AuditLogAspect auditLogAspect = signature.getMethod().getAnnotation(AuditLogAspect.class);
// 没有添加注释的不写入日志
if (auditLogAspect == null) {
return;
} else {
String userId = ApiUtils.currentUserId();//当前登录的机构ID。使用自己项目的
String operation = auditLogAspect.operation();
if (name != null) {
operation = operation + ",名称为:" + name;
}
String logType = auditLogAspect.logType();
String appType = auditLogAspect.appType();
String operationType = auditLogAspect.operationType();
//保存日志内容,自行替换自己的保存业务
auditLogService.saveOperation(userId, operation, operationType, logType, appType);
}
}
}`
service业务类
```java
@Service
public class UserService extends BaseService<UserMapper, User> {
@AuditLogAspect(operation = "添加用户", operationType = "处理", logType = "权限信息", appType = "4")
public boolean insertUser(UserInsertReq userInsertReq) {
//添加用户代码
}
}```
controller控制器
@Api(tags = {"用户服务"})
@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Validated
@Transactional
public class UserRestController extends SuperController {
@Autowired private UserService userService;
@ApiOperation("添加用户")
@PostMapping("/insertUser")
public ApiResponses<Boolean> insertUser(
@RequestBody @Validated(UserInsertReq.Create.class) UserInsertReq userInsertReq) {
return success(userService.insertUser(userInsertReq));
}
}
总结
本文提供的是主要代码,具体的业务保存等业务代码自己补充。
测试内容就省略了,无非就是调用对应的接口,看是否进入切面类,日志是否成功添加入库。