1

Spring seems to have some predefined ldap properties that are available in the application.properties file, such as

spring.ldap.urls=
spring.ldap.base=
spring.ldap.username
spring.ldap.password=

However, it seems that you still have to create a security configuration, where you have to define those properties in code, as Spring complains that it can´t find any AuthenticationProvider

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
                .userSearchBase("ou=people")
                .userSearchFilter("(uid={0})")
                .groupSearchBase("ou=groups")
                .groupSearchFilter("(member={0})")
                .contextSource().root("dc=baeldung,dc=com")
                .ldif("classpath:users.ldif");
    }

}

Source https://www.baeldung.com/spring-security-ldap

How can you use those properties without creating an extra configuration where you have to define them again?

1 Answer 1

1

Yes you need to redefine those properties because that Property is for creating DefaultSpringSecurityContextSource However, you can also read these properties from application.properties instead of rewriting

   @Value("${spring.ldap.urls}")
    private String ldapUrls;

    @Value("${spring.ldap.base}")
    private String ldapBase;

    @Value("${spring.ldap.userDnPatterns}")
    private String userDnPatterns;

    @Value("${spring.ldap.groupSearchBase:ou=groups}")
    private String groupSearchBase;


 @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.ldapAuthentication()
                    .userSearchBase(ldapBase)
                    .userSearchFilter("(uid={0})")
                    .groupSearchBase(groupSearchBase)    
                    .userDnPatterns(userDnPatterns)

        }

Besides these security Spring also create LdapTemplate Bean from these properties

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.