[英]How can I pass the value I get from a class to another class?
目前我正在做一個程序,通過密鑰庫檢索 Azure 客戶端 ID 和秘密值。
下面是我和我的朋友為獲取值而制定的邏輯,我的問題是如何獲取我在static void main中獲得的值並傳遞給另一個 class 使用? 我不知道如何重用我在另一個 class 中獲得的值。 請教我。
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();
}
試試下面的代碼:
// 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.