简体   繁体   English

从数据库中获取数据时解密

[英]decryption while fetching data from database

I am working on Spring MVC project with Hibernate. 我正在与Hibernate合作进行Spring MVC项目。 I have an table where some columns having encrypted data. 我有一个表,其中某些列包含加密数据。 Whenever I want to fetch data (unique result or list) I have to loop over the data and perform decryption logic then again set into models/entity. 每当我想获取数据(唯一结果或列表)时,我都必须遍历数据并执行解密逻辑,然后再次设置为模型/实体。

Currently we are doing like : 目前,我们正在做:

            query = session.createSQLQuery("SELECT C.* FROM CITY C");
            query.addEntity(City.class);                
            List<City> list = query.list();
            for (City city : list)
            {
                city.setName(AESHelper.decrypt(city.getName(), "key"));
            }

can we have something in Hibernate by using we can apply such logic while hibernate is transforming data into entity, can we use ResultTransformer for this? 我们可以在Hibernate中使用某种东西吗?我们可以在Hibernate将数据转换为实体时应用这种逻辑,是否可以为此使用ResultTransformer

You can use ResultTransformer without doubt. 您可以毫无疑问地使用ResultTransformer。 But, if you have a lot of columns/entities which requires this then use Hibernate Interceptors . 但是,如果您有很多需要此操作的列/实体,请使用Hibernate Interceptors

In your example annotate name with custom annotation like below 在您的示例中,使用以下自定义注释来注释name

City {
 @CustomAnnotation(decrypt = true) 
 private String name;
..
}

Within the interceptor when ever data is loaded, check the entities property for this custom annotation. 在拦截器中,一旦加载数据,就检查此自定义批注的实体属性。 If it is set to true then decrypt it. 如果将其设置为true,则将其解密。

This approach is non intrusive. 这种方法是非侵入性的。

Example

Update: ResultTransformer 更新:ResultTransformer

sess.createSQLQuery("SELECT name from City where name like 'xxyy'")
        .setResultTransformer(new ResultTransformer(){
   Object transformTuple(Object[] tuple, String[] aliases){
       // assign each tuple value to your object and return
   }

})

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

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