image-20230104101607731

这次 commit 主要解决日志信息中可能存在 password 等敏感字段,需要在保存前排除掉

主要涉及两个类的修改,添加实现了一个 PropertyPreExcludeFilter,集成 fastjson2 的 SimplePropertyPreFilter 实现

/**
 * 排除JSON敏感属性
 * 
 * @author ruoyi
 */
public class PropertyPreExcludeFilter extends SimplePropertyPreFilter
{
    public PropertyPreExcludeFilter()
    {
    }

    public PropertyPreExcludeFilter addExcludes(String... filters)
    {
        for (int i = 0; i < filters.length; i++)
        {
            this.getExcludes().add(filters[i]);
        }
        return this;
    }
}

我们下面简单看下若依里面 基于自定义的注解的日志是如何实现的

我们以用户的新增接口为例

/**
 * 新增用户
 */
@PreAuthorize("@ss.hasPermi('system:user:add')")
@Log(title = "用户管理", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody SysUser user)
{
    // .... 省略实现
}
image-20230104102601886

查看信息界面,不一定是对应这个接口的日志哈,简单看下有个概念就好

businessType 对应 请求方式

注解类

/**
 * 自定义操作日志记录注解
 * 
 * @author ruoyi
 *
 */
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log
{
    /**
     * 模块 
     */
    public String title() default "";

    /**
     * 功能
     */
    public BusinessType businessType() default BusinessType.OTHER;

    /**
     * 操作人类别
     */
    public OperatorType operatorType() default OperatorType.MANAGE;

    /**
     * 是否保存请求的参数
     */
    public boolean isSaveRequestData() default true;

    /**
     * 是否保存响应的参数
     */
    public boolean isSaveResponseData() default true;
}

日志切面 LogAspect,这个类实现就是做的一个 aop 切面,简单调试下核心方法 handleLog

image-20230104103713414

这里加上 returning,方法的 json 返回值

image-20230104103920549

后面就是从 request 中读取到本次请求的一些信息存到数据库

回到我们这a次的提交,我们创建完 PropertyPreExcludeFilter,在 LogAspect 中 创建这个过滤器

/** 排除敏感属性字段 */
    public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };

/**
 * 忽略敏感属性
 */
public PropertyPreExcludeFilter excludePropertyPreFilter()
{
    return new PropertyPreExcludeFilter().addExcludes(EXCLUDE_PROPERTIES);
}

image-20230104104638832

反序列化得到对象是调用排除

Logo

快速构建 Web 应用程序

更多推荐