问题描述
我有一个需要租户作为标头的 API.
I have an API that requires the tenant as a header.
如果我创建自定义顺序:
If I create a custom in-sequence:
<sequence name="WSO2AM--Ext--In">
<header
name="X-Tenant-Id"
scope="transport"
action="set"
expression="???????????????????"
/>
</sequence>
有没有我可以使用的表达方式来实现这一点?还是我应该创建一个每个 API 的中介来设置它?
Is there an expression that I can use to achieve this? Or should I resort to creating a per-API mediator to set it?
附注:查看 WSO2 源代码(CarbonTenantInfoConfigurator.java),我发现这个片段可以用作提示:
PS:Looking at WSO2 source code (CarbonTenantInfoConfigurator.java), I found this fragment that could be useful as a hint:
PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String tenantDomain = cc.getTenantDomain();
int tenantId = cc.getTenantId();
messageContext.setProperty("tenant.info.domain", tenantDomain);
messageContext.setProperty("tenant.info.id", tenantId);
但如果可能的话,我不知道如何访问自定义序列中的这些属性.
But I don't know how to access to those properties in the custom sequence, if possible.
推荐答案
在检查 ApiManager 的调试输出后,我注意到自定义序列在处理程序之后立即执行.幸运的是,OAuthAuthenticator 类(由 APIAuthenticationHandler 使用)设置了一些方便的属性,例如 END_USER_NAME
和 APPLICATION_NAME
.
After checking the debug output from ApiManager, I noticed that custom sequences are being executed right after handlers. Luckily, the OAuthAuthenticator class (used by APIAuthenticationHandler) sets some handy properties like END_USER_NAME
and APPLICATION_NAME
.
END_USER_NAME
包含调用者的姓名和租户 ([email protected]).
END_USER_NAME
contains the name and tenant of the caller ([email protected]).
这个自定义序列对我有用:
This custom sequence worked for me:
<sequence name="add_service_header" trace="enable" statistics="enable" xmlns="http://ws.apache.org/ns/synapse">
<log/>
<property name="tenant" expression="fn:substring-after(get-property('END_USER_NAME'), '@')" />
<header name="X-Tenant" scope="transport" expression="get-property('tenant')"/>
<header name="X-AppName" scope="transport" expression="get-property('APPLICATION_NAME')"/>
</sequence>
除了源代码和 另一个问题
这篇关于如何使用 WSO2 API Manager 将租户设置为中介中的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!