[keycloak-user] Keycloak + Custom AuthenticatorFactory + Spring 5

Dmitry Telegin dt at acutus.pro
Tue Nov 13 22:31:13 EST 2018


Hello Tommaso,

Spring framework needs to be initialized before you can use it, including booting IoC container etc. Keycloak itself is a pure Java EE web application; I'm not sure this is possible in principle, let alone it would likely require source code modification.

If you absolutely need to use Spring-based components, I'd recommend to deploy them as a separate WAR/EAR with Spring inside, microservice style, and use some kind of IPC/RPC to call them from your Keycloak providers.

Cheers,
Dmitry Telegin
CTO, Acutus s.r.o.
Keycloak Consulting and Training

Pod lipami street 339/52, 130 00 Prague 3, Czech Republic
+42 (022) 888-30-71
E-mail: info at acutus.pro

On Tue, 2018-11-13 at 08:01 +0100, Tommaso Tamantini wrote:
> Hi to all,
> 
>  
> 
> I'm trying to develope a custom AuthenticatorFactory with a custom
> Authenticator.
> 
>  
> 
> I would like to inject my custom Authenticator  as Spring Bean into my
> custom AuthenticatorFactory (because my authenticator should use an existing
> spring library).
> 
>  
> 
> My authenticator is like:
> 
>  
> 
> @Component
> 
> public class MyAuthenticator extends AbstractUsernameFormAuthenticator
> implements Authenticator {
> 
> [.]
> 
>  
> 
> To achieve it, I created an  ApplicationContextAware bean 
> 
>  
> 
> @Service
> 
> public class BeanUtil implements ApplicationContextAware {
> 
>  
> 
>      private static ApplicationContext applicationContext;
> 
>      
> 
>      public BeanUtil() {
> 
>      }
> 
>  
> 
>      @Override
> 
>      public void setApplicationContext(ApplicationContext
> applicationContext) throws BeansException {
> 
>           this.applicationContext = applicationContext;
> 
>      }
> 
>  
> 
>      public static Authenticator getAuthenticatorBean() { 
> 
>           return applicationContext.getBean(MyAuthenticator.class);
> 
>      } 
> 
>  
> 
> }
> 
>  
> 
> My factory is:
> 
>  
> 
> public class MyAuthenticatorFactory implements AuthenticatorFactory,
> ConfigurableAuthenticatorFactory {
> 
>  
> 
>      public static final String PROVIDER_ID = "aruba-alias-authenticator";
> 
>  
> 
>      public static final String G_RECAPTCHA_RESPONSE =
> "g-recaptcha-response";
> 
>      public static final String RECAPTCHA_REFERENCE_CATEGORY = "recaptcha";
> 
>      public static final String SITE_KEY = "site.key";
> 
>      public static final String NUMBER_KEY = "number.key";
> 
>      public static final String SITE_SECRET = "secret";
> 
>  
> 
>      @Override
> 
>      public String getId() {
> 
>           return PROVIDER_ID;
> 
>      }
> 
>  
> 
>      @Override
> 
>      public MyAuthenticator create(KeycloakSession session) {
> 
>           return BeanUtil.AuthenticatorBean();
> 
>      }
> 
>  
> 
> [.]
> 
>  
> 
> Keycloak starts up correctly. 
> 
>  
> 
> When I try to use myAuthenticator, i get: 
> 
>  
> 
>  
> 
> 16:46:48,484 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http
> management interface listening on http://0.0.0.0:9990/management
> 
> sia-keycloak | 16:46:48,484 INFO  [org.jboss.as] (Controller Boot Thread)
> WFLYSRV0051: Admin console listening on http://0.0.0.0:9990
> 
> sia-keycloak | 16:46:48,485 INFO  [org.jboss.as] (Controller Boot Thread)
> WFLYSRV0025: Keycloak 4.5.0.Final (WildFly Core 5.0.0.Final) started in
> 23456ms - Started 943 of 1231 services (653 services are lazy, passive or
> on-demand)
> 
> sia-keycloak | 16:47:12,357 WARN  [org.keycloak.services] (default task-3)
> KC-SERVICES0013: Failed authentication: java.lang.NullPointerException
> 
> sia-keycloak |  at
> ...authenticator.alias.BeanUtil.getArubaAliasAuthenticatorBean(BeanUtil.java
> :22)
> 
> sia-keycloak |  at
> ..authenticator.alias.AuthenticatorFactory.create(MyAuthenticatorFactory.jav
> a:35)
> 
> sia-keycloak |  at
> ...authenticator.alias.AuthenticatorFactory.create(MyAuthenticatorFactory.ja
> va:1)
> 
>  
> 
>  
> 
> The reason in that the Spring Context is null. 
> 
>  
> 
> Any idea about how to fix this issue?
> 
>  
> 
>  
> 
> Many thanks, 
> 
> Tom
> 
>  
> 
>  
> 
>  
> 
>  
> 
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user


More information about the keycloak-user mailing list