【问题标题】:Combining multiple spring security authentication providers into custom single one将多个 Spring Security 身份验证提供程序组合成自定义单个
【发布时间】:2018-05-21 15:14:10
【问题描述】:

我的目标是使用凭据进行两步身份验证。第一步是检查具有主体的用户是否在特殊的数据库表中具有角色。二是执行标准的ldap认证。

我需要同时执行 两个 检查,但身份验证提供程序的常见方法是在 any 身份验证提供程序第一次成功后声明身份验证成功。所以我决定创建一个自定义的 AuthenticationProvider 实现,它调用 LdapAuthenticationProvider 然后执行数据库检查逻辑,但它不起作用,因为没有什么可以与 AbstractLdapAuthenticationProvider 自动装配。

请告诉我是否

  1. 解决此类问题的方法是合理的
  2. 如果合理,我该如何注入 AbstractLdapAuthenticationProvider?

安全配置代码为

@Autowired
private DBRoleAuthenticationProvider dbRoleAuthenticationProvider;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.ldapAuthentication()
            .contextSource()
            .url("...")
            .managerDn("...")
            .managerPassword("...")
            .and()
            .userSearchFilter("uid={0}");
    auth.authenticationProvider(dbRoleAuthenticationProvider);
}

自定义身份验证提供程序是

@Component
public class DBRoleAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private UserHasRoleInDBService userHasRoleInDBService;

    @Autowired
    private AbstractLdapAuthenticationProvider ldapAuthenticationProvider;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        authentication = ldapAuthenticationProvider.authenticate(authentication);
        if (!authentication.isAuthenticated()) {
            return authentication;
        }
        try {
            String loginToSearch = (String) authentication.getPrincipal();
            if (!userHasRoleInDBService.userHasRole(loginToSearch)) {
                authentication.setAuthenticated(false);
            }
        } catch (Exception e) {
            authentication.setAuthenticated(false);
        }
        return authentication;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

提前致谢!

【问题讨论】:

    标签: spring-security spring-java-config


    【解决方案1】:

    看起来像auth.authenticationProvider(dbRoleAuthenticationProvider); 正在击败auth.ldapAuthentication()

    您应该通过查看文档来自定义为经过身份验证的用户检索权限的方式 userDetailsContextMapper(UserDetailsContextMapper userDetailsContextMapper) and ldapAuthoritiesPopulator(LdapAuthoritiesPopulator ldapAuthoritiesPopulator) LdapAuthenticationProviderConfigurer的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-18
      • 2018-05-07
      • 2011-02-09
      • 1970-01-01
      • 2012-03-07
      • 2016-07-19
      • 2016-09-24
      • 2011-01-03
      相关资源
      最近更新 更多