Issue with Spring Boot Guide - Authenticating a User with LDAP

Viewed 6483

The Spring Boot Getting Started guide "Authenticating a User with LDAP" gives java.net.ConnectException: Connection refused

I have just followed step by step instructions in this getting started guide -

https://spring.io/guides/gs/authenticating-ldap/

Did development in STS

Same as in the example above by spring.io

Towards the end, the guide points out that there should be a clean login for username= bob and password=bobspassword

My application on the other had generates this error when I enter the same credentials in the login form-

localhost:8389; nested exception is javax.naming.CommunicationException: localhost:8389 [Root exception is java.net.ConnectException: Connection refused (Connection refused)]

4 Answers

The guide's complete didn't even work out of the box for me. After quite some experimentations, eventually below was how it worked out on my end:

(1) application.properties

spring.ldap.embedded.ldif=classpath:test-server.ldif
spring.ldap.embedded.base-dn=dc=springframework,dc=org
spring.ldap.embedded.port=8399 

ATTENTION 8399 here, not 8389. 8389 was listening on my Windows 10, and I verified that by doing netstat -an |find /i "389". But even with that working, the Spring Security login page kept complaining about Embedded ldap connection refused to port 8399. That's what inspired me to change the port number from 8389 to 8399. Note that I first added a new "Inbound Rule" in Windows Firewall for 8399. Follow this link for instructions about how to open or close a port https://docs.bitnami.com/installer/faq/windows-faq/administration/use-firewall-windows/ enter image description here

(2) Based off the sample code provided by the Spring Guides, Change/Remove the two lines as commented below:

  @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")//CHANGE 8389 to 8399
          .and()
        .passwordCompare()
          .passwordEncoder(new BCryptPasswordEncoder()) //REMOVE this line
          .passwordAttribute("userPassword");
  }
}

Removing .passwordEncoder(new BCryptPasswordEncoder()) cuts off complexities if you just began with this Spring Guides tutorial for authenticating with LDAP. If you don't like cutting the corners, you need to refer to some other great Stackoverflow posts for what it takes to make the passwordEncoder work. Right now, my solution is only scoped to make the easiest test like "bob" for uid and "bobspassword" for userPassword to work. Without removing BCrytPasswordEncoder() as shown above, you'll see a warning when testing with "bob" and "bobspassword": "Encoded passoword does not look like BCrypt". enter image description here

That was all I deviated from the guide and then I could log in with "bob" and "bobspassword" etc. as predefined in test-server.ldif.

The problem is that the guide at https://spring.io/guides/gs/authenticating-ldap/ does not mention how to set up your application.properties file.

SOLUTION: You need the set the below properties in your resources/application.properties file

spring.ldap.embedded.port=8389
spring.ldap.embedded.ldif=classpath:test-server.ldif
spring.ldap.embedded.base-dn=dc=springframework,dc=org

Copy the above code to your application.properties file, restart your Spring Application and it should work.

I discovered this thanks to tkhenghong's answer and the code he uploaded to his github.

One Issue we might face if we are using https://start.spring.io/ (Spring Initializer) to generate our project , check this dependency

Replace :

        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
            <scope>test</scope>
        </dependency>

With :

        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
        </dependency>

I also faced the same problem and I found out that I have configured some dependencies wrong in the build.gradle file.

Here are my dependencies(after the fix):

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.ldap:spring-ldap-core' // Essential
    implementation 'org.springframework.security:spring-security-ldap'
    implementation 'com.unboundid:unboundid-ldapsdk' // (not using testImplementation)
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
}

Here's the link to my own repositories that I learned from javabrains.io tutorial. https://github.com/tkhenghong/spring-security-ldap

Hope this helps.

Related