LDAP authentication within Spring Boot












0














I'm trying to use spring-ldap stuff to protect my application.



This is my WebSecurityConfig file :



package mis.maskcenter;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.formLogin();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth

.ldapAuthentication()
.userDnPatterns("uid={0},ou=People")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://ip_here:389/dc=mycomp,dc=com")
.managerPassword("managuser")
.managerDn("uid=managuser");

}


}


At application startup, the login page replace my home page : good!



Then I try to provide BAD credentials : the page display a BAD CREDENTIAL message : good too :o) there is no message in the application terminal.



Then I try with CORRECT credentials : this time the login page don't display any message, just reload to empty login screen -sic- and in the terminal where I launch the application I have this message :



2018-11-16 14:54:54.217 ERROR 22623 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path  threw exception

org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]; nested exception is javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]


Even after reading ldapAuthentication documentation, my comprehension of the subject is quite limited, so any help would be appreciate :o)



Any suggestion ?
Thanks










share|improve this question






















  • Is your dn pattern complete? Should it be uid={0},ou=People,dc=mycomp,dc=com or something similar?
    – Ryan
    Nov 16 '18 at 15:35










  • I changed to .userDnPatterns("uid={0},ou=People,dc=mycomp,dc=com") now I have a bad credential message either with correct or not login+passwd but no more problem in the terminal
    – alak
    Nov 16 '18 at 16:06










  • I can complete with this piece of code from another application which also connect to LDAP but without Spring stuff ` environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://ip_here:389"); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, "cn=managuser,dc=mycomp,dc=com"); environment.put(Context.SECURITY_CREDENTIALS, "managuser"); `
    – alak
    Nov 16 '18 at 16:14










  • I don't think your manager dn is complete. Do you have the full dn?
    – Ryan
    Nov 16 '18 at 16:31










  • managerDn should match userDnPatterns so with {0} as a placeholder in 'uid={0},ou=People' either use managerDn('managuser') or remove userDnPatterns and pass the full user dn, for example : managerDn("uid=managuser,ou=people,dc=example,dc=com")
    – EricLavault
    Nov 16 '18 at 17:08


















0














I'm trying to use spring-ldap stuff to protect my application.



This is my WebSecurityConfig file :



package mis.maskcenter;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.formLogin();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth

.ldapAuthentication()
.userDnPatterns("uid={0},ou=People")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://ip_here:389/dc=mycomp,dc=com")
.managerPassword("managuser")
.managerDn("uid=managuser");

}


}


At application startup, the login page replace my home page : good!



Then I try to provide BAD credentials : the page display a BAD CREDENTIAL message : good too :o) there is no message in the application terminal.



Then I try with CORRECT credentials : this time the login page don't display any message, just reload to empty login screen -sic- and in the terminal where I launch the application I have this message :



2018-11-16 14:54:54.217 ERROR 22623 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path  threw exception

org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]; nested exception is javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]


Even after reading ldapAuthentication documentation, my comprehension of the subject is quite limited, so any help would be appreciate :o)



Any suggestion ?
Thanks










share|improve this question






















  • Is your dn pattern complete? Should it be uid={0},ou=People,dc=mycomp,dc=com or something similar?
    – Ryan
    Nov 16 '18 at 15:35










  • I changed to .userDnPatterns("uid={0},ou=People,dc=mycomp,dc=com") now I have a bad credential message either with correct or not login+passwd but no more problem in the terminal
    – alak
    Nov 16 '18 at 16:06










  • I can complete with this piece of code from another application which also connect to LDAP but without Spring stuff ` environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://ip_here:389"); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, "cn=managuser,dc=mycomp,dc=com"); environment.put(Context.SECURITY_CREDENTIALS, "managuser"); `
    – alak
    Nov 16 '18 at 16:14










  • I don't think your manager dn is complete. Do you have the full dn?
    – Ryan
    Nov 16 '18 at 16:31










  • managerDn should match userDnPatterns so with {0} as a placeholder in 'uid={0},ou=People' either use managerDn('managuser') or remove userDnPatterns and pass the full user dn, for example : managerDn("uid=managuser,ou=people,dc=example,dc=com")
    – EricLavault
    Nov 16 '18 at 17:08
















0












0








0







I'm trying to use spring-ldap stuff to protect my application.



This is my WebSecurityConfig file :



package mis.maskcenter;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.formLogin();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth

.ldapAuthentication()
.userDnPatterns("uid={0},ou=People")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://ip_here:389/dc=mycomp,dc=com")
.managerPassword("managuser")
.managerDn("uid=managuser");

}


}


At application startup, the login page replace my home page : good!



Then I try to provide BAD credentials : the page display a BAD CREDENTIAL message : good too :o) there is no message in the application terminal.



Then I try with CORRECT credentials : this time the login page don't display any message, just reload to empty login screen -sic- and in the terminal where I launch the application I have this message :



2018-11-16 14:54:54.217 ERROR 22623 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path  threw exception

org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]; nested exception is javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]


Even after reading ldapAuthentication documentation, my comprehension of the subject is quite limited, so any help would be appreciate :o)



Any suggestion ?
Thanks










share|improve this question













I'm trying to use spring-ldap stuff to protect my application.



This is my WebSecurityConfig file :



package mis.maskcenter;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.formLogin();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth

.ldapAuthentication()
.userDnPatterns("uid={0},ou=People")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://ip_here:389/dc=mycomp,dc=com")
.managerPassword("managuser")
.managerDn("uid=managuser");

}


}


At application startup, the login page replace my home page : good!



Then I try to provide BAD credentials : the page display a BAD CREDENTIAL message : good too :o) there is no message in the application terminal.



