Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问下加密拦截器不挂在 “ParameterHandler”上是有什么考量吗 #19

Open
zs1211 opened this issue May 10, 2024 · 12 comments
Labels
question Further information is requested

Comments

@zs1211
Copy link

zs1211 commented May 10, 2024

No description provided.

@WhiteDG
Copy link
Owner

WhiteDG commented May 11, 2024

没什么特殊考量,感觉挂在 ParameterHandler 也可以

@zs1211
Copy link
Author

zs1211 commented May 11, 2024

好的,谢谢大佬。

@zs1211
Copy link
Author

zs1211 commented May 11, 2024

另外想要请教一下 MybatisQueryEncryptionPlugin 这个类是做什么用的呢。 阅读了一下源码没能理解

@WhiteDG
Copy link
Owner

WhiteDG commented May 11, 2024

MybatisQueryEncryptionPlugin 是用来支持加密查询
这个 issue: #1

@zs1211
Copy link
Author

zs1211 commented May 11, 2024

懂了,谢谢。

没考虑到这个场景,因为我们这边的需求是加密字段还需要支持模糊搜索

@zs1211
Copy link
Author

zs1211 commented May 11, 2024

我在想keepParameter的实现用属性拷贝的方式会不会性能好点

@WhiteDG
Copy link
Owner

WhiteDG commented May 11, 2024

懂了,谢谢。

没考虑到这个场景,因为我们这边的需求是加密字段还需要支持模糊搜索

加密字段模糊搜索可能比较难实现,这里实现的只是简单的 equals 查询

@WhiteDG
Copy link
Owner

WhiteDG commented May 11, 2024

我在想keepParameter的实现用属性拷贝的方式会不会性能好点

能具体说一下吗,现在的实现是加密执行完 sql 之后再对原来的参数执行一次解密

@zs1211
Copy link
Author

zs1211 commented May 11, 2024

我在想keepParameter的实现用属性拷贝的方式会不会性能好点

能具体说一下吗,现在的实现是加密执行完 sql 之后再对原来的参数执行一次解密

就是一个不成熟的小想法。 加密执行前把parameter参数深拷贝缓存下,执行后再把拷贝的值赋值给原来的参数

@WhiteDG
Copy link
Owner

WhiteDG commented May 11, 2024

一开始是这样实现的,不过我对深拷贝不是很懂,所以就用了 kryo 实现,后面感觉多引一个库也不是太好,就干脆直接解密,从头到尾操作同一个实例。

@zs1211
Copy link
Author

zs1211 commented May 11, 2024

大佬您好,还有个问题想要请教一下您。 在下面这段代码中,不是已经拿到了 paramName了吗,为什么还需要对String特殊处理呢

 private static void addParamToEncrypt(Map<String, EncryptedParamConfig> shouldEncryptParams, int paramIndex, Class<?> parameterType, String paramName, EncryptedField encryptedField) {
        EncryptedParamConfig encryptedParamConfig;
        if (encryptedField != null) {
            encryptedParamConfig = new EncryptedParamConfig(paramName, encryptedField);
        } else {
            encryptedParamConfig = new EncryptedParamConfig(paramName, null, IEncryptor.class);
        }
        shouldEncryptParams.put(paramName, encryptedParamConfig);
        if (parameterType.equals(String.class)) {
            EncryptedParamConfig encryptedParamConfig0;
            String paramIndexKey = "param" + (paramIndex + 1);
            if (encryptedField != null) {
                encryptedParamConfig0 = new EncryptedParamConfig(paramIndexKey, encryptedField);
            } else {
                encryptedParamConfig0 = new EncryptedParamConfig(paramIndexKey, null, IEncryptor.class);
            }
            shouldEncryptParams.put(paramIndexKey, encryptedParamConfig0);
        }
    }

@WhiteDG
Copy link
Owner

WhiteDG commented May 11, 2024

作用是处理 mybatis 自动往 parameterMap 中添加的 param1/param2... 这些顺序映射参数。
比如这个 mapper 接口:

List<User> selectByName(@EncryptedField(encryptor = MyEncryptor.class) @Param("name") String name);

在 xml 中写 sql 的时候,可以用 #{name} 也可以用 #{param1}

    <select id="selectByName" resultType="io.github.whitedg.demo.entity.User">
        select *
        from t_user
        where name = #{name}
    </select>

    <select id="selectByName" resultType="io.github.whitedg.demo.entity.User">
        select *
        from t_user
        where name = #{param1}
    </select>

@WhiteDG WhiteDG added the question Further information is requested label May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants