2015-10-28 118 views
0

我想使用基本認證,並獲得騾流的憑據。騾基本認證

這裏是我的騾子流向:

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

運行該應用程序,我使用REST客戶端在Chrome和選擇基本身份驗證,然後給用戶名和密碼。

上面的代碼工作正常。我需要的不是在Java中獲取證書,而是將其放入Map中,我需要在不使用Java代碼的情況下獲取Mule流中的證書。這可能直接在騾流中嗎?

我已經實施了Spring安全工作正常,但在這種情況下,我需要用戶輸入憑據並進一步處理。

回答

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

+0

正如我在我的問題告知,我能夠做到這一點。 – bekur

+0

我已經修改了答案,以包含您可以在流程中獲取憑據的方式。這不是一種直接的方式,但是你可以在不創建Java類的情況下做到這一點。 –

+0

騾3.8.3有問題的字符串拆分。使用這個: https://forums.mulesoft.com/questions/56799/split-statement.html – bekur