java项目实现ldap认证

java项目实现ldap认证

public static boolean ldapCheck(String userName, String password) {
        String url = "LDAP://ldap的地址:389";

        String user = "uid="+userName+",cn=employees,ou=its,o=dc";

        Hashtable<String, Object> env = new Hashtable<>();
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, user);
        env.put(Context.SECURITY_CREDENTIALS, password);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, url);
        DirContext ctx = null;
        try {
            System.out.println("ldap认证成功...");
            ctx = new InitialDirContext(env);
            return Boolean.TRUE;
        } catch (Exception err) {
            System.out.println("ldap认证失败...");
            return Boolean.FALSE;
        } finally {
            try {
                if (ctx != null) {
                    ctx.close();
                }
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
    }

你可能感兴趣的:(java,ldap认证,java登录ldap认证)