1

所以我关注了 https://blogs.sap.com/2017/12/20/deep-dive-6-with-sap-s4hana-cloud-sdk-extend-your-cloud-foundry-application-with-tenant-aware -持久性/

我有一个2.0.1.RELEASE使用 xs2 库 OAuth 2.0 安全性在 SCP 中运行的 Spring Boot 应用程序。如果我在 Postman 中创建访问令牌并直接调用我的 @RestController(到达数据库),我会从 TenantAccessor.getCurrentTenant() 收到此错误:

com.sap.cloud.sdk.cloudplatform.tenant.exception.TenantNotAvailableException: Failed to get current tenant: no request available. This error may occur when trying to obtain the current tenant within tasks that are not triggered by a request, for example, while using a RequestContextExecutor. Note that, on SCP CF, a request is required to obtain tenant information from the JWT bearer in the "Authorization" header.

我不明白为什么当我显然将带有包含不记名令牌的 Authorization 标头的请求直接发送到我的@RestController.

我有一个想法通过用 注释TenantIdentifierResolver类(包含 TenantAccessor)来解决这个问题@RequestScope,但这失败了,因为在初始化应用程序时找不到请求上下文(entityManagerFactory)。

4

1 回答 1

1

此消息表示缺少RequestContext. 这样的 aRequestContext使用 aRequestContextServletFilter或 a初始化RequestContextExecutor

您的 Spring 应用程序可能缺少 Web Servlet 过滤器。您可以尝试将以下@ServletComponentScan注释添加到您的应用程序吗?

@ServletComponentScan({"com.sap.cloud.sdk"})
  ...
@SpringBootApplication
public class Application extends SpringBootServletInitializer
{ 
    ...
}

在后台任务中,您可能必须使用RequestContextExecutor. 有关更多详细信息,请在此处查看相关问题。

于 2018-05-14T13:19:11.207 回答