spring-guides / gs-authenticating-ldap

Authenticating a User with LDAP :: Learn how to secure an application with LDAP.

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Embedded ldap connention refused

jainh opened this issue · comments

Hi
Do i need any additional configuration to setup embedded ldap, I have following dependency in my pom.xml

<dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>
        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

Also in application properties file

spring.ldap.embedded.ldif=classpath:test-data.ldif
spring.ldap.embedded.base-dn=ou=users,dc=cluster,dc=local
spring.ldap.embedded.port=8389
spring.ldap.embedded.url=ldap://localhost:8389/

But while running test i am getting connection refused.

org.springframework.ldap.CommunicationException: localhost:8389; nested exception is javax.naming.CommunicationException: localhost:8389 [Root exception is java.net.ConnectException: Connection refused]

I noticed in the guide that the @EnableWebSecurity annotation was missing from WebSecurityConfig.java. Maybe this is your issue.

@djnoddyp I added the annotation, still doesnt work.

I am having the same issue.

@MadKeys I resolved it by adding all my security configurations in my configuration class.. I don't why this happens.. below is the configuration I have for the ldap auth..
`
@configuration
@Profile("DEV_STANDALONE_H2_TEST_LDAP")
public class TestApplicationSecurityConfigurationHolder {
private static final Logger LOG = LoggerFactory.getLogger(TestApplicationSecurityConfigurationHolder.class);

@EnableGlobalAuthentication
@Profile("DEV_STANDALONE_H2_TEST_LDAP")
public static class AuthConfigure {

    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        LOG.info("#################");
        LOG.info("TEST");
        LOG.info("configuring ldap authenticaton");
        LOG.info("#################");

        auth
                .ldapAuthentication()
                .userDetailsContextMapper(ldapUserContextMapper())
                .contextSource(contextSource())
                .userDnPatterns("cn={0},ou=people")
                .groupSearchBase("ou=groups")
                .passwordCompare()
                .passwordEncoder(new PlaintextPasswordEncoder())
                .passwordAttribute("userPassword");
    }

    @Bean
    public DefaultSpringSecurityContextSource contextSource() {
        LOG.info("configuring embedded LDAP context source");
        DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
                Arrays.asList("ldap://localhost:8389/"), "dc=springframework,dc=org");
        contextSource.afterPropertiesSet();
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate(){
        return new LdapTemplate(contextSource());
    }

    @Bean
    public UserDetailsContextMapper ldapUserContextMapper() {
        return new AccountLdapContextMapper();
    }
}

}`
The UserDetailsContextMapper is to map the ldap user and then give him permission or do anything with him after he logs in.. (I do not think it 's important, I just had to explain it)

Same problem here, any solutions?

I was able to get mine to work by adding the ComponentScan annotation and pointing it to the package that contains my WebConfig file.

@SpringBootApplication

@ComponentScan("com.xxx")

public class StoreManager extends SpringBootServletInitializer { ...

Worked for me after adding
compile("org.springframework:spring-tx") compile("com.unboundid:unboundid-ldapsdk")
to the build.gradle file

Works for me with additional changes as below:

application.properties
spring.ldap.embedded.base-dn=dc=springframework,dc=org spring.ldap.embedded.ldif=classpath:test-server.ldif spring.ldap.embedded.port=8389 spring.ldap.embedded.url=ldap://localhost:8389/

It only seems to work when I use (1):
spring.ldap.embedded.ldif=classpath:test-server.ldif

If I use anything else, such as (2):
spring.ldap.embedded.ldif=src/main/resources/test-server.ldif

much less an external directory such as (3):
spring.ldap.embedded.ldif=/usr/local/clo/app/authserver/test-server.ldif

or even just the default project directory (4):
spring.ldap.embedded.ldif=test-server.ldif

None of these other options work - only option 1.

Works for me with additional changes as below:

application.properties
spring.ldap.embedded.base-dn=dc=springframework,dc=org spring.ldap.embedded.ldif=classpath:test-server.ldif spring.ldap.embedded.port=8389 spring.ldap.embedded.url=ldap://localhost:8389/

@michaellow thanks for sharing, worked for me as well with this configuration

It all works for me out of the box. All the changes suggested are already in the complete sample. Maybe if you have an issue check your maven/gradle caches for bad jars.

@dav0 if you want to use a file location for a resource you have to use the file: prefix (default is classpath).

worked for me - out of the box as the changes in the complete example.
However in my initial project i was initially getting this error - i found that the resources folder did not have the two files - application.properties and test-server.ldif
when i copied the two files in the initial - it worked

It only seems to work when I use (1):
spring.ldap.embedded.ldif=classpath:test-server.ldif

If I use anything else, such as (2):
spring.ldap.embedded.ldif=src/main/resources/test-server.ldif

much less an external directory such as (3):
spring.ldap.embedded.ldif=/usr/local/clo/app/authserver/test-server.ldif

or even just the default project directory (4):
spring.ldap.embedded.ldif=test-server.ldif

None of these other options work - only option 1.

Try this:
spring.ldap.embedded.ldif=file:///usr/local/clo/app/authserver/test-server.ldif

add in application.properties

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

This does not work if you have different version of spring dependencies.
just do not mess up with dependencies demo app works as is.

它似乎只在我使用(1)时起作用: spring.ldap.embedded.ldif=classpath:test-server.ldif

如果我使用其他任何东西,例如(2): spring.ldap.embedded.ldif=src/main/resources/test-server.ldif

更不用说外部目录,例如 (3): spring.ldap.embedded.ldif=/usr/local/clo/app/authserver/test-server.ldif

甚至只是默认项目目录 (4): spring.ldap.embedded.ldif=test-server.ldif

这些其他选项都不起作用 - 只有选项 1。

have you solve the question ? I have the same question like you

I want to konwn have you final solve the question? And how do you do it ?

Assuming you are following this guide (https://spring.io/guides/gs/authenticating-ldap/) and made your project with spring initializer, please check the dependency unboundid-ldapsdk. If there is scope Test remove it.