Solon

solon.vault

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon.vault</artifactId>
</dependency>

1、描述

基础扩展插件,提供项目的配置脱敏支持。(比如,数据库连接的信息)

2、配置示例

solon.vault:
  password: "liylU9PhDq63tk1C"

3、代码应用

  • 使用工具类生成密文
public class TestApp {
    public static void main(String[] args) throws Exception{
        Solon.start(TestApp.class, args);

        //打印生成的密文
        System.out.println(VaultUtils.encrypt("root"));
    }
}
  • 使用生成的密文配置敏感信息,并使用专有注解注入
solon.vault:
  password: "liylU9PhDq63tk1C"

test.db1:
  url: "..."
  username: "ENC(xo1zJjGXUouQ/CZac55HZA==)"
  password: "ENC(XgRqh3C00JmkjsPi4mPySA==)"

使用 @VaultInject 做密文配置的注入:

@Configuration
public class TestConfig {
    @Bean("db2")
    private DataSource db2(@VaultInject("${test.db1}") HikariDataSource ds){
        return ds;
    }
}

4、定制加密算法(没事儿,不用搞定制)

@Component
public class VaultCoderImpl implements VaultCoder {
    private final String password;
    
    public VaultCoderImpl() {
        this.password = Solon.cfg().get("solon.vault.password");
    }
    
    @Override
    public String encrypt(String str) throws Exception {
        return null;
    }

    @Override
    public String decrypt(String str) throws Exception {
        return null;
    }
}

//或者

@Configuration
public class Config {
    @Bean
    public VaultCoder vaultCoderInit(){
        return new AesVaultCoder();
    } 
}