【问题标题】:Mapping each http block to a specific Authentication Provider将每个 http 块映射到特定的身份验证提供程序
【发布时间】:2011-08-14 01:15:09
【问题描述】:

我想根据用户的上下文路径来建立我的 Spring Security 配置。如果用户违反带有http://path1/resource1 的网址,我想将他们定向到特定的身份验证提供程序。如果他们通过http://path2/resource2 进来,我想将他们引导至不同的身份验证提供程序。这些 url 路径是基于 REST 的 Web 服务调用,因此它们是无状态的并且不是来自表单的原因。目前,所有身份验证提供程序都被执行。这种情况的最佳方法是什么?我正在使用弹簧安全 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>

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    您可以在每个 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>
    

    此功能已在 Spring Security 3.1 中添加。

    【讨论】:

    • 注意id 的使用,而不是alias 用于authentication-manager。如果您使用alias,Spring Security 似乎可以选择错误的身份验证管理器。
    【解决方案2】:

    这对我有用:

    <security:authentication-manager alias="basicAuthenticationManager">
      <security:authentication-provider user-service-ref="accountService">
        <security:password-encoder hash="sha"/>
      </security:authentication-provider>
      <security:authentication-provider user-service-ref="accountService"/>
    </security:authentication-manager>
    
    <bean id="basicProcessingFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
        <property name="authenticationManager">
            <ref bean="basicAuthenticationManager" />
        </property>    
        <property name="authenticationEntryPoint">
            <ref bean="basicProcessingEntryPoint" />
        </property>
    </bean>
    
    <bean id="basicProcessingEntryPoint"
        class="com.yourpackage.web.util.CustomBasicAuthenticationEntryPoint">
        <property name="realmName" value="yourRealm" />
    </bean>
    
    <!-- Stateless RESTful service using Basic authentication -->   
    <security:http pattern="/rest/**" create-session="stateless" entry-point-ref="basicProcessingEntryPoint">       
        <security:custom-filter ref="basicProcessingFilter" position="BASIC_AUTH_FILTER" />     
        <security:intercept-url pattern="/rest/new" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <security:intercept-url pattern="/rest/**" access="ROLE_USER" />
    </security:http>
    
    <!-- Additional filter chain for normal users, matching all other requests -->
    <security:http use-expressions="true">
        <security:intercept-url pattern="/index.jsp" access="permitAll" />      
        <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    
        <security:form-login login-page="/signin" 
            authentication-failure-url="/signin?signin_error=1" 
            default-target-url="/" 
            always-use-default-target="true"/>      
        <security:logout />
    </security:http>
    

    我实现了身份验证入口点,因为我需要在某些情况下发送一些特殊的错误代码,但您不需要这样做。

    【讨论】:

    • 感谢马丁卡斯特的回复。您可能需要澄清的一件事是如何将您的 rest http 块映射到一个身份验证提供程序,同时将您的表单 http 块映射到另一个身份验证提供程序。这就是我想要实现的目标,在你的例子中我不清楚。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-12-25
    • 2019-08-08
    • 2021-02-28
    • 1970-01-01
    • 2015-08-24
    • 2011-08-09
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多