【ShardingSphere】ShardingSphere的加解密器


ShardingSphere默認支持AES和MD5兩種加密。

關於ShardingSphere脫敏詳細,請到官網詳閱:https://shardingsphere.apache.org/document/4.1.1/cn/features/orchestration/encrypt/

1、AES加密配置

#加密方式、密鑰配置
spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes
spring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=123456

#plainColumn表示明文列,cipherColumn表示脫敏列
spring.shardingsphere.encrypt.tables.user.columns.salary.plainColumn=
spring.shardingsphere.encrypt.tables.user.columns.salary.cipherColumn=salary
spring.shardingsphere.encrypt.tables.user.columns.salary.encryptor=encryptor_aes
# 查詢是否使用密文列
spring.shardingsphere.props.query.with.cipher.column=true

2、MD5加密配置

spring.shardingsphere.encrypt.encryptors.encryptor_md5.type=md5
spring.shardingsphere.encrypt.tables.user.columns.email.plainColumn=
spring.shardingsphere.encrypt.tables.user.columns.email.cipherColumn=email
spring.shardingsphere.encrypt.tables.user.columns.email.encryptor=encryptor_md5
# 查詢是否使用密文列
spring.shardingsphere.props.query.with.cipher.column=true

3、自定義加密

在Apache ShardingSphere中,很多功能實現類的加載方式是通過SPI注入的方式完成的。 Service Provider Interface (SPI)是一種為了被第三方實現或擴展的API,它可以用於實現框架擴展或組件替換。

1)實現自定義解密器 (實現Sharding Encryptor 接口)
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shardingsphere.encrypt.strategy.spi.Encryptor;
public class Sha256Encryptor implements Encryptor {
	private Properties properties = new Properties();

	@Override
	public void init() {
	}

	@Override
	public String encrypt(Object plaintext) {
		if (null == plaintext) {
			return null;
		}
		return DigestUtils.sha256Hex(String.valueOf(plaintext));
	}

	@Override
	public Object decrypt(String ciphertext) {
		return ciphertext;
	}
  // 加解密器的類型
	@Override
	public String getType() {
		return "SHA256";
	}

	@Override
	public Properties getProperties() {
		return properties;
	}

	@Override
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
}
2) .創建org.apache.shardingsphere.spi.encrypt.ShardingEncryptor 文件

resources/META-INF/services目錄下新增配置文件,名字為:org.apache.shardingsphere.encrypt.strategy.spi.Encryptor

配置內容:

com.wcw.encryptor.Sha256Encryptor
3)springboot配置文件
# SHA256 -> Sha256Encryptor.getType
spring.shardingsphere.encrypt.encryptors.encryptor_sha256.type=SHA256
spring.shardingsphere.encrypt.tables.user.columns.email.encryptor=encryptor_sha256

代碼下載地址:https://gitee.com/firefish/sharding-sphere-study.git


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM