实施自定义Keycloak身份验证器SPI

时间:2019-07-03 10:04:34

标签: java keycloak

我正在尝试实现自定义密钥克隆Authenticator SPI,以针对外部身份提供程序进行身份验证。用户已经存在于密钥库存储中,我只需要连接到自定义SPI即可对其进行身份验证。

我正在遵循官方指南https://www.keycloak.org/docs/latest/server_development/index.html#_auth_spi_walkthrough的8.3节,该节与我所需要的非常相似。

我遇到的问题是之后,身份验证流运行到自定义Authenticator的“ action”方法中,从AuthenticationProcessor类引发了异常,检查,来自以下检查:

 // org.keycloak.authentication.AuthenticationProcessor - line 876
    if (authenticationSession.getAuthenticatedUser() == null) {
         throw new AuthenticationFlowException(AuthenticationFlowError.UNKNOWN_USER);
    } 

看到此问题后,我尝试解决此问题的想法是从密钥库存储区中获取用户(已通过外部身份提供者验证),并将其推入AuthenticationSession中,如下所示:

// Connect against external Service Provider
// and asume "USER_ID" represents an already validated User

// AuthenticationFlowContext = afc is given as parameter
UserFederationManager ufm = afc.getSession().users();   // <-- PROBLEM
UserModel userFound = ufm.getUserById("USER_ID", afc.getRealm());

if (userFound != null) {
    // get reference to the authSession
    AuthenticationSessionModel asm = afc.getAuthenticationSession();
    // set authenticated user on the session
    asm.setAuthenticatedUser(userFound );
    return true;
}
return false;

上述代码的问题是,与users()类的org.keaycloak.models.KeycloackSession方法有关的Java NoSuchMethodExceptionError被抛出。像这样:

  

11:26:32,628错误[org.keycloak.services.error.KeycloakErrorHandler](默认任务14)未捕获的服务器错误:java.lang.NoSuchMethodError:org.keycloak.models.KeycloakSession.users()Lorg / keycloak / models / UserFederationManager;

任何可以帮助我解决此问题的建议将不胜感激!

2 个答案:

答案 0 :(得分:1)

正如亨利所说,这很可能是版本冲突。我遇到了类似的问题,该问题在this thread的帮助下得以解决。它建议您降级某些依赖项版本,但就我而言,我们解决了将其改回Tomcat的问题。

答案 1 :(得分:1)

似乎问题是我使用的是org.keycloak.models.UserFederationManager实例,而不是org.keycloak.models.UserProvider实例。 UserFederationManager实现了UserProvider,并且在该keycloak使用

的注入机制下,更通用的类型似乎比特定的类型更好。
 // UserFederationManager ufm = afc.getSession().users();   // <-- PROBLEM
 // UserProvider ufm = afc.getSession().users();            // <-- WORKS

即使现在可以使用,您的两个建议也是有效的,因为我的构建版本确实与运行时版本不同,我将解决该问题以避免进一步的Bug。

谢谢您的投入!

相关问题