Play! Platform Identification Using LDAP

I am writing webApp with Play2 for Java and want to use LDAP to authenticate users ... Im new for LDAP and really don't know exactly how it works and how to use it in Play ...

at the moment, I have found this plugin that should probably do the trick, but I cannot find a single example that uses LDAP authentication. Do you know any tutorial that can help me take the first steps?

I also stumbled upon this blog post that looks good but doesn't use plugins for authentication, so it might not be that flexible? http://www.philipp.haussleiter.de/2013/07/adding-ldap-authentication-to-a-play-2-application/

+6
source share
1 answer

I have an example for user authentication using LDAP and a playback platform. Here is the code, hope this helps

public class ActiveDirectoryServices { public static final String ldapURL = Play.application().configuration().getString("ActiveDirectory.url"); public static final String domainName = Play.application().configuration().getString("ActoveDirectory.DomainName"); public static final int timeout = Play.application().configuration().getInt("ActoveDirectory.timeout"); public static Promise<Boolean> authenticate(String username, String password) throws AuthenticationException, CommunicationException, NamingException{ Hashtable<String, String> env = new Hashtable<String,String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put("com.sun.jndi.ldap.connect.timeout", ""+(timeout*1000)); env.put(Context.PROVIDER_URL, ldapURL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, username+domainName); env.put(Context.SECURITY_CREDENTIALS, password); DirContext authContext = null; authContext = new InitialDirContext(env); return Promise.pure(Boolean.TRUE); } } 

Then in the controller, I use the above code as follows:

 try { Promise<Boolean> promiseActiveDirectoryCheck = ActiveDirectoryServices.authenticate(userName, password); return promiseActiveDirectoryCheck.flatMap(response -> { if(response){ return Promise.pure(ok("access granted")); } }); }catch (AuthenticationException exp) { return Promise.pure(ok("access denied")); }catch (CommunicationException exp) { return Promise.pure(ok("The active directory server is not reachable")); }catch (NamingException exp) { return Promise.pure(ok("active directory domain name does not exist")); } 
+4
source

Source: https://habr.com/ru/post/982620/


All Articles