【问题标题】:Keycloak multi-tenancy and bearer-only microserviceKeycloak 多租户和仅承载微服务
【发布时间】:2018-09-27 20:53:46
【问题描述】:

我有一些 Spring Boot 微服务,前面有 Spring Cloud API 网关 (Zuul)。
API 网关对用户进行身份验证并转发授权承载令牌标头。
在单个领域工作正常。

现在,我想使用多个领域。

使用KeycloakConfigResolver,我可以使用 API 网关对用户进行身份验证(基于路径的 keycloak 部署)。

但是我应该如何为微服务(仅承载)配置 KeycloakConfigResolver,以便使用正确的 KeycloakDeployment?由于两个领域都可以访问每个服务,我不知道如何检测哪个领域用户已通过身份验证?

我不能使用KeycloakConfigResolver 的路径,例如用于订单服务,用户可以在/orders 上执行 GET,由 realm1 或 realm2 进行身份验证...

使用标题似乎也不是一个好的解决方案...... 任何想法?

我希望我足够清楚......

【问题讨论】:

  • As every service can be accessed by both realms I don't know how to detect against which realm user was authenticated。领域实体在 keycloak 中应该是不透明的。这对你来说是不是有点像黑客攻击?想不出在这里使用多个领域会带来什么好处..

标签: spring-boot spring-cloud multi-tenant keycloak


【解决方案1】:

您可以从KeycloakPrincipal(登录用户)中找到realm,然后相应地构建KeycloakDeployment,您可以找到示例here

InputStream is = getClass().getResourceAsStream("/realm1-keycloak.json");
KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(is);

【讨论】:

  • 但是由于用户只登录了 API Gateway 客户端,还没有登录 API Gateway 后面的服务,SecurityContextHolder.getContext().getAuthentication() 为空。该服务唯一可用的信息是来自标头的不记名令牌....
  • 查看这里了解如何获取 KeycloakPrincipal stackoverflow.com/questions/31864062/…
  • API gateway 中需要有 KeycloakConfigResolver,这样你才能将请求转发到正确的领域
猜你喜欢
  • 1970-01-01
  • 2022-12-29
  • 2017-01-14
  • 2018-04-07
  • 1970-01-01
  • 2021-11-26
  • 2021-12-11
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多