2015-11-04 66 views
2

我想为我的mule应用程序实现HTTP基本身份验证,它在HTTP上侦听,URI是http://localhost:8082/api/customers。让我们假设只支持POST和GET。Mule HTTP基本身份验证和拦截url

我想以这样的方式

  • 如果用户有ROLE_ADMIN的角色,他可以访问POST和GET端点
  • 如果用户只ROLE_USER角色,他可以实现认证只访问GET端点

我想利用Spring身份验证管理器来达到此目的。我配置如下认证:

首先,我创建了认证管理器

<ss:authentication-manager xmlns:ss="http://www.springframework.org/schema/security" alias="authenticationManager"> 
    <ss:authentication-provider> 
     <ss:user-service id="userService"> 
      <ss:user name="user" password="user" authorities="ROLE_USER" /> 
      <ss:user name="admin" password="admin" authorities="ROLE_ADMIN" /> 
     </ss:user-service> 
    </ss:authentication-provider> 
</ss:authentication-manager> 

后来我想拦截的URL

<ss:http auto-config="true" realm="mule-realm" use-expressions="true"> 
    <ss:intercept-url pattern="/api/customers" method="GET" access="ROLE_USER"/> 
    <ss:intercept-url pattern="/api/customers" method="POST" access="ROLE_ADMIN"/> 
</ss:http> 

最后我把basic-security-filter HTTP侦听器后,像下面

<http:listener config-ref="CustomerAPI-httpListenerConfig" path="/api/*" doc:name="HTTP" /> 
<http:basic-security-filter realm="mule-realm" securityProviders="memory-provider" /> 

<mule-ss:security-manager xmlns:mule-ss="http://www.mulesoft.org/schema/mule/spring-security" name="muleSecurityManager"> 
    <mule-ss:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" /> 
</mule-ss:security-manager> 

现在,我所期望的是,请求fr om角色为ROLE_USER(具有user/user凭据)的用户只能访问GET,而来自角色为的用户的请求ROLE_ADMIN(具有admin/admin凭据)可以同时访问GET和POST。

但我错了。来自角色为ROLE_USER的用户的请求同时访问POST和GET,这是我从未想到的。

任何人都可以请指导我以正确的方式来达到上述要求吗?

回答

3

你可以看到这个例子raml-intro用spring-security设置你的api。在这种情况下,我使用security.properties来管理用户和角色。

正如你可以在这些lines中看到的,我有一个处理安全访问的流程,但没有角色限制。

为了处理您可以修改代码的角色,并添加以下行

<mule-ss:authorization-filter requiredAuthorities="ROLE_ADMIN"/> 

你的流程应该是这样的:

<flow name="get:/publishers:publishers-config"> 
    <mule-ss:http-security-filter realm="mule-realm" 
     securityProviders="security-provider"/> 
    <mule-ss:authorization-filter requiredAuthorities="ROLE_ADMIN"/> 
    <set-payload 
     value="[{&#xA; &quot;id&quot; : 1,&#xA; &quot;name&quot; : &quot;DC Comics&quot;&#xA;},&#xA;{&#xA; &quot;id&quot; : 2,&#xA; &quot;name&quot; : &quot;Marvel Comics&quot;&#xA;}]" 
     doc:name="Set Payload" /> 
</flow> 
+0

我试过你的答案。但是在部署过程中出现错误:'cvc-complex-type.2.4。a:从元素'mule-ss:authorization-filter'开始找到无效的内容。 “{”http://www.mulesoft.org/schema/mule/core":annotations}'之一是预期的。“我从这个错误中得到的理解是我们不能在里面有'mule-ss:authorization-filter'元素'骡子-SS:HTTP的安全filter'。如果我错了,你能纠正我吗? –

+0

对不起,我已经更新了我的答案。现在,它的工作。 –

2

如果要实现基于授权Http方法,你可以使用下面的例子:

<ss:authentication-manager xmlns:ss="http://www.springframework.org/schema/security" alias="authenticationManager"> 
    <ss:authentication-provider> 
     <ss:user-service id="userService"> 
      <ss:user name="user" password="user" authorities="ROLE_USER" /> 
      <ss:user name="admin" password="admin" authorities="ROLE_ADMIN,ROLE_USER" /> 
     </ss:user-service> 
    </ss:authentication-provider> 
</ss:authentication-manager> 

{...} 

<flow name="testingFlow"> 
    <http:listener config-ref="HTTP_Listener_Configuration2" path="/*" doc:name="HTTP" allowedMethods="GET,POST"/> 

    <http:basic-security-filter realm="mule-realm"/> 
    <set-variable variableName="method" value="#[header:INBOUND:http.method]" doc:name="method rest" /> 

    <choice> 
     <when expression="method.equals('GET')"> 
      <mule-ss:authorization-filter requiredAuthorities="ROLE_USER"/> 
     </when> 
     <when expression="method.equals('POST')"> 
      <mule-ss:authorization-filter requiredAuthorities="ROLE_ADMIN"/> 
     </when> 
    </choice>  
</flow> 

请注意,这是必要的因为“mule-ss:authorization-filter”不允许多个值,所以将角色ROLE_ADMIN和ROLE_USER分配给用户管理员。