通过Active Directory LDAP使用Spring-Security进行身份验证

时间:2019-10-19 13:56:23

标签: spring spring-security active-directory ldap spring-ldap

我无法使用真实的活动目录进行身份验证,让我更好地解释一下,我尝试使用spring.io提出的示例进行身份验证没有问题,其中内部服务启动没有问题。 参考https://spring.io/guides/gs/authenticating-ldap/

我试图通过插入活动目录的配置来修改下面的代码,但没有成功。您能否在不使用示例中的内部服务的情况下指导我或向我展示真实连接的真实案例?我在网上查看了一下,但发现所有与官方示例类似的内容都没有任何真实案例

@Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userDnPatterns("uid={0},ou=people")
                .groupSearchBase("ou=groups")
                .contextSource()
                    .url("ldap://localhost:8389/dc=springframework,dc=org")
                    .and()
                .passwordCompare()
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword");
    }

错误显示: LDAP处理期间发生未分类的异常;嵌套异常为javax.naming.NamingException:[LDAP:错误代码1-000004DC:LdapErr:DSID-0C0907C2,注释:为了执行此操作,必须在连接上完成成功的绑定。数据0,v2580

3 个答案:

答案 0 :(得分:2)

是的,通过LDAP进行认证太麻烦了。为了能够对AD执行身份验证,您需要使用 ActiveDirectoryLdapAuthenticationProvider 。 这是工作示例:

@Override
protected void configure(AuthenticationManagerBuilder auth) {
    ActiveDirectoryLdapAuthenticationProvider adProvider =
            new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://localhost:8389");
    adProvider.setConvertSubErrorCodesToExceptions(true);
    adProvider.setUseAuthenticationRequestCredentials(true);
    auth.authenticationProvider(adProvider);
}

为节省您的时间,请阅读以下内容,这确实很重要: AD authentication doc

答案 1 :(得分:0)

解决方案是Yaroslav Kiryak的职位

答案 2 :(得分:0)

我在这里找到了一个样本,这很有用:

https://github.com/sachin-awati/Mojito/tree/master/webapp/src/main/java/com/box/l10n/mojito/security

可以有选择地实现 UserDetailsContextMapperImpl,如果在Active Directory查找期间找不到用户,mapUserFromContext会覆盖UserDetails以创建loadUserByUsername对象}}。

@Component
public class UserDetailsContextMapperImpl implements UserDetailsContextMapper {

    @Override
    public UserDetails mapUserFromContext(DirContextOperations dirContextOperations, String username, Collection<? extends GrantedAuthority> authorities) {

        UserDetails userDetails = null;

        try {
            userDetails = userDetailsServiceImpl.loadUserByUsername(username);

        } catch (UsernameNotFoundException e) {
            String givenName = dirContextOperations.getStringAttribute("givenname");
            String surname = dirContextOperations.getStringAttribute("sn");
            String commonName = dirContextOperations.getStringAttribute("cn");

            userDetails = userDetailsServiceImpl.createBasicUser(username, givenName, surname, commonName);
        }

        return userDetails;
    }

请确保您使用的是ActiveDirectoryLdapAuthenticationProvider春季安全性类,因为与其他LDAP服务器相比,Active Directory具有自己的细微差别。您可能需要在安全配置类中使用@EnableGlobalAuthentication批注,因为您可能有多个AuthenticationManagerBuilder,这使事情非常混乱。

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        ActiveDirectoryLdapAuthenticationProvider adProvider =
                new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://primarydc.domain.com:389");
        adProvider.setConvertSubErrorCodesToExceptions(true);
        adProvider.setUseAuthenticationRequestCredentials(true);
        auth.authenticationProvider(adProvider);
}

此处有更多详细信息: https://github.com/spring-projects/spring-security/issues/4324 https://github.com/spring-projects/spring-security/issues/4571