简体   繁体   English

玩!使用LDAP进行框架验证

[英]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... 我正在使用Play2 for Java编写webApp,并希望使用LDAP进行用户身份验证...我是LDAP的新手,实际上并不知道它是如何工作的以及如何在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. 现在我发现这个插件应该可以解决这个问题,但我找不到任何使用LDAP身份验证的例子。 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/ 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. 我有一个使用LDAP和play框架验证用户的示例。 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"));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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