请问 shardingsphere-proxy 自定义加解密

请问shardingsphere-proxy怎么实现自定义加解密,如我已经implements Encryptor了,如何配置到proxy才生效

使用二进制发布包 :: ShardingSphere 可以参考下这个.

参考这个后,我把jar包放进ext-lib中了,还报以下错误

2022/1/19 上午11:21:37Exception in thread "main" java.util.ServiceConfigurationError: org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm: Provider com.hang.encryptor.spi.UserNameEncryptor not found
2022/1/19 上午11:21:37	at java.util.ServiceLoader.fail(ServiceLoader.java:239)
2022/1/19 上午11:21:37	at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
2022/1/19 上午11:21:37	at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
2022/1/19 上午11:21:37	at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
2022/1/19 上午11:21:37	at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.spi.ShardingSphereServiceLoader.load(ShardingSphereServiceLoader.java:53)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.spi.ShardingSphereServiceLoader.register(ShardingSphereServiceLoader.java:47)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.encrypt.rule.EncryptRule.<clinit>(EncryptRule.java:54)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.encrypt.rule.builder.EncryptRuleBuilder.build(EncryptRuleBuilder.java:36)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.encrypt.rule.builder.EncryptRuleBuilder.build(EncryptRuleBuilder.java:32)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder.buildRules(SchemaRulesBuilder.java:85)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder.buildRules(SchemaRulesBuilder.java:70)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.mode.manager.memory.MemoryContextManagerBuilder.build(MemoryContextManagerBuilder.java:53)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.initContext(BootstrapInitializer.java:77)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.init(BootstrapInitializer.java:68)
2022/1/19 上午11:21:37	at org.apache.shardingsphere.proxy.Bootstrap.main(Bootstrap.java:47)

不知哪里搞错了

方便提供一个 demo 吗? 或者说把你的配置和实现发出来看看.

(1)config-encrypt.yaml 如下:

(2)自定义的加解密类
image

能把自定义的加密类发全吗? 还有就是最好是文字的形式发一下, 我这边方便直接使用.

好的,谢谢。

//自定义加密类
public class TestEncryptor implements EncryptAlgorithm {

    protected byte[] secretKey;

    @Override
    public void init() {
        secretKey = createSecretKey();
    }

    @SneakyThrows(GeneralSecurityException.class)
    @Override
    public String encrypt(final Object plaintext) {
        if (null == plaintext) {
            return null;
        }
        byte[] result = getCipher(Cipher.ENCRYPT_MODE).doFinal(StringUtils.getBytesUtf8(String.valueOf(plaintext)));
        return Base64.encodeBase64String(result);
    }

    @SneakyThrows(GeneralSecurityException.class)
    @Override
    public Object decrypt(final String ciphertext) {
        byte[] result = getCipher(Cipher.DECRYPT_MODE).doFinal(Base64.decodeBase64(ciphertext));
        String strResult = new String(result, StandardCharsets.UTF_8);
        strResult = strResult.replaceAll("(\\S)\\S(\\S*)", "$1*$2");
        return strResult;
    }

    //SPI加载时要用到的标识
    @Override
    public String getType() {
        return "userName";
    }

    private byte[] createSecretKey() {
        return Arrays.copyOf(DigestUtils.sha1("123456"), 16);
    }

    protected String getEncryptType() {
        return "AES";
    }

    protected Cipher getCipher(final int decryptMode)
            throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
        Cipher result = Cipher.getInstance(getEncryptType());
        result.init(decryptMode, new SecretKeySpec(secretKey, getEncryptType()));
        return result;

    }

//配置文件
schemaName: encrypt_db

dataSources:
  ds_0:
    url: jdbc:mysql://192.168.1.105:33061/dpim?characterEncoding=utf-8
    username: root
    password: fu2019
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
rules:
- !ENCRYPT
  encryptors:
    aes_encryptor:
      type: AES
      props:
        aes-key-value: 123456abc
    userName_encryptor:
      type: userName
      props:
        aes-key-value: 123456
#    md5_encryptor:
#      type: MD5
  tables:
    zx_card_info:
      columns:
        name:
          cipherColumn: name
          encryptorName: userName_encryptor
        mobile:
          cipherColumn: mobile
          encryptorName: aes_encryptor

我这边尝试是可以的, 给你附上我的截图, 还有 example.





shardingsphere-sharding-db-table-demo.zip (34.9 KB)

1 个赞

好的,奇怪了,您是用5.0.0版本的吗?我再试试看,感谢您的回复!

嗯, 这个跟版本没什么关系, 5.0.0 是支持 SPI 的. 所以检查下您的配置和 jar 包

谢谢!发现是我打包成jar的问题,我参考这个文章解决了。SpringBoot:解决可执行jar包不能被其他项目依赖问题 - 走看看

1 个赞

好的, 那恭喜你了.

大神,我的自定义类通过配置读aes-key-value 一直提示为空,如下:

2022/1/24 下午4:35:25Exception in thread "main" java.lang.IllegalArgumentException: aes-key-value can not be null.
2022/1/24 下午4:35:25	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:142)
2022/1/24 下午4:35:25	at com.hang.plugin.encrypt.spi.BaseEncryptor.createSecretKey(BaseEncryptor.java:46)
2022/1/24 下午4:35:25	at com.hang.plugin.encrypt.spi.BaseEncryptor.init(BaseEncryptor.java:29)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory.createAlgorithm(ShardingSphereAlgorithmFactory.java:43)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.encrypt.rule.EncryptRule.lambda$new$0(EncryptRule.java:66)
2022/1/24 下午4:35:25	at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.encrypt.rule.EncryptRule.<init>(EncryptRule.java:66)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.encrypt.rule.builder.EncryptRuleBuilder.build(EncryptRuleBuilder.java:36)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.encrypt.rule.builder.EncryptRuleBuilder.build(EncryptRuleBuilder.java:32)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder.buildRules(SchemaRulesBuilder.java:85)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder.buildRules(SchemaRulesBuilder.java:70)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.mode.manager.memory.MemoryContextManagerBuilder.build(MemoryContextManagerBuilder.java:53)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.initContext(BootstrapInitializer.java:77)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.proxy.initializer.BootstrapInitializer.init(BootstrapInitializer.java:68)
2022/1/24 下午4:35:25	at org.apache.shardingsphere.proxy.Bootstrap.main(Bootstrap.java:47)

userName_encryptor 调整为 userName-encryptor.

你好,还是一样的错误,不知是在时候什么把Properties写入到自定义的EncryptAlgorithm,或者你能否提供一个demo,哈

demo 上面不是提供了吗? 你看看里面的配置.

我知道了,要默认定义相同的变量才行如下:

private Properties props = new Properties();

Cool !!!

京ICP备2021015875号