0

我是 Mule 的新手,我想验证来自 URI 的时间戳和 nonce 参数以防止重放攻击。我使用choice-when路由器,当时间戳和nonce有效(验证过程复杂)时,将请求转发到后端rest服务,否则返回错误代码和消息。我发现很多 MEL 用法用于选择时,但是我可以使用 java 定义自定义表达式吗?有一个例子吗?URI 示例:

http://muledemo.org/ci2/ni/del?id=0xe413&timestamp=1376022133&nonce=a03ed9c

代码片段:

<choice doc:name="Choice">
  <when expression="???how to call a java validator???">
  </when>
  <otherwise>
    <processor-chain doc:name="Processor Chain">
      <echo-component doc:name="Echo" />
      <http:outbound-endpoint exchange-pattern="request-response" method="POST"
        address="http://localhost:8081#[message.inboundProperties['http.request']]" doc:name="HTTP" />
    </processor-chain>
  </otherwise>
</choice>

编辑:

XML配置:

<spring:beans>
  <spring:bean id="replayAttackCheck" class="org.jamee.demo.mule.ReplayAttackCheck"/>
</spring:beans>

... ...

<when expression="#[replayAttackCheck.validate(payload)]">
  <expression-component doc:name="Check Required Params"><![CDATA[payload="{\"code\":\"PA002\", \"message\":\"timestamp or nonce is illegal\"}"]]>
  </expression-component>
</when>

错误日志:

ERROR 2013-08-09 14:32:39,090 [[demo.router].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Execution of the expression "replayAttackCheck.validate(payload)" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: HashMap
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. [Error: unresolvable property or identifier: replayAttackCheck]
[Near : {... replayAttackCheck.validate(pay ....}]
4

2 回答 2

4

Correct MEL syntax:

<when expression="#[app.registry.replayAttackCheck.validate(payload)]">
于 2013-08-09T16:26:49.540 回答
0

您只需使用完全限定的路径

#[com.mulesoft.foo(payload, someFooVar)]

或者,您可以在配置中为您的应用程序全局定义它,并在 MEL 表达式中使用“foo”函数

<configuration doc:name="Config">
    <expression-language autoResolveVariables="false">
        <global-functions>
            def foo(fooVar){
            return com.mulesoft.foo(payload, fooVar);
            }                
        </global-functions>
    </expression-language>
</configuration>
于 2013-08-09T06:05:36.260 回答