I have implemented a UserStorageProvider using HTTP/REST calls to fetch user data from external API. For UserStorageProvider caching policy was set as DEFAULT. During authentication flow every time the UserModel is required UserStorageProvider.getUserById() is called. But it is expected that the user data will be retrieved from the cache, and not from the UserStorageProvider at least for subsequent authentications.
Сould you explain the reasons for this behavior and how to configure caching properly?
Idk really but you can implement something like this:
public class CustomUserProvider implements UserStorageProvider, UserLookupProvider, CredentialInputValidator, CredentialInputUpdater {
protected Map<String, UserModel> loadedUsers = new HashMap<>();
@Override
public UserModel getUserByUsername(RealmModel realmModel, String username) {
try {
log.info("getUserByUsername called. username: {}, realm: {}", username, realmModel);
UserModel user = loadedUsers.get(username);
if (user != null) {
log.info("User is already loaded in this transaction so rest call will be skipped");
return user;
}
CCResponseDto ccUser = restClient.getCustomerByLoginName(username);
if (ccUser == null)
return null;
user = mapUser(realmModel, ccUser);
loadedUsers.put(username, user);
return user;
} catch (Exception ex) {
log.error("Error during getting the user for CC. {}", ex.getMessage());
return null;
}
}