【问题标题】:How to get HTTP status code for REST service in Mule如何在 Mule 中获取 REST 服务的 HTTP 状态码
【发布时间】:2014-10-22 23:41:23
【问题描述】:

我有以下 Mule 流程:-

<jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>

    <mule-ss:security-manager>
        <mule-ss:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" />
    </mule-ss:security-manager>
    <spring:beans>
        <ss:authentication-manager alias="authenticationManager">
            <ss:authentication-provider>
                <ss:user-service id="userService">
                    <ss:user name="username" password="password" authorities="ROLE_ADMIN" />
                </ss:user-service>
            </ss:authentication-provider>
        </ss:authentication-manager>
    </spring:beans>

<flow name="testFlow1" doc:name="testFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8083" doc:name="HTTP">
  <mule-ss:http-security-filter realm="realm" />
</http:inbound-endpoint>

 <jersey:resources doc:name="REST">
    <component class="MyRest" />
  </jersey:resources>

 <logger message="Done !!!!!" level="INFO" doc:name="Logger"/>

     <!-- http://username:password@localhost:8083/myrest   or   http://localhost:8083/myrest and in this case we need to provide username and password from browser-->

 <catch-exception-strategy>
    <choice >
     <when expression="#[exception.getCauseException().getClass().getName()=='org.springframework.security.authentication.BadCredentialsException']">
     <logger level="INFO" message="Authentication failed exception: #[exception.getCauseException().getClass().getName()]"/>
     <set-property propertyName="http.status" value="401"/>
     <http:response-builder status="401" doc:name="Status code = 401" doc:description="Authentication failed"/>
      </when>
        <when expression="#[exception.getCauseException().getClass().getName()=='org.springframework.security.access.AccessDeniedException']">
      <logger level="INFO" message="Access denied exception: #[exception.getCauseException().getClass().getName()]"/>
      <set-property propertyName="http.status" value="405"/>
     <http:response-builder status="405" doc:name="Status code = 405" doc:description="Authorization exception - Access denied"/>
     </when>
      <otherwise>
      <logger message="An exception has been caught: #[exception.getCauseException().getClass().getName()]" level="ERROR" doc:name="Exception Thrown"/>
     <set-payload value="Error detected while processing" doc:name="Prepare response for client"/>
     <set-property propertyName="http.status" value="500"/>
     <http:response-builder status="500" doc:name="Status code = 500" doc:description="Exception - Sending a 500 Http Status code as Response"/>
     </otherwise>
      </choice>
  </catch-exception-strategy>

    </flow>

我参考了以下内容:- https://github.com/daveEason/mule-example-spring-security/blob/master/src/main/app/mule-config.xml

现在每当我点击 url 时都会出现问题:- http://localhost:8083/myrest ...我得到以下异常:-

ERROR 2014-10-20 23:06:52,223 [[test].connector.http.mule.default.receiver.03] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String (org.mule.api.security.UnauthorisedException)
  org.mule.transport.http.filters.HttpBasicAuthenticationFilter:156 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/security/UnauthorisedException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.security.UnauthorisedException: Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
    at org.mule.transport.http.filters.HttpBasicAuthenticationFilter.authenticateInbound(HttpBasicAuthenticationFilter.java:156)
    at org.mule.security.AbstractEndpointSecurityFilter.authenticate(AbstractEndpointSecurityFilter.java:54)
    at org.mule.security.AbstractAuthenticationFilter.doFilter(AbstractAuthenticationFilter.java:52)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

ERROR 2014-10-20 23:06:52,225 [[test].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: An exception has been caught: org.mule.api.security.UnauthorisedException
ERROR 2014-10-20 23:06:52,520 [[test].connector.http.mule.default.receiver.03] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String (org.mule.api.security.UnauthorisedException)
  org.mule.transport.http.filters.HttpBasicAuthenticationFilter:156 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/security/UnauthorisedException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.security.UnauthorisedException: Registered authentication is set to org.mule.module.spring.security.filters.http.HttpBasicAuthenticationFilter but there was no security context on the session. Authentication denied on endpoint http://localhost:8083. Message payload is of type: String
    at org.mule.transport.http.filters.HttpBasicAuthenticationFilter.authenticateInbound(HttpBasicAuthenticationFilter.java:156)
    at org.mule.security.AbstractEndpointSecurityFilter.authenticate(AbstractEndpointSecurityFilter.java:54)
    at org.mule.security.AbstractAuthenticationFilter.doFilter(AbstractAuthenticationFilter.java:52)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

ERROR 2014-10-20 23:06:52,522 [[test].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: An exception has been caught: org.mule.api.security.UnauthorisedException

我想知道为什么它会产生这样的异常..

但是如果我从代码中删除 exception block 并点击 http://localhost:8083/myrest 它可以正常工作并按预期询问 usernamepassword并产生预期的结果..

但我想知道为什么当我在代码中使用 异常块 时它会生成异常.. 其实我使用异常块的目的是在不同的情况下获取和控制不同的http状态

【问题讨论】:

    标签: mule mule-studio


    【解决方案1】:

    这里有两件事在起作用:

    1. Jersey 资源引发的异常

      此类异常必须由一个或多个自定义 JAX-RS 异常处理程序处理,并通过一个或多个 jersey:exception-mapper 元素注册到 jersey:resources

    2. mule-ss:http-security-filter抛出的异常

      这些异常是由 Mule 在 JAX-RS 范围之外引发的,因此它们必须由 catch-exception-strategy 元素处理,就像您在问题中所做的那样。

    拥有这种错误处理组合应该可以满足您的需求。

    【讨论】:

    • 感谢大卫的回复
    【解决方案2】:

    要检索 HTTP 代码,您可以使用过滤器并在流中引发异常。举个例子:

    <message-filter throwOnUnaccepted="true">
       <message-property-filter pattern="message.inboundProperties['http.status'] >= 400" />
    </message-filter>
    

    【讨论】:

    • 仍然遇到同样的异常
    猜你喜欢
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多