Authenticating using LDAP with spring LDAP API and without using spring security

陌路散爱 提交于 2021-01-21 06:13:49

问题


I am using spring-ldap-core plugin in my Sprint boot application. Basically, the LDAPTemplate - http://docs.spring.io/spring-ldap/docs/current/apidocs/org/springframework/ldap/core/LdapTemplate.html

I basically want to convert the xml configuration below into java using Spring LDAP API and want to avoid using spring security.

The xml configuration that I want to convert is -

 <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>

Here is my java code below-

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;
    }
}

This is how I am trying to invoke authentication - The method that this snippet is a part of returns a boolean value if authentication happens

 AndFilter filter = new AndFilter();

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

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

This is not working and the error I get is that :

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

I want to know how the following xml properties can be configured using LDAP API?

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

Also, what else am I missing? Why is this not working? Any help will be really appreciated!


回答1:


I was able to figure this out.

//LDAP connection using LDAPTemplate

@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;
    }
}

//Authentication portion

AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("mailNickname", username));

Boolean authenticate = ldapTemplate.authenticate(base, filter.encode(), userpassword);


来源:https://stackoverflow.com/questions/40180421/authenticating-using-ldap-with-spring-ldap-api-and-without-using-spring-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!