Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Security LDAP Changes After 5.7 - Updating To Match Spring Docs #12574

Closed
mjrother opened this issue Jan 23, 2023 · 1 comment
Closed
Assignees
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@mjrother
Copy link

mjrother commented Jan 23, 2023

So I am starting with the spring guide on authenticating ldap (https://github.com/spring-guides/gs-authenticating-ldap) I took the "complete" example and the app starts and validates fine. I tried updating the example according to this website #10138.
From :

@Autowired
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 BCryptPasswordEncoder())
                .passwordAttribute("userPassword");

To :

@bean
public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
contextSourceFactoryBean.setPort(0);
return contextSourceFactoryBean;
}

@bean
public AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
contextSource, new BCryptPasswordEncoder());
factory.setUserDnPatterns("uid={0},ou=people");
factory.setPasswordAttribute("pwd");
return factory.createAuthenticationManager();
}

Thinking this closer to the recommendations and closer to what I would be using in our real project. Now when I try to build ... the test phase keeps failing with the following stack trace

ERROR[m] loginWithValidUserThenAuthenticated  Time elapsed: 0 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticationManager' defined in class path resource [com/example/authenticatingldap/WebSecurityConfig.class]: Unsatisfied dependency expressed through method 'authenticationManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contextSourceFactoryBean': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Unable to load LDIF classpath*:*.ldif
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contextSourceFactoryBean': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Unable to load LDIF classpath*:*.ldif
Caused by: java.lang.IllegalStateException: Unable to load LDIF classpath*:*.ldif
Caused by: com.unboundid.ldap.sdk.LDAPException: An entry with DN 'dc=springframework,dc=org' already exists in the server.

Also opened issue with guide github..spring-guides/gs-authenticating-ldap#46.

To Reproduce
download the complete example, make changes and build

Expected behavior
able to update code to match spring security recommendations and run.

Sample
https://github.com/spring-guides/gs-authenticating-ldap

@mjrother mjrother added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Jan 23, 2023
@sjohnr
Copy link
Member

sjohnr commented Jan 24, 2023

Thanks for the report, @mjrother!

I saw that you also asked this question on SO, but I didn't yet see a reply to my comment. I also noticed that @Buzzardo seems to be working on that guide very recently.

Given that this appears to be an issue with the (3rd party) embedded LDAP library and you have an issue opened on the guide issue tracker and a stackoverflow question, I'm going to close this for now.

If it turns out this is a Spring Security issue, we can re-open this.

@sjohnr sjohnr closed this as completed Jan 24, 2023
@sjohnr sjohnr added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Jan 24, 2023
@sjohnr sjohnr self-assigned this Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

2 participants