【问题标题】:Mule Basic Authentication骡子基本认证
【发布时间】:2015-10-28 19:23:13
【问题描述】:

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

这是我的 Mule 流程:

<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>

Java 代码:

public class AuthTest implements Callable {
@Override
    public Map<String, String> onCall(MuleEventContext eventContext) throws Exception {     
        Map<String, String> authMap = new HashMap<String, String>();              
        final String authorization = eventContext.getMessage().getInboundProperty("authorization");        
        if (authorization != null && authorization.startsWith("Basic")) {           
            String base64Credentials = authorization.substring("Basic".length()).trim();
            String credentials = new String(Base64.decode(base64Credentials), Charset.forName(Base64.PREFERRED_ENCODING));
            if (credentials != null) {
                 final String[] values = credentials.split(":",2);              
                 eventContext.getMessage().setInvocationProperty("username", values[0]);
                 eventContext.getMessage().setInvocationProperty("password", values[1]);                 
                 authMap.put("username", values[0]);
                 authMap.put("password", values[1]);
            }
        }        
        return authMap;       
    }
}

要运行这个应用程序,我在 Chrome 中使用 rest 客户端并选择基本身份验证,然后提供用户名和密码。

以上代码运行良好。我需要的不是在 Java 中获取凭据然后放入 Map,而是在不使用 Java 代码的情况下获取 Mule 流中的凭据。这可以直接在 Mule 流中实现吗?

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

【问题讨论】:

    标签: mule


    【解决方案1】:

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

    <?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

    【讨论】:

    • 正如我在问题中告知的那样,我能够做到这一点。
    • 我已修改答案以包括您在流程中获取凭据的方式。这不是直接的方法,但您可以在不创建 java 类的情况下做到这一点。
    • Mule 3.8.3 存在字符串拆分问题。使用这个: forums.mulesoft.com/questions/56799/split-statement.html
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2018-02-14
    相关资源
    最近更新 更多