使用 MSAL for Java 在令牌缓存中获取和删除帐户Get and remove accounts from the token cache using MSAL for Java

MSAL for Java 默认提供内存中令牌缓存。MSAL for Java provides an in-memory token cache by default. 内存中令牌缓存的持续时间与应用程序实例的持续时间相同。The in-memory token cache lasts the duration of the application instance.

查看哪些帐户在缓存中See which accounts are in the cache

如以下示例所示,可以通过调用 PublicClientApplication.getAccounts() 查看哪些帐户在缓存中:You can check what accounts are in the cache by calling PublicClientApplication.getAccounts() as shown in the following example:

PublicClientApplication pca = new PublicClientApplication.Builder(
                labResponse.getAppId()).
                authority(TestConstants.ORGANIZATIONS_AUTHORITY).
                build();

Set<IAccount> accounts = pca.getAccounts().join();

从缓存中删除帐户Remove accounts from the cache

如以下示例所示,若要从缓存中删除帐户,请找到需要删除的帐户,然后调用 PublicClientApplicatoin.removeAccount()To remove an account from the cache, find the account that needs to be removed and then call PublicClientApplicatoin.removeAccount() as shown in the following example:

Set<IAccount> accounts = pca.getAccounts().join();

IAccount accountToBeRemoved = accounts.stream().filter(
                x -> x.username().equalsIgnoreCase(
                        UPN_OF_USER_TO_BE_REMOVED)).findFirst().orElse(null);

pca.removeAccount(accountToBeRemoved).join();

了解详细信息Learn more

如果使用的是适用于 Java 的 MSAL,请了解适用于 Java 的 MSAL 中的自定义令牌缓存序列化If you are using MSAL for Java, learn about Custom token cache serialization in MSAL for Java.