使用MEL表达式,mule中的基本身份验证无效。 如何使用户名和密码动态化?
答案 0 :(得分:0)
我可以看到在这种类似情况下打开一个错误,其中流变量在HTTP配置中动态使用: - https://www.mulesoft.org/jira/browse/MULE-8895
需要检查您正在使用的Mule版本......它应该可以在 Mule版本3.7 +
中使用或者您可以尝试以下示例: -
<http:listener-config name="HTTP_Listener_Configuration2" host="0.0.0.0" port="8089" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="#[flowVars['urlHost']]" port="#[flowVars['urlHostPort']]" doc:name="HTTP Request Configuration"/>
<flow name="muleclientFlow">
<http:listener config-ref="HTTP_Listener_Configuration2" path="/test" doc:name="HTTP"/>
<set-payload value="gdfgdfgdgsd" doc:name="Set Payload"/>
<set-variable variableName="urlHost" value="localhost" doc:name="Variable"/>
<set-variable variableName="urlHostPort" value="8081" doc:name="Variable"/>
<set-variable variableName="urlHostUser" value="anon" doc:name="Variable"/>
<set-variable variableName="urlHostPassword" value="anon" doc:name="Variable"/>
<set-variable variableName="basic_credentials" value="#[flowVars.urlHostUser+':'+flowVars.urlHostPassword]" doc:name="Variable"/>
<http:request config-ref="HTTP_Request_Configuration" path="/" method="GET" doc:name="HTTP">
<http:request-builder>
<http:header headerName="Authorization" value="#['Basic ' + org.apache.commons.codec.binary.Base64.encodeBase64String(flowVars.basic_credentials.getBytes())]"/>
</http:request-builder>
</http:request>
<logger message="Success !!" level="INFO" doc:name="Logger"/>
</flow>
在这里你可以看到我使用了另一个流变量basic_credentials,它结合了变量#[flowVars.urlHostUser+':'+flowVars.urlHostPassword]
: -
<set-variable variableName="basic_credentials" value="#[flowVars.urlHostUser+':'+flowVars.urlHostPassword]" doc:name="Variable"/>
最后在HTTP请求配置中传递它: -
<http:request-builder>
<http:header headerName="Authorization" value="#['Basic ' + org.apache.commons.codec.binary.Base64.encodeBase64String(flowVars.basic_credentials.getBytes())]"/>
</http:request-builder>
参考: - https://forums.mulesoft.com/questions/34854/dynamically-set-username-and-password-http-request.html