【问题标题】:How can I pass the value I get from a class to another class?如何将我从一个类获得的值传递给另一个类?
【发布时间】:2021-06-19 16:19:34
【问题描述】:

目前我正在做一个通过密钥库检索 Azure 客户端 ID 和秘密值的程序。

下面是我和朋友获取值的逻辑,我的问题是如何将我在 static void main 中得到的值传递给另一个类使用?我不知道如何重用我在另一个课程中获得的价值。请教我。

public class SecretReceiver {

private static SecretClient secretClient;

public static final String AZURE_CLIENT_ID="AZURE_CLIENT_ID";
public static final String AZURE_CLIENT_SECRET="AZURE_CLIENT_SECRET";
public static final String AZURE_TENANT_ID="AZURE_TENANT_ID";
public static final String AZURE_KEY_VAULT_NAME="AZURE_KEY_VAULT_NAME";

private static final String KEY_VAULT_URL = "https://%s.vault.azure.net";

private static void secretReceiverBuilder() {
    if (secretClient == null) {
        String keyVaultUrl = String.format(KEY_VAULT_URL, getProperty(AZURE_KEY_VAULT_NAME, ""));
        secretClient = new SecretClientBuilder()
                .vaultUrl(keyVaultUrl)
                .credential(new ClientSecretCredentialBuilder()
                        .clientId(getProperty(AZURE_CLIENT_ID, ""))
                        .clientSecret(getProperty(AZURE_CLIENT_SECRET, ""))
                        .tenantId(getProperty(AZURE_TENANT_ID, ""))
                        .build())
                .buildClient();
    }
}

public static void loadConfigFileAndSetEnv(String filePath) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))))) {
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] split = line.split("=");
            if (split.length > 1) {
                String key = split[0].trim();
                String value = split[1].trim();
                if (key.contains(AZURE_CLIENT_ID)) {
                    System.setProperty(AZURE_CLIENT_ID, value);
                    continue;
                }
                if (key.contains(AZURE_CLIENT_SECRET)) {
                    System.setProperty(AZURE_CLIENT_SECRET, value);
                    continue;
                }
                if (key.contains(AZURE_TENANT_ID)) {
                    System.setProperty(AZURE_TENANT_ID, value);
                    continue;
                }
                if (key.contains(AZURE_KEY_VAULT_NAME)) {
                    System.setProperty(AZURE_KEY_VAULT_NAME, value);
                }
            }
        }
    } catch (Exception e) {
        log.error("Load file : {} error.", filePath, e);
    }
}

public static String getProperty(String key, String defaultValue) {
    String value = System.getProperty(key);
    if (StringUtils.isBlank(value)) {
        value = System.getenv(key);
    }
    return StringUtils.isBlank(value) ? defaultValue : value;
}

public static void main() throws Exception {
    //getUatAKV();
    loadConfigFileAndSetEnv("C:script\\key_vault.conf");
    String username = getSecretByKey("client-secret-name");
    String secret = getSecretByKey("client-secret");
    System.out.println("This is client id: " + username);
    System.out.println("This is client secret: " + secret);
}

public static String getSecretByKey(String name) {
    if (secretClient == null) {
        secretReceiverBuilder();
    }
    return secretClient.getSecret(name).getValue();
}

【问题讨论】:

  • 为什么不将它们作为参数传递给构造函数或 secretReceiverBuilder()?

标签: java spring azure


【解决方案1】:

试试下面的代码:

// Create static variable in a Class
public class Global {

    public static String USER_NAME ="";
    public static String SECRET ="";

}

// Set the value in your main function
String username = getSecretByKey("client-secret-name");
String secret = getSecretByKey("client-secret");
Global.USER_NAME = username;
Global.SECRET = secret;

// Get value in another class
System.out.println("This is client id: " + Global.USER_NAME);
System.out.println("This is client secret: " + Global.SECRET);

【讨论】:

  • system.out.printin也放入main函数中?
  • @BingHao 在另一个班级。
  • 嘿,谢谢。有用!!真的帮了我很多!!谢谢!!
猜你喜欢
  • 1970-01-01
  • 2018-02-05
  • 2014-02-24
  • 2013-03-16
  • 1970-01-01
  • 2011-08-26
  • 2017-10-30
相关资源
最近更新 更多