简体   繁体   English

使用 keycloak 服务器进行身份验证后,将自定义字段添加到网关上的当前用户

[英]add custom fields to current user on gateway after authenticating with keycloak server

We're using Keycloak server for authenticating against several IDPS (google, active directory, etc).我们使用 Keycloak 服务器对多个 IDPS(谷歌、活动目录等)进行身份验证。 We have a spring gateway microservice which plays role of a client and several other microservices which play role of resource servers.我们有一个 spring gateway 微服务,它扮演客户端的角色,还有几个其他的微服务扮演资源服务器的角色。

When user authenticates via keycloak, we want to associate the authenticated user with some custom fields (like context, roles, user details) from our custom database (NOT Keycloak DB) and send those fields to other microservices as well, so that we do not need to load the fields from DB in every microservice.当用户通过 keycloak 进行身份验证时,我们希望将经过身份验证的用户与我们自定义数据库(不是 Keycloak DB)中的一些自定义字段(如上下文、角色、用户详细信息)相关联,并将这些字段也发送到其他微服务,这样我们就不会需要在每个微服务中从数据库加载字段。

How would you do that?你会怎么做? Making a GlobalFilter in the Gateway which would add those fields to request headers and setting those headers somehow to the principal object in resource servers?在网关中创建一个 GlobalFilter,它将这些字段添加到请求标头并将这些标头以某种方式设置到资源服务器中的主体对象? Or using cache (redis) to store the fields on gateway and load them in resource servers?或者使用缓存(redis)将字段存储在网关上并将它们加载到资源服务器中? Or do you have some other solution?或者你有其他解决方案吗? For example extending access token, overiding UserDetailsService, etc..例如扩展访问令牌、覆盖 UserDetailsS​​ervice 等。

What's important to note is, that we don't want to extends Keycloak Database, since we want to have the whole role management in our custom database.需要注意的是,我们不想扩展 Keycloak 数据库,因为我们希望在我们的自定义数据库中进行整个角色管理。 Reason for that is that keycloak schema is not very flexible.原因是 keycloak 模式不是很灵活。 We want to use keycloak only as a dummy authentication server.我们只想将 keycloak 用作虚拟身份验证服务器。

The preferred option for security related values is for Keycloak to reach out to your APIs or custom data sources at the time of token issuance, then include your domain specific claims in JWT access tokens.安全相关值的首选选项是让 Keycloak 在颁发令牌时联系您的 API 或自定义数据源,然后将您的域特定声明包含在 JWT 访问令牌中。 In keycloak I believe this is done via a protocol mapper, as in this answer .在 keycloak 中,我相信这是通过协议映射器完成的,就像在这个答案中一样。

This design pattern is discussed in the Claims Best Practices article.声明最佳实践文章中讨论了此设计模式。 It is recommended to not send secure values such as roles in custom headers etc, since they are potentially easier to change by a hostile party.建议不要发送安全值,例如自定义标头中的角色等,因为它们可能更容易被敌对方更改。 Instead each API should receive the JWT and validate it, in a zero trust manner, then use the received claims for authorization.相反,每个 API 都应该以零信任的方式接收 JWT 并对其进行验证,然后使用收到的声明进行授权。

For non secure values, such as a session_id or correlation_id used for logging, simple HTTP headers work well.对于非安全值,例如用于日志记录的session_idcorrelation_id ID,简单的 HTTP 标头效果很好。

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

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