如何在Mule ESB中使用同步JMS实现HTTP基本身份验证?

时间:2012-11-04 20:41:34

标签: http rest authentication jms mule

我创建了一个简单的流,它公开了一个链接到REST服务的HTTP端点。 REST服务返回一个String,然后使用请求 - 响应模式将其置于JMS队列中。这个想法是放在队列中的字符串将由某个消费者使用,并且将使用Reply-To地址创建响应。所以基本上这就是同步JMS。

流程按预期工作,除非使用基本身份验证保护HTTP端点。在这种情况下,响应是:

java.io.NotSerializableException: com.sun.jersey.spi.container.ContainerResponse (org.apache.commons.lang.SerializationException). Message payload is of type: MuleResponseWriter$1

并且队列中没有消息。删除基本身份验证会导致不会抛出异常。此外,删除JMS出站端点会导致回显String,这使我相信问题出在JMS端点上。我怀疑问题出在JMS端点的响应上,因为将流中的组件设置为单向可以消除问题。我认为问题可能是端点在没有身份验证凭据的情况下向HTTP端点发送一些响应,因此失败,但我不确定如何处理此问题。

以下是我创建的流程:

<mule
    xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
    xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
    xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core"
    version="CE-3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mule-ss="http://www.mulesoft.org/schema/mule/spring-security"
    xmlns:json="http://www.mulesoft.org/schema/mule/json"
    xmlns:ss="http://www.springframework.org/schema/security" xsi:schemaLocation="
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
    http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd 
    http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
    http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
    http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd 
    http://www.mulesoft.org/schema/mule/spring-security http://www.mulesoft.org/schema/mule/spring-security/3.1/mule-spring-security.xsd 
    http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd ">
<jms:activemq-connector name="Active_MQ" specification="1.1" 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="RESTAPISync" doc:name="RESTAPISync">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP">
        <mule-ss:http-security-filter realm="realm"/>
    </http:inbound-endpoint>
    <jersey:resources doc:name="REST">
        <component class="SFREST"/>
    </jersey:resources>
    <jms:outbound-endpoint exchange-pattern="request-response" queue="tmp" connector-ref="Active_MQ"
                           doc:name="JMS"/>
</flow>

SFREST.java组件:

import javax.ws.rs.GET;
import javax.ws.rs.Path;


@Path("/pops")
public class SFREST{

    @GET
    public String getPOPs()  throws Exception{
        return "hello";
    }

}

例外:

ERROR 2012-11-04 21:40:57,485 [[jmstest].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message               : Authentication Attempt Failed. Message payload is of type: String
Code                  : MULE_ERROR-54999
--------------------------------------------------------------------------------
Exception stack is:
1. Bad credentials (org.springframework.security.authentication.BadCredentialsException)
    org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider:137 (null)
2. Authentication Attempt Failed. Message payload is of type: String (org.mule.api.security.UnauthorisedException)
org.mule.security.MuleSecurityManager:96 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/security/UnauthorisedException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.springframework.security.authentication.BadCredentialsException: Bad credentials
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:137)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at org.mule.module.spring.security.SpringProviderAdapter.authenticate(SpringProviderAdapter.java:70)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

ERROR 2012-11-04 21:41:04,522 [[jmstest].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message               : java.io.NotSerializableException: com.sun.jersey.spi.container.ContainerResponse (org.apache.commons.lang.SerializationException). Message payload is of type: MuleResponseWriter$1
Code                  : MULE_ERROR-29999
--------------------------------------------------------------------------------
Exception stack is:
1. com.sun.jersey.spi.container.ContainerResponse (java.io.NotSerializableException)
java.io.ObjectOutputStream:1164 (null)
2. java.io.NotSerializableException: com.sun.jersey.spi.container.ContainerResponse (org.apache.commons.lang.SerializationException)
org.apache.commons.lang.SerializationUtils:111 (null)
3. java.io.NotSerializableException: com.sun.jersey.spi.container.ContainerResponse (org.apache.commons.lang.SerializationException). Message payload is of type: MuleResponseWriter$1 (org.mule.api.MessagingException)
org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:35 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.io.NotSerializableException: com.sun.jersey.spi.container.ContainerResponse
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at org.apache.commons.collections.map.AbstractHashedMap.doWriteObject(AbstractHashedMap.java:1182)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

原来问题是jersey_response属性无法序列化,需要删除。因此,无论有效负载是什么,只要此属性在消息中,消息就无法传递给JMS出站组件。更多信息可在以下网址找到:

http://forum.mulesoft.org/mulesoft/topics/http_authentication_with_jms_request_response?utm_content=topic_link&utm_medium=email&utm_source=reply_notification

答案 1 :(得分:0)

异常的原因是因为任何jersey组件都会返回一个com.sun.jersey.spi.container.ContainerResponse,在将其传播到下一个消息处理器之前需要进行转换,如here所述