这是服务标注政策:
<ServiceCallout name="GeoCodeClient">
<Request clearPayload="false" variable="GeocodingRequest" />
<Response>GeocodingResponse</Response>
<Timeout>30000</Timeout>
<HTTPTargetConnection>
<URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
</HTTPTargetConnection>
</ServiceCallout>
让我们说我必须访问受用户名/密码保护的资源。如何将此基本授权添加到此策略以使我能够执行此操作?
答案 0 :(得分:2)
在我们的项目中,KeyValueMaps用于在组织级别存储基本身份验证信息。使用KeyValueMap policy检索授权信息,并将其作为基本auth标头添加到请求消息中。
看看这种方法是否适合你。
答案 1 :(得分:2)
要为服务标注添加基本身份验证标头,您可以使用“AssignMessage”策略在“GeocodingRequest”中设置“授权”标头,如下所示:
<AssignMessage enabled="true" continueOnError="true" async="false" name="AssignAuthorizationHeaderPolicy">
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="true" transport="http" type="request">GeocodingRequest</AssignTo>
<Add>
<Headers>
<Header name="Authorization">Basic YourAuthenticationHeader</Header>
</Headers>
</Add>
</AssignMessage>
创建此策略后,您需要将其作为流程附加到proxy.xml中serviceCallout之前的请求流中:
<Step>
<FaultRules/>
<Name>AssignAuthorizationHeaderPolicy</Name>
</Step>
<Step>
<FaultRules/>
<Name>GeoCodeClient</Name>
</Step>
答案 2 :(得分:0)
添加到已经说过的内容,如果你需要base64编码(如果你正在使用基本授权,你可能会这样做),你需要做脚本标注。例如,您可以使用以下Python:
import base64
if (client_secret is not None):
data = client_id + ":" + client_secret
header_value = base64.b64encode(data)
header_value = "Basic " + header_value
flow.setVariable("request.header.Authorization", header_value)
由于你需要包含适当的库,所以JS会有点棘手,但我确信还有很多例子可供使用。
答案 3 :(得分:0)
使用键值映射以安全的方式存储敏感数据
Step 1)Use below API to Create/Update the key Value maphttps://api.enterprise.apigee.com/v1/o/{orgname}/environments/{env}/keyvaluemaps Body:-{
"entry" : [ {
"name" : "basic_auth_system1",
"value" : "Basic XXXXXXXXXXX"
} ],
"name" : "system1_credentials"
}
Step 2) Policy used to lookup The key Value map
<KeyValueMapOperations enabled="true" continueOnError="false" async="false" name="keymap_get_credentials" mapIdentifier="system1_credentials">
<DisplayName>keymap_get_credentials</DisplayName>
<FaultRules/>
<Properties/>
<ExpiryTimeInSecs>-1</ExpiryTimeInSecs>
<Get assignTo="basic_auth_system1">
<Key>
<Parameter>basic_auth_system1</Parameter>
</Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>