简体   繁体   English

JPA:在 JPA 查询中使用 AttributeConverter 自动解密

[英]JPA: Using AttributeConverter automatically decrypt in JPA query

I wrote my own AttributeConverter to encrypt and decrypt Strings and its similar to:我编写了自己的 AttributeConverter 来加密和解密字符串,它类似于:

@Converter
public class CryptoConverter implements AttributeConverter<String, String> {

@Override
public String convertToDatabaseColumn(String attribute)  {
 //works fine
}

@Override
public String convertToEntityAttribute(String dbData) {
//works fine
}

}

My entity class is using this converter:我的实体 class 正在使用这个转换器:

@Entity
@PublishKeyword
@Table(name = "CARD")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Card {

//...

@Column(name = "CARD_LAST_4_DIGITS")
@Convert(converter = CryptoConverter.class)
@Attribute(keyword = "CARD_CARDLAST4DIGITS", resolvedKeyword = "cardLast4Digits", length = 100)
private String cardLast4Digits;

//...
}

Everything works fine until here.直到这里一切正常。 The data I had set is encrypted in the database and is also decrypted after getting the result set.我设置的数据在数据库中加密,得到结果集后也解密。 But the CryptoConverter is not used in automatically in JPA query.但 CryptoConverter 不会在 JPA 查询中自动使用。 ? ?

@Override
public Card findCard(String lastFour, String tenantId, EntityManager entityManager) {
    Query query = entityManager.createQuery("SELECT e FROM Card e where e.cardLast4Digits = :lastFour").setParameter("lastFour", "lastFour");
    List<Card> cardList = query.getResultList();
    if (cardList != null && cardList.size() > 0) {
        return cardList.get(0);
    }
    return null;
}

The input is also being converted.输入也被转换。 So if you use asymmetric encryption (so every time you encrypt the same value you get different output) then filtering will never work.因此,如果您使用非对称加密(因此每次加密相同的值都会得到不同的输出),那么过滤将永远无法工作。

Solution can be to use symmetric encryption or add a column that uses a hash (which is always the same for the same value) and filter on that.解决方案可以是使用对称加密或添加使用 hash 的列(对于相同的值始终相同)并对其进行过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM