简体   繁体   中英

Keycloak - UserModelDelegate setters are not called

i am working with Keycloak v4.8.3.

Im currently trying to implement a UserStorageProvider with an import strategy. Im following the documentation:

https://www.keycloak.org/docs/4.8/server_development/index.html#import-implementation-strategy

This is my createAdapter method:

   protected UserModel createAdapter(RealmModel realm, String username) {
    UserModel local = session.userLocalStorage().getUserByUsername(username, realm);
    if (local == null) {
        local = session.userLocalStorage().addUser(realm, username);
        local.setFederationLink(model.getId());
        local.setEnabled(true);
        local.setFirstName("test");
        local.setLastName("test");
    }
    return new UserModelDelegate(local) {
        @Override
        public void setUsername(String username) {
            System.out.println(username);
            super.setUsername(username);
        }
        @Override
        public void setEmail(String email) {
            System.out.println(email);
            super.setEmail(email);
        }
    };
}

On login, the user adapter gets created and imported to the keycloak interal database. When i try to change the Email address on the account page, the setEmail() method is never called. Keycloak just updates the interal database.

I need to change the Email address in an external system aswell. How can i keep my external storage in sync?

We ended up not implementing it at the time, but as @monzonj asked for it i investigated the problem again. The key seems to be to implement the ImportedUserValidation interface

public class CustomUserProvider implements ImportedUserValidation {
    /*ImportedUserValidation*/
    @Override
    public UserModel validate(RealmModel realm, UserModel user) {
        return new CustomUser(user);
    }
}


public class CustomUser extends UserModelDelegate { 
    public CustomUser(UserModel delegate) {
        super(delegate);
    }

    @Override
    public void setFirstName(String firstName) {
        super.setFirstName(firstName);
        // Put your code here...
    }
}

Im not sure if this is the right way to do it, but a quick test showed, that this will call the setter when you try to change your first name on the account management page.

EDIT: We are now using keycloak v8.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM