【问题标题】:Authenticating using LDAP with spring LDAP API and without using spring security使用带有 spring LDAP API 的 LDAP 进行身份验证,而不使用 spring 安全性
【发布时间】:2016-10-22 13:25:08
【问题描述】:

我在我的 Sprint 启动应用程序中使用 spring-ldap-core 插件。 基本上,LDAPTemplate - http://docs.spring.io/spring-ldap/docs/current/apidocs/org/springframework/ldap/core/LdapTemplate.html

我基本上想使用 Spring LDAP API 将下面的 xml 配置转换为 java,并希望避免使用 spring security。

我要转换的xml配置是-

 <ldap-server id="ldapServer"
                 url="ldap://ad.company.com:389"
                 manager-dn="CN=serviceaccount,OU=Service Accounts,DC=ad,DC=company,DC=com"
                 manager-password="password"/>

    <authentication-manager>
        <ldap-authentication-provider
                server-ref="ldapServer"
                user-search-base="dc=ad,dc=company,dc=com"
                user-search-filter="sAMAccountName={0}"
                group-search-filter="member={0}"
                group-search-base="ou=Groups,dc=ad,dc=company,dc=com"
                group-role-attribute="cn"/>
    </authentication-manager>

下面是我的java代码-

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.authentication.DefaultValuesAuthenticationSourceDecorator;

@Configuration
public class LdapConfiguration {

    @Bean
    public LdapContextSource contextSource(){
        LdapContextSource contextSource = new LdapContextSource();

        contextSource.setUrl("ldap://ad.company.com:389");
        contextSource.setBase("DC=ad,DC=company,DC=com");
        contextSource.setUserDn("CN=serviceaccount,OU=Service Accounts,DC=ad,DC=company,DC=com");
        contextSource.setPassword("password");
        contextSource.afterPropertiesSet();
        return contextSource;
    }


    @Bean
    public LdapTemplate ldapTemplate(){

        LdapTemplate template = new LdapTemplate(contextSource());
        try {
            template.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return template;
    }
}

这就是我尝试调用身份验证的方式 - 如果身份验证发生,此 sn-p 所属的方法将返回一个布尔值

 AndFilter filter = new AndFilter();

    filter.and(new EqualsFilter("sAMAccountName", userloginName));

    return ldapTemplate.authenticate("OU=Service Accounts", filter.encode(), userPassword);

这不起作用,我得到的错误是:

No results found for search, base: 'OU=Service Accounts'; filter: '(sAMAccountName=usernameIinput)'.

我想知道如何使用 LDAP API 配置以下 xml 属性?

group-search-filter="member={0}"
group-search-base="ou=Groups,dc=ad,dc=company,dc=com"
group-role-attribute="cn"/>

另外,我还缺少什么?为什么这不起作用? 任何帮助将不胜感激!

【问题讨论】:

    标签: java authentication spring-boot ldap spring-ldap


    【解决方案1】:

    我能够弄清楚这一点。

    //使用LDAPTemplate的LDAP连接

    @Configuration
    public class LdapConfiguration {
    
        @Bean
        public LdapContextSource contextSource(){
            LdapContextSource contextSource = new LdapContextSource();
            contextSource.setUrl("ldap://companyurl.com:389");
            contextSource.setUserDn("CN=serviceaccount,OU=Service Accounts,DC=ad,DC=company,DC=com");
            contextSource.setPassword("secretpassword");
            return contextSource;
        }
    
        @Bean
        public LdapTemplate ldapTemplate(){
            LdapTemplate template = new LdapTemplate(contextSource());
            return template;
        }
    }
    

    //认证部分

    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("mailNickname", username));
    
    Boolean authenticate = ldapTemplate.authenticate(base, filter.encode(), userpassword);
    

    【讨论】:

    • 我们可以在不设置 contextSource.setUserDN() 和 contextSource.setPassword() 的情况下执行 ldapTemplate.authenticate() 吗?
    • @TXT,contextSource中的userDN参数为必填参数。不传递此参数,将无法获取 ldap 连接。
    猜你喜欢
    • 2011-05-04
    • 2021-09-02
    • 2018-08-09
    • 1970-01-01
    • 2017-07-07
    • 2012-11-18
    • 2019-08-30
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多