Spring Boot 3 Keycloak 多租户配置

问题描述 投票:0回答:1

我有一个 Spring Boot 3 微服务和一个具有多个领域的独立密钥斗篷。用户可以调用端点并在那里传递“授权”标头,其中包含 Bearer jwt 令牌,它指定现有领域之一。

当 spring boot 微服务访问 required realm 来验证当前请求时,如何实现这种行为,根据请求动态确定 realm?

请注意,我使用的是 Spring boot v3.0.6。 以前基于主题Spring Boot Keycloak Multi Tenant Configuration但现在我们不能使用以前的方法,如那里所说Use Keycloak Spring Adapter with Spring Boot 3

我只有一个想法,即为每个领域创建几个 oauth2 提供者,并以某种方式在运行时实现对目标提供者的选择。 非常感谢您!

java spring-boot spring-security keycloak spring-boot-3
1个回答
0
投票

你可以按照我的教程,都是多租户的。全部配置为“静态”多租户,但是一个为“动态”租户演示conf。

有些只使用 Spring Boot“官方”启动器,有些则使用薄包装器。使用最新版本,您可以配置一个资源服务器以接受由几乎 0 Java conf 发布的任意多个领域的 JWT:

<properties>
    <com.c4-soft.springaddons.version>6.1.9</com.c4-soft.springaddons.version>
</properties>
<dependencies>
    <dependency>
        <groupId>com.c4-soft.springaddons</groupId>
        <artifactId>spring-addons-webmvc-jwt-resource-server</artifactId>
        <version>${com.c4-soft.springaddons.version}</version>
    </dependency>
</dependencies>
@Configuration
@EnableMethodSecurity
public static class WebSecurityConfig {
}
scheme: http
origins: ${scheme}://localhost:4200,${scheme}://localhost:8080,${scheme}://localhost:8100
auth-server: https://localhost:8443

com:
  c4-soft:
    springaddons:
      security:
        cors:
        - path: /solutions/**
          allowed-origins: ${origins}
        issuers:
        - location: ${auth-server}/realms/realm1
          username-claim: $.preferred_username
          authorities:
          - path: $.realm_access.roles
          - path: $.resource_access.*.roles
        - location: ${auth-server}/realms/realm2
          username-claim: $.preferred_username
          authorities:
          - path: $.realm_access.roles
          - path: $.resource_access.*.roles
        - location: ${auth-server}/realms/realm3
          username-claim: $.preferred_username
          authorities:
          - path: $.realm_access.roles
          - path: $.resource_access.*.roles
        permit-all:
        - /actuator/health/readiness
        - /actuator/health/liveness
        - /v3/api-docs/**

server:
  ssl:
    enabled: false

---
scheme: https

server:
  ssl:
    enabled: true

spring:
  config:
    activate:
      on-profile: ssl

如果领域是在运行时生成的(在资源服务器启动后),但您不想使用“我的”启动器,那么您将不得不参考Spring Security参考文档.

© www.soinside.com 2019 - 2024. All rights reserved.