Override Keyclock JpaUserProvider

66 Views Asked by At

Dose anybody know how overide Keyclock JpaUserProvider using extension?

Hi. It is required to call an external microservice to check the license when adding a user and roles to the keyclock. Can anyone know how to do this?

1

There are 1 best solutions below

1
Jon On

To override the JpaUserProvider with extension, you need to create a custom SPI (Service Provider Interface). Use the UserProvider interface to create a custom user provider and override the methods you think you might need. Here's an example:

 public class CustomUserProvider implements UserProvider {
        
        private final EntityManager em;
        
        public CustomUserProvider(EntityManager em) {
            this.em = em;
        }

        @Override
        public UserModel addUser(RealmModel realm, String username) {
            UserModel user = ... // Add user to your database
            
            // Call your external microservice here
            
            return user;
        }
        
        // Override other methods...
    }

    public class CustomUserProviderFactory implements UserProviderFactory {

        @Override
        public UserProvider create(KeycloakSession session) {
            EntityManager em = session.getProvider(JpaConnectionProvider.class).getEntityManager();
            return new CustomUserProvider(em);
        }
        
        // Implement other methods...
    }

Note: You might want to look at this article for more info.