簡體   English   中英

如何控制Mule Cache中的密鑰生成

[英]How to control key generation in Mule Cache

我的要求是將用戶(使用外部ws)作為我的Mule Flow的一部分進行身份驗證,並在緩存中使用身份驗證的結果。 但是,如果用戶憑據發生更改,則應自動使緩存失效,並且必須使用外部ws完成用戶身份驗證。 基本上,緩存鍵應基於用戶憑據。 這可能嗎 ?

這是我的騾子流,我看到Mule在第一次請求后緩存結果,無論有效負載是否在后續請求中發生變化(這是發送憑證的位置),mule總是返回緩存中的結果。 因此,當第一個請求具有不正確的憑據時,用戶身份驗證失敗並且mule緩存響應。 從此時起,無論在后續請求中發送正確的憑據,它始終引用緩存並返回用戶身份驗證失敗。 我如何實現我想要實現的目標?

這是我的騾子流:

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP-Inbound-Endpoint" host="0.0.0.0" port="8888" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" host="10.10.10.10" port="8080" doc:name="HTTP Request Configuration"/>
    <ee:object-store-caching-strategy name="Auth-Cache-Strategy" doc:name="Caching Strategy">
        <in-memory-store name="UserAuthCache" maxEntries="100" entryTTL="3600" expirationInterval="3600"/>
    </ee:object-store-caching-strategy>
    <flow name="cacheauthenticationFlow">
        <http:listener config-ref="HTTP-Inbound-Endpoint" path="*" doc:name="HTTP"/>
        <object-to-string-transformer doc:name="Object to String"/>
        <ee:cache cachingStrategy-ref="Auth-Cache-Strategy" doc:name="Cache">
            <logger message="Incoming: #[message.payload]" level="INFO" doc:name="Logger"/>
            <scripting:transformer doc:name="Python">
                <scripting:script engine="jython"><![CDATA[import base64
authorization = message.getInboundProperty("authorization")
#print "Authorization is: \"" + authorization + "\""
authstring = authorization.split()
#print authstring
credentials = authstring[-1]
#print "Credentials => " + credentials

decodedAuth = credentials.decode('base64')
#print decodedAuth
if (decodedAuth.find("@") > 0):
    (id, password) = decodedAuth.split(":")
    (username, project) = id.split("@")
    print username + ":" + password + ", Project: " + project
else:
    (username, password) = decodedAuth.split(":")
    print username + ":" + password

message.payload = { "username" : username + "@" + project , "password" : password }
result = message]]></scripting:script>
            </scripting:transformer>
            <json:object-to-json-transformer doc:name="Object to JSON"/>
            <logger message="Incoming payload: #[message.payload]" level="INFO" doc:name="Logger"/>
            <http:request config-ref="HTTP_Request_Configuration" path="/wservices/authenticate/user" method="POST" doc:name="HTTP-Dev-Box-Authentication"/>
            <object-to-string-transformer doc:name="Object to String"/>
        </ee:cache>
        <logger message="Response From Cache: #[message.payload]" level="INFO" doc:name="Logger"/>
        <set-payload value="#[message.payload.'status']" doc:name="Response"/>
    </flow>
</mule>

由於緩存作用域的默認密鑰生成策略基於消息有效內容,因此在您的特定情況下,只需移動scripting:transformer即可在ee:cache作用域之前執行。

對於更通用的解決方案,您不希望覆蓋請求有效負載,可以在ee:cache元素上定義keyGenerationExpressionkeyGenerator-ref屬性,以控制生成的緩存鍵的方式。

例如,要使用完整的HTTP授權標頭作為密鑰,您可以使用:

 <ee:cache cachingStrategy-ref="Auth-Cache-Strategy" doc:name="Cache"
      keyGenerationExpression="#[message.inboundProperties.authorization]">
     <!-- Your flow here --> 
 </ee:cache>

有關更多信息,請參閱緩存范圍文檔

如果你想使用這種方法,你可以做的是將jython代碼的一部分移到緩存范圍之外,更改它以便將用戶和密碼設置為消息上的單獨流變量,然后在keyGenerationExpression使用它們,然后再次使用它們在簡單的set-payload轉換器中的緩存范圍內。

您可以定義全局配置“Caching_Strategy”

<ee:object-store-caching-strategy name="Caching_Strategy" keyGenerationExpression="#[flowVars.userID]" doc:name="Caching Strategy"/>

並在緩存流程中引用全局配置:

<ee:cache doc:name="Cache" cachingStrategy-ref="Caching_Strategy">
<!-- flow -->
</ee:cache>

您可以通過流變量#[flowVars.userID]控制keyGenerationExpression

有關示例的完整詳細信息,請參閱http://www.tutorialsatoz.com/caching-in-mule-cache-scope/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM