栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

巧用自定义注解,一行代码解决审计日志

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

巧用自定义注解,一行代码解决审计日志

一、简介

任何一个软件系统,都不可避免的会碰到【信息安全】这个词,尤其是对于刚入行的新手,比如我,我刚入行的时候,领导让我做一个数据报表导出功能,我就按照他的意思去做,至于谁有权限操作导出,导出的数据包含敏感信息应该怎么处理,后端接口是不是做了权限控制防止恶意抓取,这些问题我基本上不关心,我只想一心一意尽快实现需求,然后顺利完成任务交付。

实际上,随着工作阅历的增加,你会越来越能感觉到,实现业务方提的需求,只是完成了软件系统研发中的【能用】要求;服务是否【可靠】可能需要从架构层和运维方面去着手解决;至于是否【安全】、更多的需要从【信息安全】这个角度来思考,尤其是当我们的软件系统面对外界的恶意干扰和攻击时,是否依然能保障用户正常使用,对于大公司,这个可能是头等大事,因为可能一个很小很小的漏洞,一不小心可能会给公司带来几千万的损失!

最常见的就是电商系统和支付系统,尤其是需求旺季的时候,经常有黑客专门攻击这些电商系统,导致大量服务宕机,影响用户正常下单。

像这样的攻击案例每天都有,有的公司甚至直接向黑客气妥,给钱消灾!

但是这种做法肯定不是长久之计,最重要的还是主动提升系统的【安全】防御系数。

由于信息安全所涉及的要求内容众多,今天,我在这里仅仅向大家介绍其中关于【审计日志】的要求和具体应用,后续也会向大家介绍其他的要求。

【审计日志】,简单的说就是系统需要记录谁,在什么时间,对什么数据,做了什么样的更改!这个日志数据是极其珍贵的,后面如果因业务操作上出了问题,可以很方便进行操作回查。

同时,任何一个 IT 系统,如果要过审,这项任务基本上也是必审项!

好了,需求我们清楚了,具体应用看下面!
二、实践

实现【审计日志】这个需求,我们有一个很好的技术解决方案,就是使用 Spring 的切面编程,创建一个代理类,利用afterReturning和afterThrowing方法来实现日志的记录。

具体实现步骤如下

