简体   繁体   English

Keycloak Customization在身份验证流程中运行自定义java

[英]Keycloak Customization to run custom java in authentication flow

Please let me know if this is not the right place to post, but I have been looking all over for information regarding this and can't seem to find a concise answer. 如果这不是发布的正确位置,请告诉我,但我一直在寻找有关此问题的信息,似乎无法找到简明的答案。

I have been attempting to use keycloak to meet our application's user management requirements. 我一直在尝试使用keycloak来满足我们应用程序的用户管理要求。 While I have found keycloak to be very capable and quite effective, I have run into what may be a dead end for our usage. 虽然我发现keycloak非常有能力且非常有效,但我遇到了可能是我们使用的死胡同。

Background: 背景:

Traditionally, our application has used a very basic login framework that would verify the authentication. 传统上,我们的应用程序使用了一个非常基本的登录框架来验证身份验证。 Then using a third party application, that we cannot change , identify the roles that user would have via a wsdl operation and insert into our applications database. 然后使用我们无法更改的第三方应用程序,通过wsdl操作识别用户将拥有的角色并插入到我们的应用程序数据库中。

For example, if we verify the user John Doe exists and authenticate his credentials, we call the wsdl in our java code to get what roles that user should have (super user, guest, regular user). 例如,如果我们验证用户John Doe存在并验证其凭据,我们在java代码中调用wsdl以获取用户应具有的角色(超级用户,访客,普通用户)。 Obviously this entire framework is pretty flawed and at the end of the day, this is why weve chosen to use keycloak. 显然,整个框架都存在很大缺陷,最终,这就是我们选择使用keycloak的原因。

Problem 问题

Unfortunately, as I mentioned we cannot change the third party application, and we must get user role mappings from this wsdl operation. 不幸的是,正如我所提到的,我们无法更改第三方应用程序,我们必须从此wsdl操作获取用户角色映射。 I know there is a way to create/modify keycloak's users and roles via java functions. 我知道有一种方法可以通过java函数创建/修改keycloak的用户和角色。 However, in order to keep this architecture modular is there a way to configure the authentication flow to reach out to this WSDL on keycloaks side for role mapping ? 但是,为了保持这种体系结构的模块化,有一种方法可以配置身份验证流程以在密钥泄露端到达此WSDL以进行角色映射吗? (ie not in the application code but maybe in a scriplet in the authentication flow) (即不在应用程序代码中,但可能在认证流程中的scriplet中)

What I am looking for is essentially how to configure the authentication flow to run something as simple as "hello world" in java after the credentials are verified but before access is granted. 我正在寻找的本质上是如何配置身份验证流程以在验证凭据之后但在授予访问权限之前在java中运行像“hello world”这样简单的事情。

Not sure if the Authentication SPI could be used 不确定是否可以使用Authentication SPI

What you need is User Storage SPI . 您需要的是用户存储SPI Keycloak documentation provides a good walkthrough implementing a simple file-based user storage provider. Keycloak文档提供了一个实现简单的基于文件的用户存储提供程序的良好演练。 Here is the full source code of example project used in docs. 以下是docs中使用的示例项目的完整源代码。

User Storage SPI is very broad, so Keycloak offers 2 approaches to reuse features already present in Keycloak: 用户存储SPI非常广泛,因此Keycloak提供了两种方法来重用Keycloak中已有的功能:

  1. Have your users in external database and augment it with Keycloak features 让您的用户使用外部数据库并使用Keycloak功能对其进行扩充
  2. Store users in Keycloak and import relevant info from external database 将用户存储在Keycloak中并从外部数据库导入相关信息

There are documented pros and cons of each approach. 每种方法都有记录的优缺点。 Using one of above approaches you can implement a tiny provider that uses a remote service only for user-group management. 使用上述方法之一,您可以实现一个小型提供程序,该提供程序仅将远程服务用于用户组管理。

Yes. 是。 You can write custom authenticator using the Authentication SPI. 您可以使用Authentication SPI编写自定义验证器。 All the steps given for development of Keycloak Authentication SPI works fine except the deployment. 除了部署之外,开发Keycloak Authentication SPI的所有步骤都可以正常工作。 For deploying custom SPI, add your jar as module in standalone.xml. 要部署自定义SPI,请将jar作为模块添加到standalone.xml中。 To add the newly created SPI, follow below link: First, you need to register your SPI in Keycloak server. 要添加新创建的SPI,请按照以下链接:首先,您需要在Keycloak服务器中注册您的SPI。 Add another SPI in standalone.xml or domain.xml. 在standalone.xml或domain.xml中添加另一个SPI。

<spi name="authenticator_name"> <provider name="authenticator_name" enabled="true"/> </spi>

To register module for Keycloak server, add a module in Keycloak-server subsystem: 要为Keycloak服务器注册模块,请在Keycloak-server子系统中添加模块:

<subsystem xmlns="urn:jboss:domain:keycloak-server:1.1">
  <web-context>auth</web-context>
    <providers>
      <provider>classpath:${jboss.home.dir}/providers/*</provider>
      <provider>module:com.abc</provider>
        .....
        .....
</subsystem>

Once, this changes are made, you need to create a module at com/abc Steps: Build the code. 一旦进行了这些更改,您需要在com / abc中创建一个模块步骤:构建代码。

set KEYCLOAK_HOME=Keycloak Installation folder
%KEYCLOAK_HOME%/bin/jboss-cli.sh --command="module add --name={module_name}  --resources={path to your jar/your jar file name}  --dependencies=org.keycloak.keycloak-core,org.keycloak.keycloak-server-spi,org.keycloak.keycloak-server-spi-private,org.keycloak.keycloak-services,org.jboss.resteasy.resteasy-jaxrs,javax.ws.rs.api,org.keycloak.keycloak-common"

Once SPI module is added in Keycloak server, start Keycloak server and add the execution flow in the Keycloak Admin Console (Authentication). 在Keycloak服务器中添加SPI模块后,启动Keycloak服务器并在Keycloak管理控制台(身份验证)中添加执行流程。

For more details visit: http://www.keycloak.org/docs/3.0/server_development/topics/providers.html http://www.keycloak.org/docs/3.0/server_development/topics/auth-spi.html 有关详细信息,请访问: http//www.keycloak.org/docs/3.0/server_development/topics/providers.html http://www.keycloak.org/docs/3.0/server_development/topics/auth-spi.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM