1

我想在身份验证失败后返回 401 HTTP_response。我试图通过使用具有以下配置的 http:response-builder 组件来做到这一点。我不关心流程中之前的任何事情,一旦达到这一点,它需要做的就是这个。

<flow name="main" doc:name="main">
<http:inbound-endpoint exchange-pattern="request-response"
    address="http://localhost:8081/example" doc:name="HTTP" />
<byte-array-to-string-transformer
    doc:name="Byte Array to String" />
<cxf:proxy-service namespace=""
        service="" payload="envelope"
        wsdlLocation=""
        enableMuleSoapHeaders="false" doc:name="ProxyService"/>
<cxf:proxy-client payload="envelope" enableMuleSoapHeaders="true" doc:name="ProxyClient" />
<http:outbound-endpoint exchange-pattern="request-response"
        address=""
        doc:name="request" />
<byte-array-to-string-transformer
    doc:name="Byte Array to String" />
<logger message="The payload after the request is:#[payload]"
    level="INFO" doc:name="Logger" />
<choice doc:name="Choice">
...
<otherwise>
    <processor-chain>
        <logger message="Failed authentication is:#[payload]" level="INFO"
            doc:name="Logger" />
        <http:response-builder status="401"
            doc:name="HTTP Response Builder">
            <set-payload value="Authentication Failed" />
        </http:response-builder>
    </processor-chain>
</otherwise>

我通过soapUI收到的错误

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Could not find a transformer to transform "SimpleDataType{type=org.mule.transport.http.HttpResponse, mimeType='text/xml'}" to "SimpleDataType{type=java.io.InputStream, mimeType='*/*'}". (org.mule.api.transformer.TransformerException). Message payload is of type: PostMethod</faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>.

对我来说,这说明我当前的消息是 text/xml,它正试图返回一个未知类型。我尝试将构建器的内容类型设置为 text/xml,结果相同

编辑:当前状态是 HTTP/1.1 500 Internal Server Error

非常感谢您花时间阅读和任何回复。

4

1 回答 1

1

您不能在使用它的http:response-builder地方使用它,因为它的输出不会由 HTTP 入站端点直接处理,而是由cxf:proxy-service. 当响应向下游流向入站端点时,它会命中cxf:proxy-service负责准备 SOAP 响应的流:它只能处理具有 SOAP 有效负载的消息,并将严格控制状态代码,覆盖任何设置它的尝试流中的其他地方。此外,在您的情况下,您正在构建一个非 SOAP 响应,cxf:proxy-service当它处理该响应时,这将更加令人不快。

所以诀窍就是将登录失败的检测与HTTP响应的构建分开,将HTTP响应构建定位在流的响应阶段。这给出了:

<flow name="main" doc:name="main">
    <http:inbound-endpoint exchange-pattern="request-response"
        address="http://localhost:8081/example" doc:name="HTTP" />
    <response>
        <expression-filter expression="#[flowVars.authenticationFailed]" />
        <http:response-builder status="401"
            doc:name="HTTP Response Builder">
            <set-payload value="Authentication Failed" />
        </http:response-builder>
    </response>
    <byte-array-to-string-transformer
        doc:name="Byte Array to String" />
    <cxf:proxy-service namespace=""
            service="" payload="envelope"
            wsdlLocation=""
            enableMuleSoapHeaders="false" doc:name="ProxyService"/>
    <cxf:proxy-client payload="envelope" enableMuleSoapHeaders="true" doc:name="ProxyClient" />
    <http:outbound-endpoint exchange-pattern="request-response"
            address=""
            doc:name="request" />
    <byte-array-to-string-transformer
        doc:name="Byte Array to String" />
    <logger message="The payload after the request is:#[payload]"
        level="INFO" doc:name="Logger" />
    <set-variable variableName="authenticationFailed" value="false" />
    <choice doc:name="Choice">
    ...
    <otherwise>
        <processor-chain>
            <logger message="Failed authentication is:#[payload]" level="INFO"
                doc:name="Logger" />
            <set-variable variableName="authenticationFailed" value="true" />
        </processor-chain>
    </otherwise>

请注意使用流变量(名为authenticationFailed)来控制响应。

于 2012-10-18T16:28:33.657 回答