将每个http块映射到特定的身份验证提供程序

c12*_*c12 2 spring-security

我想根据用户的上下文路径建立我的Spring Security配置.如果用户使用http:// path1/resource1反对url,我想将它们定向到特定的身份验证提供程序.如果他们进入http:// path2/resource2,我想将他们引导到另一个身份验证提供程序.这些url路径是基于REST的Web服务调用,这就是为什么它们是无状态的而不是来自表单.目前,所有身份验证提供程序都已执 这种情况的最佳方法是什么?我正在使用spring-security 3.1.0.M1.

<http pattern="/path1/**" create-session="stateless">
        <intercept-url pattern="/**" access="ROLE_USER,ROLE_VAR,ROLE_ADMIN" />
        <http-basic />      
</http>
<http pattern="/path2/**" create-session="stateless">
        <intercept-url pattern="/**" access="ROLE_USER,ROLE_VAR,ROLE_ADMIN" />
        <http-basic />      
</http>
Run Code Online (Sandbox Code Playgroud)

Car*_*cha 7

您可以在每个http块中定义身份验证管理器引用:

<http pattern="/api/**" authentication-manager-ref="apiAccess">
    ...
</http>

<http auto-config = "true" authentication-manager-ref="webAccess">
    ...
</http>

<!-- Web authentication manager -->
<authentication-manager id="webAccess">
    <authentication-provider
        user-service-ref="userService">
    </authentication-provider>
</authentication-manager>

<!-- API authentication manager -->    
<authentication-manager id="apiAccess">
    <authentication-provider
        user-service-ref="developerService">
    </authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)

Spring Security 3.1中添加了此功能.

  • 注意使用`id`而不是`alias`作为`authentication-manager`.如果使用`alias`,Spring Security似乎可以选择错误的身份验证管理器. (2认同)