使用Keycloak和SpringBoot的多租户

时间:2017-10-26 03:02:34

标签: spring-boot multi-tenant keycloak spring-boot-gradle-plugin keycloak-services

我使用spring boot adapter和keycloak spring boot adapter来执行多租户,我可以使用ip和域名访问页面。 但是当我运行gradlew脚本时,似乎没有检测到这个bean没有发生任何变化。即使我包括一个记录器,日志也不会打印,所以我假设根本没有读取该文件。我错过了什么吗?或者是否有任何我可以用于多租户的实现。感谢。

KeycloakTomcatContextCustomizer

@Component
public class KeycloakTomcatContextCustomizer implements TomcatContextCustomizer
{
    private static final Logger logger = LoggerFactory.getLogger(KeycloakTomcatContextCustomizerBean.class);

    @Override
    public void customize(Context context)
    {
        LoginConfig loginConfig = new LoginConfig();
        loginConfig.setAuthMethod("--KEYCLOAK--");
        context.setLoginConfig(loginConfig);
        context.addSecurityRole("myproject");

        SecurityConstraint constraint = new SecurityConstraint();
        constraint.addAuthRole("myproject");

        SecurityCollection collection = new SecurityCollection();
        collection.addPattern("/contexts");
        constraint.addCollection(collection);

        context.addConstraint(constraint);

        context.addParameter("keycloak.config.resolver", HostBasedKeycloakResolver.class.getName());
    }

    public class HostBasedKeycloakResolver extends KeycloakSpringBootConfigResolver
    {
        private KeycloakDeployment keycloakDeployment;

        @Autowired
        private AdapterConfig adapterConfig;

        @Override
        public KeycloakDeployment resolve(OIDCHttpFacade.Request request) {
        if (keycloakDeployment != null) {
            return keycloakDeployment;
        }

            //get the host part here

        //build keycloakdeployment
            keycloakDeployment = KeycloakDeploymentBuilder.build(adapterConfig);
            adapterConfig.setAuthServerUrl("https://"+host+"/auth");
            System.out.println(adapterConfig.getAuthServerUrl());
            return keycloakDeployment;
        }
    }

}

我的application.yml

keycloak:
  cors: true
  realm: Boot-Project
  realmKey: AARjANBgkqhkiYUitdhjnCAQ8AMIIBCgKCAQEArOS/TTjkgjdoiQ7F6m5x206lJ+K9VBpEjkjrignxIdH7pJDWv9UMg2CL1q3Tfkjg/YdjkljgkbsnqrSzjBcIU5HQ2AQLkRm2eCPuLIB23d2VS3hZGqvbyqN42hbk/oRhloS0tS2/frq4fIeU53KQiRPPiBt1IEO7DINoDUXdyOWS7g/rSrMkjjUm9SohXdv8u3aB+mnI8gNwEag17Cj+wqoc1smPj5jb/8Ab3MynQHv4ekgXYFPI5BEQSXXflBLbL2kjqR2xP8y8XTsOz58XLyWBydjN2R37uds9D2TqipU3tdc286b276RhNCwIDAQAB
  auth-server-url: https://${__AUTH_VM__:localhost}/auth
  ssl-required: none
  resource: myproject
  bearer-only: true
  public-client: false
  credentials:
    secret: ls5f7c3g-d045-444f-8234-6cth6970726y
  securityConstraints[0]:
    securityCollections[0]:
      name: secured context api and app
      authRoles: 
        - commongui
      patterns:
        - /contexts/*
        - /config.json

1 个答案:

答案 0 :(得分:3)

我遇到了与你想要达到的类似的情况,你的代码似乎没问题。以下内容可能对您有所帮助:

1)keycloak关于如何实施多租户的官方文件: http://www.keycloak.org/docs/3.2/securing_apps/topics/oidc/java/multi-tenancy.html

从那里可以看出,您需要配置要使用的KeycloackConfigResolver,但不幸的是,在撰写本文时,没有方便的方法在Spring Boot中执行此操作,就像我在此票证中找到的那样:

2)Spring Boot适配器的自定义KeycloakConfigResolver: https://issues.jboss.org/browse/KEYCLOAK-4139?_sscc=t

3)在#1和#2之后,可能会发生的事情是Spring可能会覆盖您的实现类,具体取决于Spring配置的解析方式。尝试从您正在扩展的类中覆盖更多方法,添加一些日志记录,然后在运行时检查Spring日志(如果这是正在发生的事情)(您将看到类似这样的内容" .. 。覆盖bean的bean定义...使用不同的定义:用...替换...")

4)尝试排除相关类,以便不会覆盖您的实现类例如: @SpringBootApplication(exclude = {KeycloakSpringBootConfiguration.class})

希望这有助于或提供一些想法。

此致 先生。 JZ