【问题标题】:How to get the incoming cookies to a Mule HTTP end point如何将传入的 cookie 获取到 Mule HTTP 端点
【发布时间】:2018-03-13 12:16:42
【问题描述】:

我正在使用 Mule EE 3.5.2。 我正在向 Mule 中的传入 HTTP 端点发布一个带有 cookie 标头的 HTTP 请求(PostMan)。如何阅读此 Cookie? 在实践中,这个 cookie 将通过一个 NGinX 代理;我需要它传递给另一个应用程序。

【问题讨论】:

    标签: http cookies mule


    【解决方案1】:

    首先确保您的连接器有enableCookies="true"

    然后您会找到一个名为 cookies 的 inboundProperty,类型为 Lorg.apache.commons.httpclient.Cookie

    要访问它们,只需#[message.inboundProperties['cookies']

    【讨论】:

      【解决方案2】:

      以下是如何在没有自定义 java 类的情况下将来自 rest 响应的 cookie 保存在会话变量中。

      <set-session-variable variableName="incomingCookies" value="#[org.mule.transport.http.CookieHelper.parseCookiesAsAClient(message.inboundProperties['set-cookie'],null)]" doc:name="Set incomingCookies as Session Variable"/>
      <set-variable variableName="cookie-name" value="#[org.mule.transport.http.CookieHelper.getCookieValueFromCookies(incomingCookieMap,'cookie-name')]" doc:name=“Set cookie-name as Flow Variable”/>
      

      您可以使用类似的方法从 CookieHelper 类中的 parseCookiesAsAServer 方法从休息请求中提取 cookie。

      更多关于 CookieHelper 类的信息在这里https://www.mulesoft.org/docs/site/3.8.0/apidocs/org/mule/transport/http/CookieHelper.html

      【讨论】:

        【解决方案3】:

        这不再适用于新的 http:listener 组件。 设置该属性将给出:

            org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'enableCookies' is not allowed to appear in element 'http:listener'.
        

        那么如何使用新的 http:listener 组件来做到这一点... 我遇到的一个问题是我需要访问传入的 cookie,而 Mule 仅以未格式化的字符串提供 cookie。

        所以这是我的一个朋友开发的一个选项,我对其进行了一些改进,以便在流程中轻松访问 cookie:

        package transformers;
        
        import java.net.URI;
        import java.net.URISyntaxException;
        import java.util.Arrays;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;
        
        import org.mule.api.MuleMessage;
        import org.mule.api.transformer.TransformerException;
        import org.mule.api.transport.PropertyScope;
        import org.mule.transformer.AbstractMessageTransformer;
        import org.mule.transport.http.CookieHelper;
        import org.apache.commons.httpclient.Cookie;
        
        public class CookieGrabber extends AbstractMessageTransformer {
        
            public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
                Object              _CookieHeader   = message.getInboundProperty("Cookie");
                List<Cookie>        _CookieList     = null;
                Map<String,String>  _CookieMap      = new HashMap<String,String>();
        
                try {
                    //Grab the cookies from the header and put them into a List
                    _CookieList = (List<Cookie>) Arrays.asList(CookieHelper.parseCookiesAsAServer(_CookieHeader.toString(), 
                                                                                                   new URI("" + message.getInboundProperty("host"))));
                    //And put them in a convenient List which can be accessed from the flow
                    message.setProperty("incomingCookieList", _CookieList, PropertyScope.SESSION);
        
                    //Let's also put them in a nice Map, since incoming cookies will 
                    //usually only contain a name and a value, so let's get easy access to them by their name.
                    for (Cookie _Cookie : _CookieList){
                        _CookieMap.put(_Cookie.getName(), _Cookie.getValue());
                    }
                    message.setProperty("incomingCookieMap", _CookieMap, PropertyScope.SESSION);
        
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
                return message;
            }
        }
        

        然后是这个流程示例,它显示了如何使用此代码 sn-p。 它包含一个设置一些 cookie 的侦听器,将其转发到“代理”,该代理将读取 cookie,但还将请求转发到另一个端点,使其成为透明代理,但在此过程中会读取 cookie。

        <?xml version="1.0" encoding="UTF-8"?>
        <mule   xmlns:json="http://www.mulesoft.org/schema/mule/json" 
                xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 
                xmlns:http="http://www.mulesoft.org/schema/mule/http" 
                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/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
        http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
            <custom-transformer     class="transformers.CookieGrabber" 
                                    name="MyCookieTranformer" 
                                    doc:name="Java"/>
            <http:listener-config   name="HTTP_Configuration_CookieHandlerExample" 
                                    host="0.0.0.0" 
                                    port="8080" 
                                    doc:name="HTTP Listener Configuration"/>
            <http:request-config    name="HTTP_Request_Configuration" 
                                    host="localhost" 
                                    port="8080" 
                                    doc:name="HTTP Request Configuration"/>
            <flow name="CookieSetterFlow">
                <http:listener      config-ref="HTTP_Configuration_CookieHandlerExample" 
                                    path="/setCookies/*" 
                                    doc:name="setCookies" 
                                    doc:description="Call this module by entering http://localhost:8080/setCookie"/>
                <message-properties-transformer doc:name="Set the cookies" 
                                                doc:description="Set some random cookies in the header">
                    <add-message-property       key="Cookie" 
                                                value="token=abcde; name=dennis"/>
                </message-properties-transformer>
                <http:request       config-ref="HTTP_Request_Configuration" 
                                    path="/proxyCookie" 
                                    method="GET" 
                                    doc:name="call proxyCookies" 
                                    doc:description="Invoke the cookieReceiver with the cookies we've just set. Note the failure status code validator with a non-existing http code. It's a nasty bug, but it works like this...">
                    <http:failure-status-code-validator values="00000"/>
                </http:request>
            </flow>
            <flow name="CookieProxyFlow">
                <http:listener      config-ref="HTTP_Configuration_CookieHandlerExample" 
                                    path="/proxyCookie" 
                                    doc:name="proxyCookies" 
                                    doc:description="This connector will proxy the cookieReceiver"/>
                <transformer        ref="MyCookieTranformer" 
                                    doc:name="GrabCookies" 
                                    doc:description="Use our custom transformers.CookieGrabber class to put the cookies in a nice java.util.List in a session variable."/>
                <logger             message="CookieProxy: Value of cookie &quot;token&quot;: &quot;#[sessionVars.incomingCookieMap.get('token')]&quot;." 
                                    level="INFO" 
                                    doc:name="Have a cookie!" 
                                    doc:description="Let get a cookie value, simply by referring the name of it as the key from our map"/>
                <flow-ref           name="copy-and-clean-headers" 
                                    doc:name="copy-and-clean-headers" 
                                    doc:description="Cope the headers and clean the Mule stuff from the headers to forward it clean to the receiver."/>
                <set-property       propertyName="host" 
                                    value="localhost" 
                                    doc:name="Set Host" 
                                    doc:description="Now not really necessary, but you'll probably want to set the hostname to the actual service endpoint."/>
                <http:request       config-ref="HTTP_Request_Configuration" 
                                    path="/receiveCookie" 
                                    method="GET" 
                                    doc:name="forward to receiveCookies" 
                                    doc:description="Invoke the cookieReceiver.">
                    <http:failure-status-code-validator values="00000"/>
                </http:request>
                <flow-ref           name="copy-and-clean-headers" 
                                    doc:name="copy-and-clean-headers" 
                                    doc:description="Again copy the headers and clean the Mule http stuff."/>
            </flow>
            <sub-flow name="copy-and-clean-headers" >
                <copy-properties    propertyName="*" 
                                    doc:name="Copy All HTTP Headers"/>
                <remove-property    propertyName="Content-Length" 
                                    doc:name="Remove Content Length"/>
                <remove-property    propertyName="MULE_*" 
                                    doc:name="Remove MULE Properties"/>
                <remove-property    propertyName="X_MULE*" 
                                    doc:name="Remove X_MULE Properties"/>
                <remove-property    propertyName="http.*" 
                                    doc:name="Remove http Properties"/>
            </sub-flow>
            <flow name="CookieReceiverFlow">
                <http:listener      config-ref="HTTP_Configuration_CookieHandlerExample" 
                                    path="/receiveCookie" 
                                    doc:name="receiveCookies" 
                                    doc:description="This connector receives the cookies we've just set"/>
                <transformer        ref="MyCookieTranformer" 
                                    doc:name="GrabCookies" 
                                    doc:description="Use our custom transformers.CookieGrabber class to put the cookies in a nice java.util.List in a session variable."/>
                <logger             message="CookieReceiver: Value of cookie &quot;token&quot;: &quot;#[sessionVars.incomingCookieMap.get('token')]&quot;. Yep, still there :)" 
                                    level="INFO" 
                                    doc:name="Have a cookie!" 
                                    doc:description="Let get a cookie value, simply by referring the name of it as the key from our map"/>
                <set-payload        value="#[sessionVars.incomingCookieList.toArray(String)]" 
                                    doc:name="Put CookieList to payload" 
                                    doc:description="Put the session vairable List that contains the cookies in the payload"/>
                <json:object-to-json-transformer    returnClass="java.lang.String" 
                                                    doc:name="Object to JSON" 
                                                    doc:description="Convert our payload to a JSON object"/>
            </flow>
        </mule>
        

        你可以通过运行它并打开这个页面来测试它:http://localhost:8080/setCookies

        希望这会有所帮助。

        【讨论】:

          【解决方案4】:

          您可以通过以下方式获取 cookie:

          #[headers:INBOUND:cookie] or #[message.inboundProperties['cookie']]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-02-22
            • 1970-01-01
            • 2012-12-28
            • 2016-03-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多