WSO2 Synapse:设置 URL 参数 [英] WSO2 Synapse: setting a URL parameter

查看:24
本文介绍了WSO2 Synapse:设置 URL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些看似简单但无法正常工作的事情.基本上我希望 WSO2 API 管理器将 URL 参数添加到 REST 调用.

I am trying to do something that seems straightforward but can't get it to work. Basically I want the WSO2 API manager to add a URL parameter to a REST call.

我安装了 WSO2 API 管理器.我还有一个 Tomcat 运行一个名为 someservlet 的伪造 servlet,它只记录它接收到的任何内容并返回 ok.我已将 servlet 作为 API 添加到管理器中,因此我可以直接调用 servlet 或通过 WSO2 API mgr.

I have a WSO2 API manager installed. I also have a Tomcat running with a bogus servlet called someservlet that simply logs anything it receives and returns ok. I have added the servlet as an API in the manager, so I can either call the servlet directly or via WSO2 API mgr.

我可以跑

curl http://localhost:8080/someservlet/servlet/toto?blob=blib&secret=pass

servlet 工作正常,它告诉我它收到了路径 /toto 和参数 blobsecret.

and the servlet works fine, it tells me it's received the path /toto and parameters blob and secret.

我可以跑

curl -H "Authorization: Bearer [...]" --url "http://192.168.23.1:8280/someservlet/1.0/toto?blob=blib&secret=pass"

它的作用完全一样.到目前为止,一切顺利.

And it does exactly the same. So far, so good.

我想要的是运行:

curl -H "Authorization: Bearer MqVQuHqLNphtPV3XF1CtXVmbyP8a" --url "http://192.168.23.1:8280/someservlet/1.0/toto?blob=blib"

(请注意,我已删除 secret 参数)

(note that I've removed the secret parameter)

...仍然得到相同的结果.

...and still get the same result.

所以基本上我希望 API 管理器添加 URL 参数 secret=pass.

So basically I want API manager to add the URL parameter secret=pass.

使用带有 属性 REST_URL_POSTFIX 的 Synapse XML 配置.

Use Synapse XML config with property REST_URL_POSTFIX.

我编辑了 API 配置文件,并添加了

I edited the API config file, and added

<property name="REST_URL_POSTFIX" value="/blob?toto=titi" scope="axis2" type="STRING"/>

现在如果我跑

curl -H "Authorization: Bearer [...]" --url "http://192.168.23.1:8280/someservlet/1.0/toti?blab=blib&secret=puss"

就好像我运行了 someservlet/1.0/blob?toto=titi:我的所有路径和参数都消失了,并被配置的路径和参数替换.嘿,这就是它应该的工作方式,不是吗!

it's as if I ran someservlet/1.0/blob?toto=titi: all my path and parameters have disappeared and been repaced with the configured ones. Hey, that's how it's supposed to work, isn't it!

问题:

  1. 这不会向 URL 添加任何内容,它会设置 URL 后缀,这意味着现有参数会消失(在上面的示例中,blab=blib)
  2. 它必须以 "/" 开头才能成为有效值,所以我不能只添加 &secret=pass (当然,因为问题1,这无论如何都没用)
  1. this doesn't add something to the URL, it sets the URL postfix, meaning that existing parameters disappear (in the above example, blab=blib)
  2. it has to start with a "/" to be a valid value, so I can't just add &secret=pass (of course, because of problem 1, this would be useless anyway)

所以基本上这不能让我附加最后的 &secret=pass.

So basically this doesn't enable me to append the final &secret=pass.

我找到了这个中介,虽然它可能不会成功,但这是一个很好的线索:我可以用 secret=foo 调用,然后让中介用 替换它>secret=pass.

I found this mediator, and although it probably won't do the trick, it's a good lead: I can just call with secret=foo, and get the mediator to replace it with secret=pass.

我把它放在配置文件中:

I put this in the config file:

<rewrite>
  <rewriterule>
    <action type="replace" value="pass" fragment="query" regex="foo"/>
  </rewriterule>
</rewrite>

这不起作用.起初我以为我没有正确的动作参数.但是错误信息是:

This doesn't work. At first I thought I didn't have the action parameters right. But the error message is:

Malformed URL when processing /someservlet/1.0/toti?blab=blib&amp;secret=foo

格式错误?异常堆栈跟踪中有更多详细信息:

Malformed? There's more detail in the exception stack trace:

java.net.MalformedURLException: no protocol: /someservlet/1.0/toti?blab=blib&secret=foo

所以发生的情况是中介(记录或重写)收到一条消息,其 "To:" 字段指向一个没有协议的 URL!

So what's happening is that the mediators (log or rewrite) receive a message whose "To:" field points to a URL with no protocol!

当然,我一直在用谷歌搜索,在某些情况下我发现其他人有 logMediator: To:/blabla,而其他(大多数)情况下他们有 logMediator: 到: http://blabla.我真的不明白是什么导致了这种差异.:-(

Of course I've been googling around, and there are some cases where I find other people have logMediator: To: /blabla, and other (most) cases where they have logMediator: To: http ://blabla. I don't really see what's causing the difference. :-(

所以这就是我卡住的地方!:-(

我知道可能有一个大锤解决方案应该可以工作:

I'm aware that there's probably a sledgehammer solution that should work:

  1. 使用属性存储完整路径和所有参数
  2. 实现我自己的中介器(例如在 Java 中)来修改这些参数
  3. 使用 property REST_URL_POSTFIX 将修改后的后缀放在调用上
  1. use property to store the full path and all parameters
  2. implement my own mediator (e.g. in Java) to modify these parameters
  3. use property REST_URL_POSTFIX to put the modified postfix on the call

不过我觉得这个问题应该有更简单的解决方案.

However I feel that this problem should have a simpler solution.

我有一种希望,有人会给我指出一个我还没有找到的简单资源(中介、样本、语法错误等),而这正是我想要的.乐观... :-)

I have a kind of hope that someone will point me to a simple resource (mediator, sample, syntax error, anything) that I haven't found and that does just what I want. Optimism... :-)

感谢阅读.有什么想法吗?

Thanks for reading. Any ideas?

推荐答案

对于可能遇到相同问题的任何人,这里有另一种解决方案,更简单且有效.

For anyone who may have the same issue, here's another solution, simpler and works.

进入 carbon 管理门户,进入 API 列表,找到相关的 API 并点击它.这导致 API 的 XML 配置.在地址"字段之后(在 XML 中处于同一级别)添加字段:

Go to the carbon admin portal, to the list of APIs, find the relevant API and click on it. This leads to the XML config of the API. After the "address" field (and at the same level in the XML) add the field:

<property name="Authorization" value="stuff to add" scope="transport"/>

这会添加属性Authorization",其值为stuff".

This adds the property "Authorization" with value "stuff".

这篇关于WSO2 Synapse:设置 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