在wso2 Enterprise Intigrator中从认知中获取令牌并进行缓存

问题描述 投票:0回答:1

我需要从Cognito获得令牌,但是我不知道如何通过企业集成商获得令牌。我也有client_idclient_secet以及端点。但是我无法获得令牌,似乎我没有使用适当的参数正确调用端点。

获得令牌后,我需要将其缓存直到令牌过期,因此我不必一遍又一遍地打电话

这是我正在尝试的代码

<cache id="cache-sample" scope="per-host" collector="false" hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator" timeout="5000">
    <implementation type="memory" maxSize="1000"/>
</cache>
<header name="client_id" scope="transport" value="XXXXXXXXXXXXXXXXX"/>
<header name="client_secret" scope="transport" value="XXXXXXXXXXXXXXXXXXX"/>
<header name="Content-Type" scope="transport" value="application/json"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<call>
    <endpoint>
       <address uri="https://xcxxxxxxxxxx.auth.us-east-1.amazoncognito.com/oauth2/token"/>
    </endpoint>
</call>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<cache scope="per-host" collector="true"/>
<respond/>
caching wso2 wso2esb amazon-cognito wso2ei
1个回答
0
投票
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api/myService" name="myService-api" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST" uri-template="/getToken">
        <inSequence>
            <script language="js"><![CDATA[var payload = mc.getPayloadXML();
                var log = mc.getServiceLog();
                var client_id = payload..*::client_id.toString();
                var client_secret = payload..*::client_secret.toString();
                mc.setProperty("client_id", client_id);
                mc.setProperty("client_secret", client_secret);]]>
            </script>
            <payloadFactory media-type="json">
                <format/>
                <args/>
            </payloadFactory>
            <property name="ContentType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"/>
            <property expression="fn:concat($ctx:client_id,':',$ctx:client_secret)" name="credentials" scope="default" type="STRING"/>
            <property expression="fn:concat('Basic ', base64Encode($ctx:credentials))" name="Authorization" scope="transport" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
            <property name="FORCE_POST_PUT_NOBODY" scope="axis2" type="BOOLEAN" value="true"/>
            <property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
            <cache 
                collector="false" 
                hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator" 
                id="cognito-token" 
                scope="per-host" 
                timeout="3600">
                <onCacheHit>
                    <log level="custom">
                        <property expression="/" name="[usage-service-api]:: CHACHE HIT"/>
                    </log>
                    <respond/>
                </onCacheHit>
                <implementation maxSize="50" type="memory"/>
            </cache>
            <call>
                <endpoint>
                    <http method="post" uri-template="https://xxxxxxxxxxx.amazoncognito.com/oauth2/token?grant_type=client_credentials"/>
                </endpoint>
            </call>
            <property name="RESPONSE" scope="default" type="STRING" value="true"/>
            <cache collector="true" id="cognito-token" scope="per-host"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

对于第一个呼叫缓存,在这种情况下会发生未命中,它将呼叫端点并将其存储在缓存中对于第二个呼叫,直到该呼叫的时间到期为止,它将命中缓存,即Cache hit然后它将响应onCacheHit调解人

<onCacheHit>
    <respond/>
</onCacheHit>

但是在缓存命中后,它将跳过在cache介体之后编写的任何语句的任何执行。因此,[[因此,如果您按某种顺序编写,请按其他顺序进行]API中可以有多个resources

如果您想知道如何调用此API,请参阅此getting cognito acces_token with wso2 esb

<log level="custom"> <property expression="/" name="[usage-service-api]:: CHACHE HIT"/> </log>

上面的代码块将缓存的内容记录在CACHE HIT
© www.soinside.com 2019 - 2024. All rights reserved.