Mule基本身份验证

时间:2015-10-28 19:23:13

标签: mule

我正在尝试使用基本身份验证并获取Mule流中的凭据。

这是我的骡子流程:

Excel.Application.AutomationSecurity

Java代码:

<http:listener config-ref="httpConfig" path="/*" doc:name="HTTP" allowedMethods="get, post, delete" />
<apikit:router config-ref="api-config" doc:name="APIkit Router" />
<flow name="get:/sourcing/helloworld-secure:api-config">
   <flow-ref name="authenticate-ldap" doc:name="authenticate-ldap"/>
</flow> 

<flow name="authenticate-ldap">
    <logger message="Name: #[message.inboundProperties.get('username')] Password:#[message.inboundProperties['password']] level="INFO"/>
    <component class="com.test.AuthTest" doc:name="Java"/>
    <logger message="After java: #[flowVars.username] #[flowVars.password]" level="INFO" doc:name="Logger"/>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <data-mapper:transform config-ref="Map_auth_to_ldap_request" doc:name="Map auth to ldap request"/>      
 </flow>

要运行此应用程序,我在Chrome中使用rest客户端并选择Basic Auth,然后提供用户名和密码。

以上代码工作正常。我需要的是不是在Java中获取凭据然后放入Map,而是需要在不使用Java代码的情况下获取Mule流中的凭据。这是否可以直接在Mule流程中使用?

我已经实现了Spring安全性,但是在这种情况下,我需要用户输入凭据并继续进行。

1 个答案:

答案 0 :(得分:1)

为了实现基本身份验证,您可以使用“mule-ss:security-manager”元素提供身份验证源,并使用“http:basic-security-filter”元素来建立限制。最后,您可以使用“expresion-transformer”元素获取流中的凭据。这里有一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:file="http://www.mulesoft.org/schema/mule/file"
      xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"
      xmlns:db="http://www.mulesoft.org/schema/mule/db"
      xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml"
      xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
      xmlns:json="http://www.mulesoft.org/schema/mule/json"
      xmlns:ws="http://www.mulesoft.org/schema/mule/ws"
      xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps"
      xmlns:email="http://www.mulesoft.org/schema/mule/email"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"      
      xmlns:mule-ss="http://www.mulesoft.org/schema/mule/spring-security"
      xmlns:ss="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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/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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
        http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
        http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
        http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
        http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
        http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
        http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
        http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd
        http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.xsd
        http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.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.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
    ">

    <spring:beans>      
        <ss:authentication-manager alias="authenticationManager">
          <ss:authentication-provider>
            <ss:user-service id="userService">
              <ss:user name="user" password="password" authorities="ROLE_ADMIN" />
              <ss:user name="anon" password="anon" authorities="ROLE_ANON" />
            </ss:user-service>
          </ss:authentication-provider>
        </ss:authentication-manager>
    </spring:beans>

    <mule-ss:security-manager>
      <mule-ss:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" />
    </mule-ss:security-manager>

    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="9091" doc:name="HTTP Listener Configuration"/>    
    <flow name="testingFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/*" doc:name="HTTP"/>
        <logger message="Before Authentication" level="INFO" doc:name="Log Failure"/>
        <http:basic-security-filter realm="mule-realm"/>
        <set-payload value="#[message.inboundProperties.'Authorization']"/>
        <set-payload value="#[message.payloadAs(java.lang.String).substring('Basic'.length()).trim()]"/>
        <expression-transformer expression="#[new String(org.mule.util.Base64.decode(payload),java.nio.charset.Charset.forName('UTF-8')).split(':');]" />
        <set-payload value="#[['user':payload[0],'password':payload[1]]]"/>
        <logger message="#[payload]" level="INFO" doc:name="User - Password"/>

    </flow>


</mule>

文档参考:

https://docs.mulesoft.com/mule-user-guide/v/3.7/http-listener-connector#authentication