[英]How to encrypt field in MongoDB
I need to encrypt one field in a mongo document. 我需要加密mongo文档中的一个字段。 What is the best way to do it?
最好的方法是什么? I use spring.
我用春天。 There is spring annotation for it?
它有弹簧注释吗?
You can use this library that adds support for @Encrypted annotation fields: 您可以使用此库添加对@Encrypted注释字段的支持:
<dependency>
<groupId>com.bol</groupId>
<artifactId>spring-data-mongodb-encrypt</artifactId>
<version>1.0.1</version>
</dependency>
To configure spring: 配置弹簧:
@Bean
public CryptVault cryptVault() {
return new CryptVault()
.with256BitAesCbcPkcs5PaddingAnd128BitSaltKey(0, oldKey)
.with256BitAesCbcPkcs5PaddingAnd128BitSaltKey(1, secretKey)
// can be omitted if it's the highest version
.withDefaultKeyVersion(1);
}
@Bean
public EncryptionEventListener encryptionEventListener(CryptVault cryptVault) {
return new EncryptionEventListener(cryptVault);
}
And to use it: 并使用它:
@Document
public class MyBean {
@Id
public String id;
// not encrypted
@Field
public String nonSensitiveData;
// encrypted primitive types
@Field
@Encrypted
public String secretString;
@Field
@Encrypted
public Long secretLong;
// encrypted sub-document (MySubBean is serialized, encrypted and stored as byte[])
@Field
@Encrypted
public MySubBean secretSubBean;
// encrypted collection (list is serialized, encrypted and stored as byte[])
@Field
@Encrypted
public List<String> secretStringList;
// values containing @Encrypted fields are encrypted
@Field
public MySubBean nonSensitiveSubBean;
// values containing @Encrypted fields are encrypted
@Field
public List<MySubBean> nonSensitiveSubBeanList;
// encrypted map (values containing @Encrypted fields are replaced by encrypted byte[])
@Field
public Map<String, MySubBean> publicMapWithSecretParts;
}
public class MySubBean {
@Field
public String nonSensitiveData;
@Field
@Encrypted
public String secretString;
}
For more info, check out the project website 有关更多信息,请查看项目网站
the encryption can be done for now only from java. 加密现在只能从java完成。 here you have the same question asked last month
在这里你上个月问同样的问题
this has been done already in ruby, so if you want to use jruby for this in your project take a look at this 这已经在ruby中完成了,所以如果你想在你的项目中使用jruby,请看看这个
or you can wait until the MongoDB includes this in their API 或者您可以等到MongoDB在其API中包含此内容
You can use custom encryption scheme and store that, into database. 您可以使用自定义加密方案并将其存储到数据库中。 In rails it will be easy to do so.
在铁轨中,这样做很容易。
include Mongoid::Document
field :encrypted_me, type: String, encrypted: true
If you can describe what platform you are using that will make some clarification. 如果您可以描述您正在使用的平台,那么将做出一些澄清。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.