温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - Problem Implementing a custom Keycloak Authenticator SPI
java keycloak

java - 实施自定义Keycloak身份验证器SPI时出现问题

发布于 2020-03-27 10:25:25

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

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

遇到的问题是,身份验证流运行到自定义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()方法引发了Java NoSuchMethodExceptionError org.keaycloak.models.KeycloackSession像这样:

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

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

查看更多

查看更多

提问者
tony _008
被浏览
693
tony _008 2019-07-03 21:23

似乎问题是我使用的是org.keycloak.models.UserFederationManager实例,而不是org.keycloak.models.UserProvider实例。UserFederationManager实现了UserProvider,并且在该keycloak使用的注入机制下,更通用的类型似乎比特定的类型更好。

 // UserFederationManager ufm = afc.getSession().users();   // <-- PROBLEM
 // UserProvider ufm = afc.getSession().users();            // <-- WORKS

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

谢谢大家的投入!