【问题标题】:Creating a custom authentication with Acegi/Spring Security使用 Acegi/Spring Security 创建自定义身份验证
【发布时间】:2010-10-01 16:24:08
【问题描述】:

我无法准确地发现我需要实现什么才能使用 Spring Security 在我的 Web 应用程序中使用自定义身份验证方法。我有一个带有 Spring Security 插件的 Grails 应用程序,该插件当前使用标准用户/密码身份验证和浏览器表单。这工作正常。

除此之外,我还需要实现一种机制来实现MAC 身份验证。如果 HTTP 请求包含多个参数(例如用户标识符、时间戳、签名等),我需要获取这些参数,执行一些哈希和签名/时间戳比较,然后对用户进行身份验证。

我不能 100% 确定从哪里开始。我需要扩展/实现哪些 Spring Security 类?我已经阅读了Reference Documentation 并且对这些概念有很好的理解,但我不确定我是否需要过滤器、提供者或管理器,或者在哪里/如何确切地创建Authentication 对象。我一直在尝试扩展AbstractProcessingFilter 和/或实现AuthenticationProvider,但我只是理解如何让它们都玩得很好。

【问题讨论】:

    标签: spring-security


    【解决方案1】:
    1. 实现一个自定义的AuthenticationProvider,它从AuthenticationgetCredentials()getDetails()getPrincipal() 获取您的所有身份验证信息。

      使用以下配置 sn-p 将其绑定到您的 Spring Security 身份验证机制中:

    <bean id="myAuthenticationProvider" class="com.example.MyAuthenticationProvider">
        <security:custom-authentication-provider />
    </bean>
    
    1. 如果您可以从标准实现中找到合适的,此步骤是可选的。如果没有,请实现一个扩展 Authentication 接口的类,您可以在该接口上放置您的身份验证参数:

      (e.g. a user identifier, timestamp, signature, etc.)
      
    2. 扩展一个自定义SpringSecurityFilter 将上述两个类联系在一起。例如,过滤器可能会获取AuthenticationManager 并使用您的Authentication 实现作为输入调用authenticate()

      您可以扩展 AbstractAuthenticationProcessingFilter 作为开始。

      您可以引用 UsernamePasswordAuthenticationFilter 扩展 AbstractAuthenticationProcessingFilterUsernamePasswordAuthenticationFilter 实现标准的用户名/密码认证。

    3. 配置您的 Spring Security 以添加或替换标准 AUTHENTICATION_PROCESSING_FILTER。 Spring Security Filter 订单见http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html#filter-stack

      这里是一个配置 sn-p,说明如何用你的实现替换它:

    <beans:bean id="myFilter" class="com.example.MyAuthenticationFilter">
        <custom-filter position="AUTHENTICATION_PROCESSING_FILTER"/>
    </beans:bean>
    

    【讨论】:

    • 我认为您需要使用 PRE_AUTH_FILTER 位置而不是 AUTHENTICATION_PROCESSING_FILTER。此外,您可能希望查看本教程以了解在 Google AppEngine 中的身份验证实现:blog.springsource.com/2010/08/02/…
    【解决方案2】:

    我最近提出了一个使用 Spring Security 3 进行自定义身份验证的示例应用程序。 源代码是here。 更多详情在this blog post

    【讨论】:

      【解决方案3】:

      这是一个使用自定义 autenticationFilter(扩展 AUTHENTICATION_PROCESSING_FILTER)和 authenticationProvider 的 securityContext.xml 配置文件示例。用户认证数据由 jdbc 连接提供。配置适用于 Spring Security 2.0.x

      <?xml version="1.0" encoding="UTF-8"?>
      
       <sec:global-method-security />
      
       <sec:http auto-config="false" realm="CUSTOM" create-session="always" servlet-api-provision="true"
        entry-point-ref="authenticationProcessingFilterEntryPoint" access-denied-page="/notauthorized.xhtml"
        session-fixation-protection="migrateSession">
        <sec:port-mappings>
         <sec:port-mapping http="80" https="443" />
        </sec:port-mappings>
      
        <sec:anonymous granted-authority="ROLE_ANONYMOUS" username="Anonymous" />
        <sec:intercept-url pattern="/**" access="ROLE_ANONYMOUS, ROLE_USER" />
      
        <sec:logout logout-url="/logoff" logout-success-url="/home.xhtml" invalidate-session="false" />
      
       </sec:http>
      
       <bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
        <property name="loginFormUrl" value="/login.xhtml" />
        <property name="forceHttps" value="false" />
       </bean>
      
       <bean id="authenticationProcessingFilter" class="mypackage.CustomAuthenticationProcessingFilter">
        <sec:custom-filter position="AUTHENTICATION_PROCESSING_FILTER" />
        <property name="defaultTargetUrl" value="/" />
        <property name="filterProcessesUrl" value="/logon" />
        <property name="authenticationFailureUrl" value="/loginError.xhtml" />
        <property name="alwaysUseDefaultTargetUrl" value="false" />
        <property name="authenticationManager" ref="authenticationManager" />
       </bean>
      
       <jee:jndi-lookup id="securityDataSource" jndi-name="jdbc/DB_DS" /> 
      
       <bean id="myUserDetailsService" class="mypackage.CustomJdbcDaoImpl">
        <property name="dataSource" ref="securityDataSource" />
        <property name="rolePrefix" value="ROLE_" />
       </bean>
      
       <bean id="apcAuthenticationProvider" class="mypackage.CustomDaoAuthenticationProvider">
        <property name="userDetailsService" ref="myUserDetailsService" />
        <sec:custom-authentication-provider />
       </bean>
      
       <bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager">
        <property name="providers">
         <list>
          <ref local="apcAuthenticationProvider" />
         </list>
        </property>
       </bean>
      
      </beans>
      

      【讨论】:

        猜你喜欢
        • 2014-04-20
        • 2014-12-13
        • 2016-08-05
        • 2020-12-05
        • 2015-04-27
        • 2010-09-13
        • 2014-01-12
        • 2016-09-05
        • 2021-04-21
        相关资源
        最近更新 更多