简体   繁体   中英

Play! Framework Authentication with LDAP

I am writing a webApp with Play2 for Java and want to use LDAP for user authentication... Im new to LDAP and actually don't know exactly how it works and how to use it in Play...

for now I've found this plugin that should probably do the trick, but I cannot find any example of it that uses LDAP authentication. do you know any tutorial that might help me take the first steps?

I also came across this blog post which is looking good, but does not use play authentication plugins, so it might not be that flexible? http://www.philipp.haussleiter.de/2013/07/adding-ldap-authentication-to-a-play-2-application/

I have an example to authenticate user using LDAP and the play framework. Here is the code hope this will help

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 a controller I use the above code as following:

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"));

}

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