先创建审计日志表
CREATE TABLE `tb_audit_log` (
  `id` bigint(20) NOT NULL  COMMENT '审计日志,主键ID',
  `table_name` varchar(500) DEFAULT '' COMMENT '操作的表名,多个用逗号隔开',
  `operate_desc` varchar(200) DEFAULT '' COMMENT '操作描述',
  `request_param` varchar(200) DEFAULT '' COMMENT '请求参数',
  `result` int(10) COMMENT '执行结果,0:成功,1:失败',
  `ex_msg` varchar(200) DEFAULT '' COMMENT '异常信息',
  `user_agent` text COLLATE utf8mb4_unicode_ci COMMENT '用户代理信息',
  `ip_address` varchar(32) NOT NULL DEFAULT '' COMMENT '操作时设备IP',
  `ip_address_name` varchar(32) DEFAULT '' COMMENT '操作时设备IP所在地址',
  `operate_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
  `operate_user_id` varchar(32) DEFAULT '' COMMENT '操作人ID',
  `operate_user_name` varchar(32) DEFAULT '' COMMENT '操作人',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审计日志表';
然后编写一个注解类
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
@documented
public @interface SystemAuditLog {

    
    String tableName() default "";

    
    String description() default "";

}
接着编写一个代理类
@Component
@Aspect
public class SystemAuditLogAspect {

    @Autowired
    private SystemAuditLogService systemAuditLogService;

    
    @Pointcut("@within(com.example.demo.core.annotation.SystemAuditLog)|| @annotation(com.example.demo.core.annotation.SystemAuditLog)")
    public void methodAspect() {
    }

    
    @Before("methodAspect()")
    public void before(){
        System.out.println("SystemAuditLog代理 -> 调用方法执行之前......");
    }

    
    @After("methodAspect()")
    public void after(){
        System.out.println("SystemAuditLog代理 -> 调用方法执行之后......");
    }

    
    @AfterReturning(value = "methodAspect()")
    public void afterReturning(JoinPoint joinPoint) throws Exception {
        System.out.println("SystemAuditLog代理 -> 调用方法结束拦截......");
        //封装数据
        AuditLog entity = warpAuditLog(joinPoint);
        entity.setResult(0);

        //插入到数据库
        systemAuditLogService.add(entity);
    }


    
    @AfterThrowing(value="methodAspect()", throwing="ex")
    public void afterThrowing(JoinPoint joinPoint, Exception ex) throws Exception {
        System.out.println("SystemAuditLog代理 -> 抛出异常拦截......");
        //封装数据
        AuditLog entity = warpAuditLog(joinPoint);
        entity.setResult(1);
        //封装错误信息
        entity.setExMsg(ex.getMessage());

        //插入到数据库
        systemAuditLogService.add(entity);
    }


    
    private AuditLog warpAuditLog(JoinPoint joinPoint) throws Exception {
        //获取请求上下文
        HttpServletRequest request = getHttpServletRequest();
        //获取注解上的参数值
        SystemAuditLog systemAuditLog = getServiceMethodDescription(joinPoint);
        //获取请求参数
        Object requestObj = getServiceMethodParams(joinPoint);
        //封装数据
        AuditLog auditLog = new AuditLog();
        auditLog.setId(SnowflakeIdWorker.getInstance().nextId());
        //从请求上下文对象获取相应的数据
        if(Objects.nonNull(request)){
            auditLog.setUserAgent(request.getHeader("User-Agent"));
            //获取登录时的ip地址
            auditLog.setIpAddress(IpAddressUtil.getIpAddress(request));
            //调用外部接口,获取IP所在地
            auditLog.setIpAddressName(IpAddressUtil.getLoginAddress(auditLog.getIpAddress()));
        }
        //封装操作的表和描述
        if(Objects.nonNull(systemAuditLog)){
            auditLog.setTableName(systemAuditLog.tableName());
            auditLog.setOperateDesc(systemAuditLog.description());
        }
        //封装请求参数
        auditLog.setRequestParam(JSON.toJSONString(requestObj));
        //封装请求人
        if(Objects.nonNull(requestObj) && requestObj instanceof baseRequest){
            auditLog.setOperateUserId(((baseRequest) requestObj).getLoginUserId());
            auditLog.setOperateUserName(((baseRequest) requestObj).getLoginUserName());
        }
        auditLog.setOperateTime(new Date());
        return auditLog;
    }


    
    private HttpServletRequest getHttpServletRequest(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;
        HttpServletRequest request = sra.getRequest();
        return request;
    }

    
    private Object getServiceMethodParams(JoinPoint joinPoint) {
        Object[] arguments = joinPoint.getArgs();
        if(Objects.nonNull(arguments) && arguments.length > 0){
            return arguments[0];
        }
        return null;
    }


    
    private SystemAuditLog getServiceMethodDescription(JoinPoint joinPoint) throws Exception {
        //类名
        String targetName = joinPoint.getTarget().getClass().getName();
        //方法名
        String methodName = joinPoint.getSignature().getName();
        //参数
        Object[] arguments = joinPoint.getArgs();
        //通过反射获取示例对象
        Class targetClass = Class.forName(targetName);
        //通过实例对象方法数组
        Method[] methods = targetClass.getMethods();
        for(Method method : methods) {
            //判断方法名是不是一样
            if(method.getName().equals(methodName)) {
                //对比参数数组的长度
                Class[] clazzs = method.getParameterTypes();
                if(clazzs.length == arguments.length) {
                    //获取注解里的日志信息
                    return method.getAnnotation(SystemAuditLog.class);
                }
            }
        }
        return null;
    }
}
最后,只需要在对应的接口或者方法上添加审计日志注解即可
@RestController
@RequestMapping("api")
public class LoginController {

    
    @SystemAuditLog(tableName = "tb_user", description = "用户登录")
    @PostMapping("login")
    public void login(UserLoginDTO request){
        //登录逻辑处理
    }
}
相关的实体类
@Data
public class AuditLog {

    
    private Long id;

    
    private String tableName;

    
    private String operateDesc;

    
    private String requestParam;

    
    private Integer result;

    
    private String exMsg;

    
    private String userAgent;

    
    private String ipAddress;

    
    private String ipAddressName;

    
    private Date operateTime;


    
    private String operateUserId;

    
    private String operateUserName;
}
public class baseRequest implements Serializable {

    
    private String token;

    
    private String loginUserId;

    
    private String loginUserName;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getLoginUserId() {
        return loginUserId;
    }

    public void setLoginUserId(String loginUserId) {
        this.loginUserId = loginUserId;
    }

    public String getLoginUserName() {
        return loginUserName;
    }

    public void setLoginUserName(String loginUserName) {
        this.loginUserName = loginUserName;
    }
}
@Data
public class UserLoginDTO extends baseRequest {

    
    private String userName;

    
    private String password;
}

三、小结

整个程序的实现过程,主要使用了 Spring AOP 特性,对特定方法进行前、后拦截,从而实现业务方的需求。

在下篇文章中,我们会详细介绍 Spring AOP 的使用!
一、简介

任何一个软件系统,都不可避免的会碰到【信息安全】这个词,尤其是对于刚入行的新手,比如我,我刚入行的时候,领导让我做一个数据报表导出功能,我就按照他的意思去做,至于谁有权限操作导出,导出的数据包含敏感信息应该怎么处理,后端接口是不是做了权限控制防止恶意抓取,这些问题我基本上不关心,我只想一心一意尽快实现需求,然后顺利完成任务交付。

实际上,随着工作阅历的增加,你会越来越能感觉到,实现业务方提的需求,只是完成了软件系统研发中的【能用】要求;服务是否【可靠】可能需要从架构层和运维方面去着手解决;至于是否【安全】、更多的需要从【信息安全】这个角度来思考,尤其是当我们的软件系统面对外界的恶意干扰和攻击时,是否依然能保障用户正常使用,对于大公司,这个可能是头等大事,因为可能一个很小很小的漏洞,一不小心可能会给公司带来几千万的损失!

最常见的就是电商系统和支付系统,尤其是需求旺季的时候,经常有黑客专门攻击这些电商系统,导致大量服务宕机,影响用户正常下单。

像这样的攻击案例每天都有,有的公司甚至直接向黑客气妥,给钱消灾!

但是这种做法肯定不是长久之计,最重要的还是主动提升系统的【安全】防御系数。

由于信息安全所涉及的要求内容众多,今天,我在这里仅仅向大家介绍其中关于【审计日志】的要求和具体应用,后续也会向大家介绍其他的要求。

【审计日志】,简单的说就是系统需要记录谁,在什么时间,对什么数据,做了什么样的更改!这个日志数据是极其珍贵的,后面如果因业务操作上出了问题,可以很方便进行操作回查。

同时,任何一个 IT 系统,如果要过审,这项任务基本上也是必审项!

好了,需求我们清楚了,具体应用看下面!
二、实践

实现【审计日志】这个需求,我们有一个很好的技术解决方案,就是使用 Spring 的切面编程,创建一个代理类,利用afterReturning和afterThrowing方法来实现日志的记录。

具体实现步骤如下

先创建审计日志表
CREATE TABLE `tb_audit_log` (
  `id` bigint(20) NOT NULL  COMMENT '审计日志,主键ID',
  `table_name` varchar(500) DEFAULT '' COMMENT '操作的表名,多个用逗号隔开',
  `operate_desc` varchar(200) DEFAULT '' COMMENT '操作描述',
  `request_param` varchar(200) DEFAULT '' COMMENT '请求参数',
  `result` int(10) COMMENT '执行结果,0:成功,1:失败',
  `ex_msg` varchar(200) DEFAULT '' COMMENT '异常信息',
  `user_agent` text COLLATE utf8mb4_unicode_ci COMMENT '用户代理信息',
  `ip_address` varchar(32) NOT NULL DEFAULT '' COMMENT '操作时设备IP',
  `ip_address_name` varchar(32) DEFAULT '' COMMENT '操作时设备IP所在地址',
  `operate_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
  `operate_user_id` varchar(32) DEFAULT '' COMMENT '操作人ID',
  `operate_user_name` varchar(32) DEFAULT '' COMMENT '操作人',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审计日志表';
然后编写一个注解类
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
@documented
public @interface SystemAuditLog {

    
    String tableName() default "";

    
    String description() default "";

}
接着编写一个代理类
@Component
@Aspect
public class SystemAuditLogAspect {

    @Autowired
    private SystemAuditLogService systemAuditLogService;

    
    @Pointcut("@within(com.example.demo.core.annotation.SystemAuditLog)|| @annotation(com.example.demo.core.annotation.SystemAuditLog)")
    public void methodAspect() {
    }

    
    @Before("methodAspect()")
    public void before(){
        System.out.println("SystemAuditLog代理 -> 调用方法执行之前......");
    }

    
    @After("methodAspect()")
    public void after(){
        System.out.println("SystemAuditLog代理 -> 调用方法执行之后......");
    }

    
    @AfterReturning(value = "methodAspect()")
    public void afterReturning(JoinPoint joinPoint) throws Exception {
        System.out.println("SystemAuditLog代理 -> 调用方法结束拦截......");
        //封装数据
        AuditLog entity = warpAuditLog(joinPoint);
        entity.setResult(0);

        //插入到数据库
        systemAuditLogService.add(entity);
    }


    
    @AfterThrowing(value="methodAspect()", throwing="ex")
    public void afterThrowing(JoinPoint joinPoint, Exception ex) throws Exception {
        System.out.println("SystemAuditLog代理 -> 抛出异常拦截......");
        //封装数据
        AuditLog entity = warpAuditLog(joinPoint);
        entity.setResult(1);
        //封装错误信息
        entity.setExMsg(ex.getMessage());

        //插入到数据库
        systemAuditLogService.add(entity);
    }


    
    private AuditLog warpAuditLog(JoinPoint joinPoint) throws Exception {
        //获取请求上下文
        HttpServletRequest request = getHttpServletRequest();
        //获取注解上的参数值
        SystemAuditLog systemAuditLog = getServiceMethodDescription(joinPoint);
        //获取请求参数
        Object requestObj = getServiceMethodParams(joinPoint);
        //封装数据
        AuditLog auditLog = new AuditLog();
        auditLog.setId(SnowflakeIdWorker.getInstance().nextId());
        //从请求上下文对象获取相应的数据
        if(Objects.nonNull(request)){
            auditLog.setUserAgent(request.getHeader("User-Agent"));
            //获取登录时的ip地址
            auditLog.setIpAddress(IpAddressUtil.getIpAddress(request));
            //调用外部接口,获取IP所在地
            auditLog.setIpAddressName(IpAddressUtil.getLoginAddress(auditLog.getIpAddress()));
        }
        //封装操作的表和描述
        if(Objects.nonNull(systemAuditLog)){
            auditLog.setTableName(systemAuditLog.tableName());
            auditLog.setOperateDesc(systemAuditLog.description());
        }
        //封装请求参数
        auditLog.setRequestParam(JSON.toJSONString(requestObj));
        //封装请求人
        if(Objects.nonNull(requestObj) && requestObj instanceof baseRequest){
            auditLog.setOperateUserId(((baseRequest) requestObj).getLoginUserId());
            auditLog.setOperateUserName(((baseRequest) requestObj).getLoginUserName());
        }
        auditLog.setOperateTime(new Date());
        return auditLog;
    }


    
    private HttpServletRequest getHttpServletRequest(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;
        HttpServletRequest request = sra.getRequest();
        return request;
    }

    
    private Object getServiceMethodParams(JoinPoint joinPoint) {
        Object[] arguments = joinPoint.getArgs();
        if(Objects.nonNull(arguments) && arguments.length > 0){
            return arguments[0];
        }
        return null;
    }


    
    private SystemAuditLog getServiceMethodDescription(JoinPoint joinPoint) throws Exception {
        //类名
        String targetName = joinPoint.getTarget().getClass().getName();
        //方法名
        String methodName = joinPoint.getSignature().getName();
        //参数
        Object[] arguments = joinPoint.getArgs();
        //通过反射获取示例对象
        Class targetClass = Class.forName(targetName);
        //通过实例对象方法数组
        Method[] methods = targetClass.getMethods();
        for(Method method : methods) {
            //判断方法名是不是一样
            if(method.getName().equals(methodName)) {
                //对比参数数组的长度
                Class[] clazzs = method.getParameterTypes();
                if(clazzs.length == arguments.length) {
                    //获取注解里的日志信息
                    return method.getAnnotation(SystemAuditLog.class);
                }
            }
        }
        return null;
    }
}
最后,只需要在对应的接口或者方法上添加审计日志注解即可
@RestController
@RequestMapping("api")
public class LoginController {

    
    @SystemAuditLog(tableName = "tb_user", description = "用户登录")
    @PostMapping("login")
    public void login(UserLoginDTO request){
        //登录逻辑处理
    }
}
相关的实体类
@Data
public class AuditLog {

    
    private Long id;

    
    private String tableName;

    
    private String operateDesc;

    
    private String requestParam;

    
    private Integer result;

    
    private String exMsg;

    
    private String userAgent;

    
    private String ipAddress;

    
    private String ipAddressName;

    
    private Date operateTime;


    
    private String operateUserId;

    
    private String operateUserName;
}
public class baseRequest implements Serializable {

    
    private String token;

    
    private String loginUserId;

    
    private String loginUserName;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getLoginUserId() {
        return loginUserId;
    }

    public void setLoginUserId(String loginUserId) {
        this.loginUserId = loginUserId;
    }

    public String getLoginUserName() {
        return loginUserName;
    }

    public void setLoginUserName(String loginUserName) {
        this.loginUserName = loginUserName;
    }
}

@Data
public class UserLoginDTO extends baseRequest {

    
    private String userName;

    
    private String password;
}

三、小结

整个程序的实现过程,主要使用了 Spring AOP 特性,对特定方法进行前、后拦截,从而实现业务方的需求。

更多 SpringBoot 相关技术案例实践,感兴趣的朋友,在此我向大家推荐一个架构学习交流圈。交流学习伪鑫:539413949(里面有大量的面试题及答案)里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号