【问题标题】:Implementing Custom Authentication in Spring Security在 Spring Security 中实现自定义身份验证
【发布时间】:2016-07-24 19:14:20
【问题描述】:

我想就我在 Spring Security 中遇到的问题寻求您的帮助。 我有一个要求,我必须根据用户选择的选项来验证登录凭据。选项 1 将通过第三方服务验证登录用户。选项 2 将是使用数据库身份验证级别的正常验证。我该如何实施?

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    总体战略

    1. 提供org.springframework.security.authentication.AuthenticationProvider 的自定义实现,将身份验证委托给适当的后端(第三方服务、另一个AuthenticationProvider 等)。
    2. 将用户选择的选项传递给自定义AuthenticationProvider,使其能够选择正确的身份验证后端。
    3. 将自定义AuthenticationProvider 配置为默认身份验证提供程序。

    第 1 步:实施AuthenticationProvider

    AuthenticationProvider 是一个单一方法的接口。因此,自定义实现可能如下所示:

    class DelegatingAuthenticationProvider implements AuthenticationProvider {
      @Autowired
      private ThirdPartyAuthenticationService service;
    
      @Autowired
      @Qualifier("anotherAuthenticationProvider")
      private AuthenticationProvider provider;
    
      @Override
      public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
        // Get the user selection.
        String selection = (String) authentication.getDetails();
    
        // Take action depending on the selection.
        Authentication result;
        if("ThirdParty".equals(selection)) {
          // Authenticate using "service" and generate a new
          // Authentication "result" appropriately.
        }
        else {
          // Authenticate using "provider" and generate a new
          // Authentication "result" appropriately.
        }
    
        return result;
      }
    }
    

    第 2 步:将用户选择传递给AuthenticationProvider

    上面的AuthenticationProvider 实现从Authentication 对象的details 属性中获取用户选择。据推测,在调用AuthenticationProvider 之前,必须从HttpServletRequest 中提取用户选择并将其添加到Authentication 对象中。这意味着,在调用AuthenticationProvider 之前,需要调用另一个可以同时访问AuthenticationHttpServletRequest 对象的组件。

    Authentication 对象由AbstractAuthenticationProcessingFilter 的实现创建。这个类有一个名为attemptAuthentication 的方法,它接受一个HttpServletRequest 对象并返回一个Authentication 对象。因此,这似乎是实施所需内容的一个很好的候选者。对于基于用户名-密码的身份验证,实现类为UsernamePasswordAuthenticationFilter。此类返回UsernamePasswordAuthenticationToken 的新实例,它是Authentication 的实现。所以,一个扩展 UsernamePasswordAuthenticationFilter 的类就足够了。

    class ExtendedUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
      @Override
      public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        ...
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password);
        authentication.setDetails(obtainUserSelection(request));
    
        ...
    
        return authentication;
      }
    }
    

    obtainUserSelection 是从请求中获取用户选择的私有方法。

    第 3 步:配置

    在 Spring Security 配置中配置 AuthenticationProvider 和过滤器实现。具体步骤将根据使用的是 XML 还是 Java 配置而有所不同。

    【讨论】:

    猜你喜欢
    • 2014-12-04
    • 2013-08-15
    • 2014-04-20
    • 2014-12-13
    • 2014-01-12
    • 2016-08-05
    • 2020-12-05
    • 2015-04-27
    • 2012-03-24
    相关资源
    最近更新 更多