2015-08-18 27 views
7

我有一個需要租戶作爲標頭的API。如何使用WSO2 API管理器在中介中將租戶設置爲標頭

如果我創建序列的自定義:

<sequence name="WSO2AM--Ext--In"> 
    <header 
     name="X-Tenant-Id" 
     scope="transport" 
     action="set" 
     expression="???????????????????" 
    /> 
</sequence> 

有沒有我可以用它來實現這一目標的體現在哪裏?或者我應該採用創建一個API調解器來設置它?

PS: 看着WSO2的源代碼(CarbonTenantInfoConfigurator.java),我發現這個片段可能是有用的提示:

PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext(); 
String tenantDomain = cc.getTenantDomain(); 
int tenantId = cc.getTenantId(); 
messageContext.setProperty("tenant.info.domain", tenantDomain); 
messageContext.setProperty("tenant.info.id", tenantId); 

但我不知道如何訪問這些屬性自定義序列,如果可能的話。

回答

3

檢查調試後從ApiManager輸出,我注意到自定義序列正在處理程序後執行。幸運的是,OAuthAuthenticator類(由APIAuthenticationHandler使用)設置了一些便利的屬性,如END_USER_NAMEAPPLICATION_NAME

END_USER_NAME包含調用者的姓名和租戶([email protected])。

該自定義序列爲我工作:

<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> 

我不能比源代碼的其他財產找到文檔和this other question

+0

這就是我一直在尋找的東西,它工作的很棒。謝謝! –

0

正如代碼所示,這些設置爲突觸MessageContext。您可以使用以下表達式檢索這些屬性。

獲取屬性( 'tenant.info.domain')

獲得屬性( 'tenant.info.id')

感謝

Tishan

+0

我試過了。還嘗試過'get-property(tenant.info.domain)','$ ctx:tenant.info.domain'和'$ axis2:tenant.info.domain',但沒有成功。 –

+0

我用ESB 4.8.0試過,它按預期工作。以下是用於設置HTTP標頭的突觸配置段。

Tishan

相關問題