首页 > 解决方案 > 如何在多租户应用程序中手动配置 ThreadContext?

问题描述

我正在 SAP BTP 上开发一个多租户应用程序,我需要处理从非 SAP 系统接收到的 webhook 事件。更具体地说,在收到事件后,我需要访问租户子帐户中的目的地,并从该目的地后面配置的系统中查询一些数据。

我在其上接收 webhook 事件的端点是通过使用我自己的用户数据库的基本身份验证来保护的。所以这意味着传入的 webhook 请求永远不会通过 sdk RequestFilter,这通常会提取 JWT 令牌(在我的情况下无论如何都不存在)并设置ThreadContext.

我尝试了以下代码(webhook 有效负载包含正确的租户 ID):

 @Override
public void handleWebhook(WebhookEvent webhookEvent) {
    TenantAccessor.executeWithTenant(tenantOfString(webhookEvent.getTenantId()), () -> {
        Destination destination = DestinationAccessor.getDestination("MyDestination");

        //Do some stuff with the destination
    });
}

private Tenant tenantOfString(String tenantId) {
    return new Tenant() {
        @Nonnull
        @Override
        public String getTenantId() {
            return tenantId;
        }
    };
}

不幸的是,此代码导致以下错误:

com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException: com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationNotFoundException: No destination for name 'MyDestination' could be found in any of the registered loaders. When your app runs on SAP Cloud Platform, make sure you have one of the following modules in your dependency tree: com.sap.cloud.sdk.cloudplatform:scp-neo or com.sap.cloud.sdk.cloudplatform:scp-cf, depending on your SCP landscape.

有什么办法可以解决这类问题?在我看来,我必须以某种方式手动配置适当ThreadContext的才能执行此类操作。

编辑:目的地存在于租户的子帐户中,我还使用 oAuth 和从租户的身份验证端点检索到的 JWT 令牌设置了一个测试端点,我可以使用它从我的提供者帐户成功访问租户的目的地。所以我假设整个 saas 配置过程正常工作。

标签: sap-cloud-sdk

解决方案


推荐阅读