Mule EE 3.5.2를 사용하고 있습니다. 쿠키 헤더가있는 HTTP 요청 (Postman)을 Mule의 수신 HTTP 끝점에 게시하고 있습니다. 이 쿠키는 어떻게 읽을 수 있습니까? 실제로이 쿠키는 NGinX 프록시를 통해 전달됩니다. 다른 응용 프로그램으로 전달해야합니다.들어오는 쿠키를 Mule HTTP 엔드 포인트로 가져 오는 방법
1
A
답변
2
먼저 커넥터에 enableCookies="true"
이 있는지 확인하십시오.
cookies
Lorg.apache.commons.httpclient.Cookie
인 inboundProperty가 있습니다.
액세스하려면 #[message.inboundProperties['cookies']
.
1
새로운 http : listener 구성 요소에서는 더 이상 작동하지 않습니다. 속성을 줄 것이다 설정 :
org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'enableCookies' is not allowed to appear in element 'http:listener'.
그래서 새로운 http로이 작업을 수행하는 방법 : 수신기 구성 요소 ... 은 뮬 만 제공 내가 가진 문제는 내가 와서 쿠키를 액세스하는 데 필요한 것이되었다 형식이 지정되지 않은 문자열의 쿠키.
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;
}
}
다음이를 사용하는 방법을 보여줍니다이 흐름 예제가있다 :
그래서이 내가 흐름 쿠키에 쉽게 액세스를 얻기 위해 약간 강화 된 내 친구가 개발 한 옵션입니다 코드 스 니펫. 일부 쿠키를 설정하고이를 쿠키를 읽을 "프록시"로 전달하는 청취자를 포함하지만 다른 끝점으로 요청을 전달하여 투명 프록시로 만들고 프로세스에서 쿠키를 읽는 수신기를 포함합니다. 이 도움이 http://localhost:8080/setCookies
희망 :
<?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 "token": "#[sessionVars.incomingCookieMap.get('token')]"."
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 "token": "#[sessionVars.incomingCookieMap.get('token')]". 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>
당신은 그것을 실행하고이 페이지를 열어 테스트 할 수 있습니다.
2
다음은 맞춤 자바 클래스가없는 세션 변수에서 응답의 쿠키를 저장하는 방법입니다.
<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 방법을 사용하여 나머지 요청에서 쿠키를 추출하는 유사한 방법을 사용할 수 있습니다.
CookieHelper 클래스에 대한 자세한 내용은 여기 https://www.mulesoft.org/docs/site/3.8.0/apidocs/org/mule/transport/http/CookieHelper.html
관련 문제
- 1. HTTP 엔드 포인트로 뮬 파일 다운로드
- 2. Mule 워크 플로와 파일 엔드 포인트로 $ {app.home}에 작성
- 3. Mule ESB HTTP 인바운드 엔드 포인트 경로
- 4. mule 3, 아웃 바운드 엔드 포인트에서 다른 엔드 포인트로 응답을 보내려면 어떻게해야합니까?
- 5. URL을 공통 엔드 포인트로 다시 작성
- 6. Mule TcpConnector에서 Socket 객체를 가져 오는 방법
- 7. 통합 테스트에서 엔드 포인트로 파일을 업로드하는 방법
- 8. Mule - JMS 입력 엔드 포인트를 중지하는 방법
- 9. 내부 엔드 포인트로 스크립트 시작
- 10. NServiceBus가 SOAP 엔드 포인트로 라우팅됩니다.
- 11. HttpClient를 사용하여 쿠키를 가져 오는 방법
- 12. Mule HTTP 아웃 바운드 엔드 포인트에 쿼리 매개 변수 추가
- 13. AJAX html 요청에서 쿠키를 가져 오는 방법
- 14. PHP 컬에서 변수로 쿠키를 가져 오는 방법
- 15. mule https 인바운드 엔드 포인트
- 16. VM 인바운드 엔드 포인트로 통화 흐름을 발생 시키십시오.
- 17. Mule 3.2의 발신 HTTP 요청에서 쿠키를 비활성화 할 수 없습니다.
- 18. 여러 개의 RESTful 엔드 포인트로 라우팅하는 Nginx
- 19. Mule ESB - 서블릿 : 인바운드 엔드 포인트 오류
- 20. 원격 REST 엔드 포인트로 코어 데이터 전송
- 21. Mule Syslog 인바운드 엔드 포인트
- 22. Mule 3.4.0 http 인바운드 엔드 포인트에서 HTTPRequest 오브젝트 받기
- 23. Mule Enrichment : http 엔드 포인트 응답으로 XML 페이로드를 풍부하게하십시오
- 24. Mule 아웃 바운드 http 엔드 포인트 내용 길이 불일치
- 25. Mule HTTP 아웃 바운드 엔드 포인트의 응답을 무시하고 처리를 계속합니다.
- 26. 들어오는 HttpRequest에서 URL의 도메인 부분을 가져 오는 방법
- 27. Mule 일반 엔드 포인트에서 pollingFrequency 속성을 설정하는 방법
- 28. Groovy의 Mule HTTP 리스너 쿼리?
- 29. 익스프레스 엔드 포인트로 하위 프로세스 스폰하기
- 30. 들어오는 요청의 HTTP 버전 가져 오기