javaauthenticationkeycloakkeycloak-services

Keycloak not accepting a/any custom LoginFormsProviders


I am using Keycloak 25 and I am trying to modify/extend/replace the FreeMarkerLoginFormsProvider via the keycloak SPI. I have tried using a method provided on several different questions. Like this one here : keycloak Custom Login provider not getting invoked with version 19 but unsuccessfully. I still get the error:

ERROR [org.keycloak.services.error.KeycloakErrorHandler] (executor-thread-14) Uncaught server error: java.lang.NullPointerException: Cannot invoke "org.keycloak.forms.login.LoginFormsProvider.setAuthenticationSession(org.keycloak.sessions.AuthenticationSessionModel)" because the return value of "org.keycloak.models.KeycloakSession.getProvider(java.lang.Class)" is null.

Here is my code :

public class CustomLoginFormsProvider extends FreeMarkerLoginFormsProvider {

    private static final Logger logger = Logger.getLogger(CustomLoginFormsProvider.class);


    public CustomLoginFormsProvider(KeycloakSession session) {
        super(session);
        logger.info("Injecting custom login forms provider!");
    }

}

and

    public class CustomLoginFormsProviderFactory extends FreeMarkerLoginFormsProviderFactory {
    
        public static final String PROVIDER_ID = "custom-login-provider";
        @Override
        public LoginFormsProvider create(KeycloakSession session) {
            return new CustomLoginFormsProvider(session);
        }
    
        @Override
        public String getId() {
            return PROVIDER_ID;
        }
}

and I have created a file in META-INF org.keycloak.forms.login.LoginFormsProviderFactory with my custom class inside. I also start the server with --spi-login-provider=custom-login-provider --spi-login-custom-login-provider-enabled=true I have also tried copying the entire classes and implement respectively LoginFormsProvider and LoginFormsProviderFactory classes.

Is this no longer supported in keycloak ? How can I solve the NULL pointer ?

EDIT: After headbanging for a while I have crossposted this question here

Currently it does not contain a solution either but it may in the future. You can find a workaround of the problem in the answers by me.


Solution

  • Sadly enough I have to answer my own question and I will in case anybody else finds himself in the same situation as I did.

    Keycloak's logic determines the order of the providers that have to be loaded in the following order:

    1. Explicitly configured default provider
    2. Provider with the highest order.(0<= are ignored).
    3. The provider with id set to default.

    This means that as a workaround:

        @Override
        public int order() {
            return 1;
        }
    

    Order method must be explicitly specified with highest priority.