使用SQL的Spring LDAP / Active Directory

时间:2014-05-15 11:44:49

标签: active-directory spring-security-ldap

我正在尝试启用JDBA和Active Directory身份验证,我取得了很大的进步,但目前我被卡住了,因为userDetailsS​​ervice正在尝试比较LdapUserDetails中不存在的密码。检查日志时,我看到它能够查询用户并进行身份验证并正确获取角色。

我知道我应该使用bindService左右,但到现在为止我怎么也找不到。

以下是我的所作所为。

WebSecurityConfigurerAdapter中的

@Autowired
public void configureGlobal(UserDetailsService userDetailsService,UserLdapRepositoryUserDetailsService userLdapRepositoryUserDetailsService,AuthenticationManagerBuilder auth) throws Exception {
    auth
        .userDetailsService(userDetailsService).
    and()
        .userDetailsService(userLdapRepositoryUserDetailsService);
}

对于LDAP配置

    @Bean
public BaseLdapPathContextSource contextSource() {
    DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://XXXXX:389/dc=XXXXXX,dc=co");
    //contextSource.setUserDn("CN=Ali Shahbour,OU=Users,DC=XXXXXXX,DC=co");
    contextSource.setUserDn("XXXXXX");
    contextSource.setPassword("XXXXXX");
    return contextSource;
}

@Bean
@Autowired
public LdapUserSearch userSearch(BaseLdapPathContextSource contextSource) { 
    FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch("", "(uid={0})", contextSource);
    return userSearch;
}

@Bean 
@Autowired
public LdapAuthoritiesPopulator authoritiesPopulator(BaseLdapPathContextSource contextSource) {

    DefaultLdapAuthoritiesPopulator authoritiesPopulator = new DefaultLdapAuthoritiesPopulator(contextSource, "OU=CDRMonitor");

    authoritiesPopulator.setGroupSearchFilter("(member={0})");
    //authoritiesPopulator.setRolePrefix("ROLE");
    authoritiesPopulator.setSearchSubtree(true);
    //authoritiesPopulator.setConvertToUpperCase(true);

    return authoritiesPopulator;
}

至于LdapUserDetailsS​​ervice

@Service("userLdapRepositoryUserDetailsService")
public class UserLdapRepositoryUserDetailsService extends LdapUserDetailsService {

private final UserRepository userRepository;

@Autowired
public UserLdapRepositoryUserDetailsService(LdapUserSearch userSearch,
        LdapAuthoritiesPopulator authoritiesPopulator,UserRepository userRepository) {
    super(userSearch, authoritiesPopulator);
    this.userRepository = userRepository;


}

@Override
public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {
    UserDetails userDetails = super.loadUserByUsername(username);
    //User user = userRepository.findByEmail(username);
    User user = new User();
    return new LdapUserRepositoryUserDetails(user, userDetails);
}

@Override
public void setUserDetailsMapper(UserDetailsContextMapper userDetailsMapper) {
    super.setUserDetailsMapper(userDetailsMapper);
}

private final static class LdapUserRepositoryUserDetails extends User implements LdapUserDetails {

    private final LdapUserDetailsImpl ldapUserDetailsImpl;

    private LdapUserRepositoryUserDetails(User user,UserDetails userDetails) {
        super(user);
        ldapUserDetailsImpl = (LdapUserDetailsImpl) userDetails;
    }

    private static final long serialVersionUID = 5639683223516504866L;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return ldapUserDetailsImpl.getAuthorities();
    }

    @Override
    public String getUsername() {
        // TODO Auto-generated method stub
        return ldapUserDetailsImpl.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return ldapUserDetailsImpl.isAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return ldapUserDetailsImpl.isAccountNonLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return ldapUserDetailsImpl.isCredentialsNonExpired();
    }

    @Override
    public boolean isEnabled() {
        return ldapUserDetailsImpl.isEnabled();
    }

    @Override
    public String getDn() {
        return ldapUserDetailsImpl.getDn();
    }
}

}

1 个答案:

答案 0 :(得分:0)

身份验证上下文中的LDAP和SQL通常不会一起使用, 因为LDAP BIND身份验证会将密码发送到LDAP服务器,而不是检索密码的哈希值。

LDAP身份验证的预期过程如下:

  • 通常,spring security UsernamePassword过滤器用于获取凭据,默认情况下处于活动状态。例如如果您是我们的登录表单,则在提交表单时,此过滤器会获取凭据。
  • 接下来,LDAP身份验证提供程序对LDAP服务器(LDAP ContextSource)执行登录(LDAPBindAuthenticator)以验证凭据。
  • 如果登录成功,LDAP身份验证提供程序将在LDAP中搜索用户条目。这可以通过提供“用户”来定制。春豆。
  • 如果找到用户条目,LDAP Authority映射器会将用户条目的属性映射到spring security中的组/角色。默认情况下,这是所有OU属性
  • 最后,使用用户名和LDAP中检索到的组创建新的身份验证对象。

这里解释如何使用Spring XML集成LDAP身份验证。 http://docs.spring.io/spring-security/site/docs/3.1.x/reference/ldap.html