自定义对模型字段的注释,以便在spring boot中存储到数据库时进行加密和解密。

问题描述 投票:0回答:1

我有一个spring boot应用程序,是一个自定义注解的初学者,我想引入一个类似于couchbase java sdk的@EncryptedField的自定义注解。我想引入一个类似于couchbase java sdk's @EncryptedField的自定义注解,如果我把这个注解应用到model类中的一个字段,那么这个字段在存储到db时必须被加密,而在回传时必须被解密。我应该使用Spring AOP来实现这个功能吗?任何实现这个问题的意见将是非常有用的

java spring spring-boot spring-aop java-annotations
1个回答
0
投票

这取决于你使用的是什么持久化框架。

对于Mybatis,你可以 增设受体

这样

<plugins>       
    <plugin interceptor="com.XXX.XXXX.service.encryptinfo.MyInterceptor" />
</plugins>

那就实施吧

    @Intercepts({
    @Signature(type=Executor.class,method="update",args={MappedStatement.class,Object.class}),
    @Signature(type=Executor.class,method="query",args={MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})
})
public class MyInterceptor implements Interceptor{

    // your logic here
    // write something that use reflection capture your custom annotaion of the entity and field
}
© www.soinside.com 2019 - 2024. All rights reserved.