Then I try with CORRECT credentials : this time the login page don't display any message, just reload to empty login screen -sic- and in the terminal where I launch the application I have this message :



2018-11-16 14:54:54.217 ERROR 22623 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path  threw exception

org.springframework.ldap.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]; nested exception is javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]


Even after reading ldapAuthentication documentation, my comprehension of the subject is quite limited, so any help would be appreciate :o)



Any suggestion ?
Thanks







spring-boot ldap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 14:08









alak

247




247












  • Is your dn pattern complete? Should it be uid={0},ou=People,dc=mycomp,dc=com or something similar?
    – Ryan
    Nov 16 '18 at 15:35










  • I changed to .userDnPatterns("uid={0},ou=People,dc=mycomp,dc=com") now I have a bad credential message either with correct or not login+passwd but no more problem in the terminal
    – alak
    Nov 16 '18 at 16:06










  • I can complete with this piece of code from another application which also connect to LDAP but without Spring stuff ` environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://ip_here:389"); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, "cn=managuser,dc=mycomp,dc=com"); environment.put(Context.SECURITY_CREDENTIALS, "managuser"); `
    – alak
    Nov 16 '18 at 16:14










  • I don't think your manager dn is complete. Do you have the full dn?
    – Ryan
    Nov 16 '18 at 16:31










  • managerDn should match userDnPatterns so with {0} as a placeholder in 'uid={0},ou=People' either use managerDn('managuser') or remove userDnPatterns and pass the full user dn, for example : managerDn("uid=managuser,ou=people,dc=example,dc=com")
    – EricLavault
    Nov 16 '18 at 17:08




















  • Is your dn pattern complete? Should it be uid={0},ou=People,dc=mycomp,dc=com or something similar?
    – Ryan
    Nov 16 '18 at 15:35










  • I changed to .userDnPatterns("uid={0},ou=People,dc=mycomp,dc=com") now I have a bad credential message either with correct or not login+passwd but no more problem in the terminal
    – alak
    Nov 16 '18 at 16:06










  • I can complete with this piece of code from another application which also connect to LDAP but without Spring stuff ` environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://ip_here:389"); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, "cn=managuser,dc=mycomp,dc=com"); environment.put(Context.SECURITY_CREDENTIALS, "managuser"); `
    – alak
    Nov 16 '18 at 16:14










  • I don't think your manager dn is complete. Do you have the full dn?
    – Ryan
    Nov 16 '18 at 16:31










  • managerDn should match userDnPatterns so with {0} as a placeholder in 'uid={0},ou=People' either use managerDn('managuser') or remove userDnPatterns and pass the full user dn, for example : managerDn("uid=managuser,ou=people,dc=example,dc=com")
    – EricLavault
    Nov 16 '18 at 17:08


















Is your dn pattern complete? Should it be uid={0},ou=People,dc=mycomp,dc=com or something similar?
– Ryan
Nov 16 '18 at 15:35




Is your dn pattern complete? Should it be uid={0},ou=People,dc=mycomp,dc=com or something similar?
– Ryan
Nov 16 '18 at 15:35












I changed to .userDnPatterns("uid={0},ou=People,dc=mycomp,dc=com") now I have a bad credential message either with correct or not login+passwd but no more problem in the terminal
– alak
Nov 16 '18 at 16:06




I changed to .userDnPatterns("uid={0},ou=People,dc=mycomp,dc=com") now I have a bad credential message either with correct or not login+passwd but no more problem in the terminal
– alak
Nov 16 '18 at 16:06












I can complete with this piece of code from another application which also connect to LDAP but without Spring stuff ` environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://ip_here:389"); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, "cn=managuser,dc=mycomp,dc=com"); environment.put(Context.SECURITY_CREDENTIALS, "managuser"); `
– alak
Nov 16 '18 at 16:14




I can complete with this piece of code from another application which also connect to LDAP but without Spring stuff ` environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://ip_here:389"); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, "cn=managuser,dc=mycomp,dc=com"); environment.put(Context.SECURITY_CREDENTIALS, "managuser"); `
– alak
Nov 16 '18 at 16:14












I don't think your manager dn is complete. Do you have the full dn?
– Ryan
Nov 16 '18 at 16:31




I don't think your manager dn is complete. Do you have the full dn?
– Ryan
Nov 16 '18 at 16:31












managerDn should match userDnPatterns so with {0} as a placeholder in 'uid={0},ou=People' either use managerDn('managuser') or remove userDnPatterns and pass the full user dn, for example : managerDn("uid=managuser,ou=people,dc=example,dc=com")
– EricLavault
Nov 16 '18 at 17:08






managerDn should match userDnPatterns so with {0} as a placeholder in 'uid={0},ou=People' either use managerDn('managuser') or remove userDnPatterns and pass the full user dn, for example : managerDn("uid=managuser,ou=people,dc=example,dc=com")
– EricLavault
Nov 16 '18 at 17:08














1 Answer
1






active

oldest

votes


















0














I finally get it work by encapsulating the piece of java code that was working into a custom authentication provider.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339465%2fldap-authentication-within-spring-boot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I finally get it work by encapsulating the piece of java code that was working into a custom authentication provider.






    share|improve this answer




























      0














      I finally get it work by encapsulating the piece of java code that was working into a custom authentication provider.






      share|improve this answer


























        0












        0








        0






        I finally get it work by encapsulating the piece of java code that was working into a custom authentication provider.






        share|improve this answer














        I finally get it work by encapsulating the piece of java code that was working into a custom authentication provider.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 11 '18 at 14:26

























        answered Nov 19 '18 at 12:59









        alak

        247




        247






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339465%2fldap-authentication-within-spring-boot%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]