[英]LDAP server - mobile application
我正在为我的大学开发一个项目,该项目是用于移动心情的移动应用程序...我们正在使用电话间隙来完成它,我的问题是如何针对LDAP服务器进行身份验证检查。 通常,我们如何从LDAP检索信息。
Java中的默认JNDI类不是很难理解的。 基本上:
我当前项目中的一些示例代码。
步骤1和2:使用“ userdn”和“ password”创建经过身份验证的连接。
private LdapContext getLdapContext( String userdn, String password )
{
LdapContext ldapCtx = null;
Hashtable<String, String> env = new Hashtable<String, String>(5, 0.75F);
env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
if ( ldapPort.equals( "636" ) )
{
env.put( Context.SECURITY_PROTOCOL, "ssl");
env.put("java.naming.ldap.factory.socket", "portal.ldap.util.PortalSocketFactory");
}
env.put( Context.PROVIDER_URL, "ldap://"
+ ldapHost + ":"
+ ldapPort );
env.put( Context.SECURITY_AUTHENTICATION, "simple" );
env.put( Context.SECURITY_PRINCIPAL, userdn );
env.put( Context.SECURITY_CREDENTIALS, password );
try {
ldapCtx = new InitialLdapContext( env, null );
} catch ( NamingException e ) {
// Handle error
}
return ldapCtx;
}
步骤3:使用经过身份验证的连接从LDAP服务器读取对象。
private Attributes getAttrs()
{
LdapContext ctx = getLdapContext();
try
{
return ctx.getAttributes( "cn=your,ou=object,o=dn" , { "cn", "yourattribute" } );
}
catch (NamingException e)
{
// Handle error
}
catch (NullPointerException e)
{
// Handle error
}
finally
{
try {
ctx.close();
} catch (NamingException e) {}
}
}
通过属性可以访问cn = your,ou = object,o = dn对象的数据。 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.