The data encryption Java API rule configuration allows users to directly create ShardingSphereDataSource objects by writing java code. The Java API configuration method is very flexible and can integrate various types of business systems without relying on additional jar packages.
Class name: org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration
Attributes:
Name | DataType | Description | Default Value |
---|---|---|---|
tables (+) | Collection<EncryptTableRuleConfiguration> | Encrypt table rule configurations | |
encryptors (+) | Map<String, AlgorithmConfiguration> | Encrypt algorithm name and configurations |
Class name: org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration
Attributes:
Name | DataType | Description |
---|---|---|
name | String | Table name |
columns (+) | Collection<EncryptColumnRuleConfiguration> | Encrypt column rule configurations |
Class name: org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration
Attributes:
Name | DataType | Description |
---|---|---|
logicColumn | String | Logic column name |
cipherColumn | String | Cipher column name |
assistedQueryColumn (?) | String | Assisted query column name |
encryptorName | String | Encrypt algorithm name |
assistedQueryEncryptorName | String | Assisted query encrypt algorithm name |
Class name: org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration
Attributes:
Name | DataType | Description |
---|---|---|
name | String | Encrypt algorithm name |
type | String | Encrypt algorithm type |
properties | Properties | Encrypt algorithm properties |
Please refer to Built-in Encrypt Algorithm List for more details about type of algorithm.
public final class EncryptDatabasesConfiguration implements ExampleConfiguration {
@Override
public DataSource getDataSource() {
Properties props = new Properties();
props.setProperty("aes-key-value", "123456");
EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("username", "username", "", "username_plain", "name_encryptor", null);
EncryptColumnRuleConfiguration columnConfigTest = new EncryptColumnRuleConfiguration("pwd", "pwd", "assisted_query_pwd", "", "pwd_encryptor", null);
EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_user", Arrays.asList(columnConfigAes, columnConfigTest), null);
Map<String, AlgorithmConfiguration> encryptAlgorithmConfigs = new LinkedHashMap<>(2, 1);
encryptAlgorithmConfigs.put("name_encryptor", new AlgorithmConfiguration("AES", props));
encryptAlgorithmConfigs.put("pwd_encryptor", new AlgorithmConfiguration("assistedTest", props));
EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), encryptAlgorithmConfigs);
try {
return ShardingSphereDataSourceFactory.createDataSource(DataSourceUtil.createDataSource("demo_ds"), Collections.singleton(encryptRuleConfig), props);
} catch (final SQLException ex) {
ex.printStackTrace();
return null;
}
}
}