Spring Boot集成ShardingSphere实现数据加密及数据脱敏 | Spring Cloud 48
@Configuration
public class ShardingSphereConfig {
@Bean
public EncryptRuleConfiguration getEncryptRuleConfiguration() {
EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("AES", "xxx", "xxx");
EncryptColumnRuleConfiguration columnConfig = new EncryptColumnRuleConfiguration("encrypt_assistant", "age", "age_cipher", "age_plain", "");
Map<String, EncryptColumnRuleConfiguration> columns = Collections.singletonMap("age", columnConfig);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columns);
Map<String, EncryptTableRuleConfiguration> tables = Collections.singletonMap("t_encrypt_assistant", tableConfig);
return new EncryptRuleConfiguration(Collections.singleton(encryptorConfig), tables);
}
@Bean
public Properties getShardingSphereProperties() {
Properties properties = new Properties();
// 配置加密的密钥
properties.setProperty("encryptor.encryptor.key.value", "xxx");
return properties;
}
@Bean
public EncryptRule encryptRule(final EncryptRuleConfiguration encryptRuleConfiguration, final Properties properties) throws SQLException {
return new EncryptRule(encryptRuleConfiguration, properties);
}
}
这个配置类定义了如何配置ShardingSphere的数据加密规则。首先,它创建了一个EncryptorRuleConfiguration
实例,指定了加密器的类型和密钥。然后,它定义了一个EncryptColumnRuleConfiguration
实例,指定了需要加密的列和加密后的列名。最后,它创建了一个EncryptRuleConfiguration
实例,将加密器和加密列规则组合在一起,并提供了一个EncryptRule
的Bean,用于在ShardingSphere中使用加密规则。
评论已关闭