1

我在 Wildfly 17.x 凭证存储中存储了几个应用程序密码。如何以编程方式从凭证存储访问存储的密码?

这就是创建凭证存储并将密码存储在其中的方式。

/subsystem=elytron/credential-store=test:add(relative-to=jboss.server.data.dir, location=test.jceks, create=true,credential-reference={clear-text=storepass})

/subsystem=elytron/credential-store=test:add-alias(alias=keystorepw,secret-value=secret)
4

3 回答 3

3

首先原谅我用英文写作。我现在使用此代码的最佳方式是使用库 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();
}

此方法基于您的凭证存储。

于 2020-08-02T01:09:40.380 回答
2

我在不同的扩展而不是 jceks 中创建了商店。一旦解决了这个问题,我就可以从商店读取密码。花了一些时间才弄清楚这一点,因为 WildFly 在创建商店时没有抱怨,并且除了以编程方式阅读之外一切正常。

于 2020-03-16T15:10:35.230 回答
-1

如果您正在寻找一个完整的示例,请查看https://github.com/wildfly-security-incubator/elytron-examples/blob/master/credential-store/src/main/java/org/wildfly/security/ examples/CredentialStoreExample.java 你可以在那里看到,cs(那里命名为 CREDENTIAL_STORE_PROVIDER)和 elytronProvider(那里命名为 PASSWORD_PROVIDER)是通过调用适当的构造函数创建的: private static final Provider CREDENTIAL_STORE_PROVIDER = new WildFlyElytronCredentialStoreProvider(); 私有静态最终提供者 PASSWORD_PROVIDER = new WildFlyElytronPasswordProvider();

于 2021-06-24T06:59:53.830 回答