【问题标题】:Mule HTTP Basic Authentication with intercept url带有拦截 URL 的 Mule HTTP 基本身份验证
【发布时间】:2015-11-06 15:13:31
【问题描述】:

我想为我的 mule 应用程序实现 HTTP 基本身份验证,该应用程序侦听 HTTP,URI 是 http://localhost:8082/api/customers。假设只支持 POST 和 GET。

我试图以这样的方式实现身份验证

  • 如果用户具有 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 Listener 之后,如下所示

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

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

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

谁能指导我以正确的方式实现上述要求?

【问题讨论】:

    标签: spring authentication mule


    【解决方案1】:

    您可以查看此示例 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>
    

    【讨论】:

    • 我试过你的答案。但是我在部署过程中遇到了一个错误:cvc-complex-type.2.4.a: Invalid content was found starting with element 'mule-ss:authorization-filter'. One of '{"http://www.mulesoft.org/schema/mule/core":annotations}' is expected. 我从这个错误中了解到我们不能在mule-ss:http-security-filter 中包含mule-ss:authorization-filter 元素。如果我错了,你能纠正我吗?
    【解决方案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>
    

    请注意,必须将角色 ROLE_ADMIN 和 ROLE_USER 分配给用户 admin,因为“mule-ss:authorization-filter”不允许多值。

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 2018-01-24
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      相关资源
      最近更新 更多