此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Couchbase 5.4.0! |
Couchbase Field Level 加密
Couchbase 支持字段级加密。本节记录了如何将其与 Spring Data Couchbase 一起使用。
概述
使用 com.couchbase.client.java.encryption.annotation.Encrypted (@Encrypted) 注释的字段将在写入时自动加密,在读取时解密。可以通过指定 @Encrypted (migration = Encrypted.Migration.FROM_UNENCRYPTED) 将未加密的字段迁移到加密字段。
入门和配置
依赖
字段级加密可通过依赖项使用(请参阅字段级加密 )
<groupId>com.couchbase.client</groupId>
<artifactId>couchbase-encryption</artifactId>
HashiCorp Vault Transit 集成需要 Spring Vault
<groupId>org.springframework.vault</groupId>
<artifactId>spring-vault-core</artifactId>
提供 CryptoManager
需要通过覆盖 AbstractCouchbaseConfiguration 中的 cryptoManager() 方法来提供 CryptoManager。Spring Data Couchbase 以及从 CouchbaseClientFactory 进行的 Couchbase Java SDK 直接调用都将使用此CryptoManager。
@Override
protected CryptoManager cryptoManager() {
KeyStore javaKeyStore = KeyStore.getInstance("MyKeyStoreType");
FileInputStream fis = new java.io.FileInputStream("keyStoreName");
char[] password = { 'a', 'b', 'c' };
javaKeyStore.load(fis, password);
Keyring keyring = new KeyStoreKeyring(javaKeyStore, keyName -> "swordfish");
// AES-256 authenticated with HMAC SHA-512. Requires a 64-byte key.
AeadAes256CbcHmacSha512Provider provider = AeadAes256CbcHmacSha512Provider.builder().keyring(keyring).build();
CryptoManager cryptoManager = DefaultCryptoManager.builder().decrypter(provider.decrypter())
.defaultEncrypter(provider.encrypterForKey("myKey")).build();
return cryptoManager;
}
将字段定义为 Encrypted。
-
@Encrypted 将字段定义为 encrypted。
-
@Encrypted(migration = Encrypted.Migration.FROM_UNENCRYPTED) 定义一个字段,该字段在读取时可能会加密,也可能不会加密。写入时将被加密。
-
@Encrypted(encrypter = “<encrypterAlias>”) 指定要用于加密的加密器的别名。请注意,这不是算法,而是在将加密程序添加到 CryptoManager 时指定的名称。
例
@Configuration
@EnableCouchbaseRepositories("<parent-dir-of-repository-interfaces>")
@EnableReactiveCouchbaseRepositories("<parent-dir-of-repository-interfaces>")
static class Config extends AbstractCouchbaseConfiguration {
// Usual Setup
@Override public String getConnectionString() { /* ... */ }
@Override public String getUserName() { /* ... */ }
@Override public String getPassword() { /* ... */ }
@Override public String getBucketName() { /* ... */ }
/* provide a cryptoManager */
@Override
protected CryptoManager cryptoManager() {
KeyStore javaKeyStore = KeyStore.getInstance("MyKeyStoreType");
FileInputStream fis = new java.io.FileInputStream("keyStoreName");
char[] password = { 'a', 'b', 'c' };
javaKeyStore.load(fis, password);
Keyring keyring = new KeyStoreKeyring(javaKeyStore, keyName -> "swordfish");
// AES-256 authenticated with HMAC SHA-512. Requires a 64-byte key.
AeadAes256CbcHmacSha512Provider provider = AeadAes256CbcHmacSha512Provider.builder().keyring(keyring).build();
CryptoManager cryptoManager = DefaultCryptoManager.builder().decrypter(provider.decrypter())
.defaultEncrypter(provider.encrypterForKey("myKey")).build();
return cryptoManager;
}
}
@Document
public class AddressWithEncStreet extends Address {
private @Encrypted String encStreet;
.
.
AddressWithEncStreet address = new AddressWithEncStreet(); // plaintext address with encrypted street
address.setCity("Santa Clara");
address.setEncStreet("Olcott Street");
addressEncryptedRepository.save(address);
{
"_class": "AddressWithEncStreet",
"city": "Santa Clara",
"encrypted$encStreet": {
"alg": "AEAD_AES_256_CBC_HMAC_SHA512",
"ciphertext": "A/tJALmtixTxqj77ZUcUgMklIt3372DKD7l5FvbCzHNJMplbgQEv0RgSbxIfiRNr+uW2H7cokkcCW/F5YnQoXA==",
"kid": "myKey"
}
}