在用spring boot environmentpostprocessor加载application.yml之前获取azure密钥保险存储值

s5a0g9ez  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(184)

在加载配置(application.yml)之前,我需要从密钥库中获取一个值,因为该密钥用于解密配置文件中的敏感值。
当从environmentpostprocessor访问azure密钥库时,它将显示如下错误消息。但在其他进程(不是后处理器)中,它可以成功地访问。
javax.naming.serviceunavailableexception:身份验证结果为null
下面是environmentpostprocessor中的代码。

public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor {
    protected static final Logger LOGGER = LoggerFactory.getLogger(LocalSettingsEnvironmentPostProcessor.class);
    private String keyVaultName = "......";
    private String tenantId = "......";
    private String clientId = "......";
    private String clientKey = "......";
    private String keyName = "......";
    private String keyVersion = "......";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
        MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
        String baseUrl = "https://" + keyVaultName + ".vault.azure.cn:{port}/secrets/";
        String url = baseUrl + keyName + "/" + keyVersion;
        try {
            Properties properties = new Properties();
            LOGGER.info("postProcessor get key from keyvault: ", KeyVaultClient.getInstance().getKeyVal(clientId, clientKey, tenantId, url));
            properties.setProperty("jasypt.encryptor.password", KeyVaultClient.getInstance().getKeyVal(clientId, clientKey, tenantId, url));
            propertySources.addFirst(new PropertiesPropertySource("Config", properties));
            return;
        } catch (Exception e) {
            LOGGER.error("postProcessEnvironment error:", e);
            System.exit(0);
        }
    }
}

或者在加载配置之前,是否有其他方法从密钥库获取值?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题