使用案例:我们有一个现有的 生产环境 API . 我们打算使用API Manager作为传递中介,以便在我们现有的API上利用限制和分析 .
我们编写了一个自定义身份验证处理程序,它在默认 APIAuthenticationHandler
的上游加载 . 这允许我们通过自定义标头 X-Auth-Token
识别我们的用户,而不是WSO2 OAuth令牌 - 这部分适用于经过身份验证的路由 .
不幸的是,这不允许我们识别访问我们的身份验证路由的用户,结果是我们的分析数据与许多“未知”用户有关 .
我一直试图在我们的自定义身份验证处理程序中找出如何访问用户登录的HTTP POST参数,但我不能在我的生活中解决Synapse在类树中存储它的问题 .
在线提出各种建议,例如通过访问HttpServletRequest
HttpServletRequest servletRequest = (HttpServletRequest) messageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
不工作 .
这些参数不可用作MessageContext或Axis2MessageContext对象中的属性 .
log_in_message.xml
介体记录包含所需参数的SOAP Envelope:
Envelope: <?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<xformValues>
<api_key>*expunged*</api_key>
<login_id>testy.user</login_id>
</xformValues>
</soapenv:Body>
</soapenv:Envelope> {org.apache.synapse.mediators.builtin.LogMediator}
但在我的自定义处理程序中,消息上下文中只有一个空的SoapEnvelope:
[PassThru] <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body/></soapenv:Envelope>
我还无法找到一个明显的中介模式来添加到我们的API的synapse序列,以便将此REST参数注入messagecontext中的属性 . 我在网上找到的所有内容都涉及动态地向URI添加参数 .
有问题的请求是HTTP 1.1 POST(内容类型x-www-form-encoded),并且由下游服务成功处理,因此绝对包含参数 .
任何人都可以建议我可以在中介树或自定义处理程序中从java类中获取HTTP POST正文或正文参数吗?
谢谢
编辑:对于那些将来某个时间来到这里的人来说,事实证明它就像在我的处理程序中调用调解器一样简单:
public boolean handleRequest(MessageContext messageContext) {
...
// mediate
// In order to avoid a remote registry call occurring on each invocation, we
// directly get the extension sequences from the local registry.
Map localRegistry = messageContext.getConfiguration().getLocalRegistry();
// run inbound mediations
String apiName = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API);
Object sequence = localRegistry.get(apiName + "--In");
if (sequence instanceof Mediator) {
((Mediator) sequence).mediate(messageContext);
}
编辑2:Bee提出的更好的解决方案如下:
private String getLoginIdFromSoapEnvelope(org.apache.axis2.context.MessageContext axis2MessageContext) {
String loginId = null;
try {
RelayUtils.buildMessage(axis2MessageContext);
SOAPEnvelope envelope = axis2MessageContext.getEnvelope();
OMElement root = envelope.getBody().getFirstElement();
OMElement loginElement = root.getFirstChildWithName(new QName("login_id"));
loginId = loginElement != null ? loginElement.getText() : null;
}catch (Exception exc) {
log.error("Unable to unmarshal via RelayUtils.buildMessage", exc);
}
return loginId;
}
1 回答
试试这个 .
(这可能需要一些调整.. - 用于名称空间等)
参考:https://docs.wso2.com/display/ESB500/Sample+380%3A+Writing+your+own+Custom+Mediation+in+Java