首先原谅我用英文写作。我现在使用此代码的最佳方式是使用库 Maven 版本 1.12.1.Final。其他库,如最近的 Alpha,此代码有错误。
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron</artifactId>
<version>1.12.1.Final</version>
</dependency>
方法
public Password giveMeAPass(String alias) throws NoSuchAlgorithmException, CredentialStoreException, InvalidKeySpecException {
/*
* Create a ProtectionParameter for access to the store.
*/
Password storePassword = ClearPassword.createRaw(
ClearPassword.ALGORITHM_CLEAR,
"storepass".toCharArray());
ProtectionParameter protectionParameter = new CredentialSourceProtectionParameter(
IdentityCredentials.NONE.withCredential(
new PasswordCredential(storePassword)));
Security.addProvider(elytronProvider);
CredentialStore credentialStore = CredentialStore.getInstance(
"KeyStoreCredentialStore", csProvider);
// Configure and Initialise the CredentialStore
String configPath = System.getProperty("jboss.server.data.dir");
Map<String, String> configuration = new HashMap<>();
String path = configPath + File.separator + "test.jceks";
configuration.put("keyStoreType", "JCEKS");
configuration.put("location", path);
configuration.put("modifiable", "false");
//Inicialize credentialStore
credentialStore.initialize(configuration, protectionParameter);
return credentialStore.retrieve(alias, PasswordCredential.class).getPassword();
}
此方法基于您的凭证存储。