mongo和sqlServer数据表字段加解密

一. mongoDB

        实现BeforeConvertCallback  AfterConvertCallback  AfterSaveCallback,做钩子函数,然后拦截,加密算法使用自定义的加密算法,也可以用现有的。



import org.bson.Document;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback;
import org.springframework.data.mongodb.core.mapping.event.AfterSaveCallback;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback;
import org.springframework.stereotype.Component;

//注册成组件
@Component
public class DocumentCallback implements BeforeConvertCallback<IEncryptDocument>, AfterConvertCallback<IEncryptDocument>, AfterSaveCallback<IEncryptDocument> {

    @NotNull
    @Override
    public IEncryptDocument onAfterConvert(@NotNull IEncryptDocument entity, @NotNull Document document, @NotNull String collection) {
        encryptOrDecryptDocument(entity, false);

        return entity;
    }

    @NotNull
    @Override
    public IEncryptDocument onAfterSave(@NotNull IEncryptDocument entity, @NotNull Document document, @NotNull String collection) {
        encryptOrDecryptDocument(entity, false);

        return entity;
    }

    @NotNull
    @Override
    public IEncryptDocument onBeforeConvert(@NotNull IEncryptDocument entity, @NotNull String collection) {
        encryptOrDecryptDocument(entity, true);

        return entity;
    }

    private void encryptOrDecryptDocument(IEncryptDocument entity, boolean doEncrypt) {

        if (null == entity) {
            return;
        }

        Class<?> entityClass = entity.getClass();

        if (ICustomEncryptDocument.class.isAssignableFrom(entityClass)) {
            ICustomEncryptDocument iCustomEncryptDocument = (ICustomEncryptDocument) entity;
            iCustomEncryptDocument.handleEncrypt(doEncrypt);
            return;
        }

        EncryptFieldUtil.handleField(entity, doEncrypt);
    }
}

2.sqlserver

@Convert(converter = NeedEncryptAttributeConverter.class)  

加上这个注解就能,NeedEncryptAttributeConverter(实现AttributeConverter,重写两个方法)只要写好这个类





import javax.persistence.AttributeConverter;

public class NeedEncryptAttributeConverter implements AttributeConverter<String, String> {

    private EncryptService encryptService;

    @Override
    public String convertToDatabaseColumn(String attribute) {
        init();
        return encryptService.encrypt(attribute);
    }

    @Override
    public String convertToEntityAttribute(String dbData) {
        init();
        return encryptService.decrypt(dbData);
    }

    private void init() {
        if (encryptService == null) {
            encryptService = ApplicationContextHolder.getBean("encryptService", EncryptService.class);
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值