简体   繁体   中英

User authentication in LDAP java

i am new to the LDAP concept and i am able to connect to the LDAP server successfully. And also able to insert and delete records to/from LDAP database. Next thing i need to do is authenticate the user based on LDAP database records.. Help me out please. I will share the source code if needed. Thanks.

We have several samples which can help you if you need to use JNDI and LDAP.

We prefer to use a real LDAP SDK like the ones shown at https://www.ldap.com/developing-clients-apps (We use and reccomend UnboundID LDAP SDK for Java)

You should probably also consider using OpenID Connect as you can then abstract the Authentication to become agnostic to how authentication takes place.

I also used LDAP for auth. I recommand the token based auth . If you can obtain the JWT token by request. This repo java-jwt will be helpful.

If you don't mind to use Spring Security, check out this article. It will ease the implementation and configuration. Here is and example of LDAP configuration:

auth.authenticationProvider(ldapAuthenticationProvider)
    .ldapAuthentication()
    .ldapAuthoritiesPopulator(ldapAuthoritiesPopulator)
    .userSearchBase("ou=users")
    .userSearchFilter("(uid={0})")
    .groupSearchBase("ou=roles")
    .groupSearchFilter("(member={0})")
    .groupRoleAttribute("cn")
    .contextSource(contextSource);

Try to create an Object of InitialDirContext with security_principal(user dn) and security_credentials(password) of the user you want to authenticate. If you are able to create the object the user is authenticated else catch the exception and throw it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM