如何使用自定义注释存储和加载加密值 [英] How can I store and load an encrypted value using custom annotation
问题描述
我是Java自定义注释的新手
I am new to Java custom annotations
我正在开发自定义注释,该注释可以加密和解密字符串并使用spring和mongodb将其存储在数据库中,并且为了进行加密,我使用jasypt.
I am developing a custom annotation which encrypt and decrypt a string and store it in database using spring and mongodb and for encryption I am using jasypt.
我没有确切的操作步骤.
I am not getting the exact procedure to do so.
我的代码.
实体
public class Demo {
@Id
private Long id;
private String somethingPublic;
@EncryptDemo()
private String somethingPrivate;
//getter setter
}
自定义注释
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptDemo {
}
在存储实体之前,如何在自定义注释中添加加密行为.
我应该在哪里添加加密代码,以便在调用时反映到我的注释中.
Where should I add code for encryption that reflect to my annotation when it called.
我想开发一个像冬眠一样的注释.
I want to develop an annotation same like hibernate.
任何帮助将不胜感激.提前致谢.
Any help will be appreciate. Thanks in advance.
推荐答案
基本上,您需要的是
- 创建一个
AbstractMongoEventListener
来侦听AfterConvertEvent
和BeforeSaveEvent
事件 - 实施
org.springframework.util.ReflectionUtils.FieldCallback
回调以对这些事件执行操作 - 在Spring Data mongodb配置类中将侦听器注册为Bean
- Create an
AbstractMongoEventListener
to listen forAfterConvertEvent
andBeforeSaveEvent
events - Implement
org.springframework.util.ReflectionUtils.FieldCallback
callbacks to do actions on those events - Register the listener as a Bean in your Spring Data mongodb configuration class
听众:
public class EncryptionMongoEventListener extends AbstractMongoEventListener<Object> {
@Override
public void onBeforeSave(BeforeSaveEvent<Object> event) {
Object source = event.getSource();
DBObject dbObject = event.getDBObject();
ReflectionUtils.doWithFields(source.getClass(),
new EncryptCallback(source, dbObject),
ReflectionUtils.COPYABLE_FIELDS);
}
@Override
public void onAfterConvert(AfterConvertEvent<Object> event) {
Object source = event.getSource();
ReflectionUtils.doWithFields(source.getClass(),
new DecryptCallback(source),
ReflectionUtils.COPYABLE_FIELDS);
}
}
加密回调:
class EncryptCallback implements FieldCallback {
private final Object source;
private final DBObject dbObject;
public EncryptCallback(final Object source, final DBObject dbObject) {
this.source = source;
this.dbObject = dbObject;
}
@Override
public void doWith(Field field)
throws IllegalArgumentException, IllegalAccessException {
if (!field.isAnnotationPresent(/* your annotation */.class)) {
return;
}
ReflectionUtils.makeAccessible(field);
String plainText = (String) ReflectionUtils.getField(field, source);
String encryptedValue = /* your encryption of plainText */;
// update the value in DBObject before it is saved to mongodb
dbObject.put(field.getName(), encryptedValue);
}
}
解密回调:
class DecryptCallback implements FieldCallback {
private final Object source;
public DecryptCallback(Object source) {
this.source = source;
}
@Override
public void doWith(Field field)
throws IllegalArgumentException, IllegalAccessException {
if (!field.isAnnotationPresent(/* your annotation */.class)) {
return;
}
ReflectionUtils.makeAccessible(field);
String fieldValue = (String) ReflectionUtils.getField(field, source);
String decryptedValue = /* your decryption of fieldValue */;
// set the decrypted value in source Object
ReflectionUtils.setField(field, source, decryptedValue);
}
}
最后,在您的Spring Data mongodb配置类中将侦听器注册为Bean
and finally, register the listener as a bean in your Spring Data mongodb configuration class
@Bean
public EncryptionMongoEventListener encryptionMongoEventListener() {
return new EncryptionMongoEventListener();
}
这篇关于如何使用自定义注释存储和加载加密值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!