从活动目录中检索数据

时间:2015-12-03 07:01:43

标签: java active-directory

我正在开发一个java和JSP项目,需要访问一个活动目录来登录并检索一些值。我已经完成了登录部分(身份验证)但我无法获得检索部分的帮助。我对活动目录概念完全不熟悉,所以请相应地帮助我。 我给出了用于用户身份验证的java类的代码:

 package classes;

    import java.util.Hashtable;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;

    public class AD_Auth {

    private static final String CONTEXT_FACTORY_CLASS ="com.sun.jndi.ldap.LdapCtxFactory";
    private static final String domainName = "@xyz.IN";
    private static final String providerUrl = "ldap://srdeladc02.xyz.in:123";

    public static boolean authenticate(String username, String password) {

        try {
            Hashtable<Object, Object> env = new Hashtable<Object, Object>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY_CLASS);
            env.put(Context.PROVIDER_URL, providerUrl);
            env.put(Context.SECURITY_PRINCIPAL, username + domainName);
            env.put(Context.SECURITY_CREDENTIALS, password);
                        env.put(Context.SECURITY_AUTHENTICATION, "strong");
            DirContext ctx = new InitialDirContext(env);
            ctx.close();

            return true;

        } catch (NamingException e) {
            System.out.println("Dear User, The username or password you have entered is in correct, please try again.");
        }

        return false;

    }

    public static void main(String[] args) {
        // if someone emailid is myemail#icar.gov.in then username will be myemail
        //System.out.println(AD_Auth.authenticate("username", "password"));

    }

}

0 个答案:

没有答案