2014-10-20 88 views
1

我有以下骡子流量: -如何获得HTTP状态代码为REST服务骡子

<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:[email protected]: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

现在的问题,只要我打的网址: - 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 

我不知道它为什么产生这样的例外..

但是,如果我从代码中删除异常块,打http://localhost:8083/myrest它工作正常,并要求用户名密码预期,并产生预期的结果..

但我不知道它为什么产生当我使用异常块代码例外.. 其实我使用异常块的目的是获取和在不同的情况

回答

3

有在玩两样东西在这里:

  1. 异常,由新泽西州的资源

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

  2. 例外通过mule-ss:http-security-filter

    这些异常是由骡子JAX-RS的范围之外抛甩,所以他们必须处理由catch-exception-strategy元素,正如你在你的问题一样。

有了这种错误处理的组合应该为你提供你所需要的。

+0

感谢David的回复 – 2014-10-22 16:09:12

1
控制不同的HTTP状态

来检索HTTP代码,您可以使用过滤器并在流程中引发异常。下面一个例子:

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

仍然收到相同的异常 – 2014-10-20 18:44:32