如何检索 AWS Secrets Manager 密钥的特定值?
How do retrieve the specific value of an AWS Secrets Manager secret?
我有这个 java 代码可以从秘密中检索值:
public class fetchSecrets {
public static void main(String[] args) {
String secretId = "test";
SecretsManagerClient secretsClient = SecretsManagerClient.builder()
.region(Region.EU_WEST_1)
.build();
fetchPassword(secretsClient, secretId);
secretsClient.close();
}
public static void fetchPassword(SecretsManagerClient secretsClient, String secretId) {
try {
GetSecretValueRequest valueRequest = GetSecretValueRequest.builder()
.secretId(secretId)
.build();
GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
String secret = valueResponse.secretString();
System.out.println(secret);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
当我 运行 这段代码时,我得到:
{"username":"test","password":"password123456"}
如何仅输出密码或用户名密钥的值?
所以预期的输出是例如password123456
我建议执行以下操作:
首先,像这样创建一个名为 User 的 class:
public class User {
private String username;
private String password;
//setter
void setUsername(String username) {
this.username = username;
}
void setPassword(String password) {
this.password = password;
}
//getter
String getUsername(){
return this.username;
}
String getPassword(){
return this.password;
}
}
然后您可以像这样找回密码:
User user = gson.fromJson(secret , User.class);
System.out.println(user.getPassword());
原来我必须创建一个 json 对象并传入字符串以提取其值:
try {
GetSecretValueRequest request = GetSecretValueRequest.builder()
.secretId(secretId)
.build();
GetSecretValueResponse valueResponse = secretsClient.getSecretValue(request);
String secret = valueResponse.secretString();
//defining a JSON string
Object obj = JSONValue.parse(secret);
//creating an object of JSONObject class
//and casting the object into JSONObject type
JSONObject jsonObject = (JSONObject) obj;
//getting values form the JSONObject
//and casting that values into corresponding types
String username = (String) jsonObject.get("username");
//printing the values
System.out.println(username);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
输出:password123456
我有这个 java 代码可以从秘密中检索值:
public class fetchSecrets {
public static void main(String[] args) {
String secretId = "test";
SecretsManagerClient secretsClient = SecretsManagerClient.builder()
.region(Region.EU_WEST_1)
.build();
fetchPassword(secretsClient, secretId);
secretsClient.close();
}
public static void fetchPassword(SecretsManagerClient secretsClient, String secretId) {
try {
GetSecretValueRequest valueRequest = GetSecretValueRequest.builder()
.secretId(secretId)
.build();
GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
String secret = valueResponse.secretString();
System.out.println(secret);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
当我 运行 这段代码时,我得到:
{"username":"test","password":"password123456"}
如何仅输出密码或用户名密钥的值?
所以预期的输出是例如password123456
我建议执行以下操作: 首先,像这样创建一个名为 User 的 class:
public class User {
private String username;
private String password;
//setter
void setUsername(String username) {
this.username = username;
}
void setPassword(String password) {
this.password = password;
}
//getter
String getUsername(){
return this.username;
}
String getPassword(){
return this.password;
}
}
然后您可以像这样找回密码:
User user = gson.fromJson(secret , User.class);
System.out.println(user.getPassword());
原来我必须创建一个 json 对象并传入字符串以提取其值:
try {
GetSecretValueRequest request = GetSecretValueRequest.builder()
.secretId(secretId)
.build();
GetSecretValueResponse valueResponse = secretsClient.getSecretValue(request);
String secret = valueResponse.secretString();
//defining a JSON string
Object obj = JSONValue.parse(secret);
//creating an object of JSONObject class
//and casting the object into JSONObject type
JSONObject jsonObject = (JSONObject) obj;
//getting values form the JSONObject
//and casting that values into corresponding types
String username = (String) jsonObject.get("username");
//printing the values
System.out.println(username);
} catch (SecretsManagerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
输出:password123456