How to persist MSAL Java token caches across platforms
devMSAL Java uses an in-memory cache by default. To persist this cache to disk securely, you can use the PersistenceTokenCacheAccessAspect provided by this extension. This class implements the ITokenCacheAccessAspect interface required by MSAL Java.
The extension handles platform-specific encryption:
- Windows: Uses DPAPI.
- macOS: Uses the MAC Keychain (encryption is built-in).
- Linux: Uses LibSecret. Note that encryption is only supported where
libsecretis available.
Important Recommendation: Use this library for Public client applications (e.g., Desktop apps) only. For web applications, persist the cache in the session to avoid scale and performance issues.
// High-level pattern:
// 1. Configure PersistenceSettings
// 2. Create PersistenceTokenCacheAccessAspect
// 3. Pass the aspect to PublicClientApplication.builder().setTokenCacheAccessAspect()