⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usersldaprepository.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        .append("@")                        .append(usersDomain);            String filter = filterBuffer.toString();            NamingEnumeration enum  = rootCtx.search("", filter, ctls);            if (enum.hasMore()) { // ie User is in Directory                SearchResult newSr = (SearchResult)enum.next();                String userDN = newSr.getName();                Attribute servers = rootCtx.getAttributes(userDN, returnAttrs).get(groupAttr);                if (servers != null && servers.contains(baseNodeDN)) {//server already registered for user                    getLogger().info(baseNodeDN + " already in user's Groups. " );                    //System.out.println(baseNodeDN + " already in user's Groups. ");                } else {                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, authType);                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_PRINCIPAL, principal);                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_CREDENTIALS, password);                    rootCtx.modifyAttributes(userDN, DirContext.ADD_ATTRIBUTE, new BasicAttributes(groupAttr, baseNodeDN, true));                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, "none");                    getLogger().info(baseNodeDN + " added to user's groups ");                    //System.out.println(baseNodeDN + " added to users' groups ");                }            } else {                StringBuffer infoBuffer =                    new StringBuffer(64)                            .append("User ")                            .append(userName)                            .append(" not in directory.");                getLogger().info(infoBuffer.toString());                // System.out.println(infoBuffer.toString());            }        } catch (NamingException e) {            getLogger().error("Problem adding group to user " + userName);            //System.out.println("Problem adding group to user " + userName);            //System.out.println(e.getMessage());            //e.printStackTrace();        } finally {            closeDirContext(rootCtx);        }    }    public synchronized Object getAttributes(String name) {        return null;    }    public synchronized void removeUser(String userName) {        String[] attrIDs = {membersAttr};        try {            Attribute members = ctx.getAttributes("", attrIDs).get(membersAttr);            if (members == null) {                System.out.println("UsersLDAPRepository - Null list attribute.");            } else  if (!members.contains(userName)) {//user not here                getLogger().info(userName + " missing from mailGroup. ");                //System.out.println(userName + " missing from mailGroup. ");            } else {                // First, remove username from mailGroup at baseNode                ctx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, authType);                ctx.addToEnvironment(javax.naming.Context.SECURITY_PRINCIPAL, principal);                ctx.addToEnvironment(javax.naming.Context.SECURITY_CREDENTIALS, password);                ModificationItem[] mods = new ModificationItem[1];                mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute(membersAttr, userName));                ctx.modifyAttributes("", mods);                ctx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, "none");                getLogger().info(userName + " removed from mailGroup. ");                //System.out.println(userName + " removed from mailGroup. ");            }        } catch (NamingException e) {            StringBuffer exceptionBuffer =                new StringBuffer(256)                        .append("Problem removing user ")                        .append(userName)                        .append(": ")                        .append(e);            getLogger().error(exceptionBuffer.toString());            //System.out.println("Problem removing user " + userName);            //System.out.println(e.getMessage());            //e.printStackTrace();        }        if (manageGroupAttr) {            removeGroupFromUser(userName);        }        if (managePasswordAttr) {            // not yet implemented        }    }    public void removeGroupFromUser(String userName) {        Hashtable env = new Hashtable();        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");        env.put(javax.naming.Context.PROVIDER_URL, rootURL);        DirContext rootCtx = null;        try {            rootCtx = new InitialDirContext(env);            // Find directory entry            String[] returnAttrs = {groupAttr};            SearchControls ctls = new SearchControls();            ctls.setReturningAttributes(returnAttrs);            ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);            StringBuffer filterBuffer =                new StringBuffer(128)                        .append(mailAddressAttr)                        .append("=")                        .append(userName)                        .append("@")                        .append(usersDomain);            String filter = filterBuffer.toString();            NamingEnumeration enum  = rootCtx.search("", filter, ctls);            if (enum.hasMore()) { // ie User is in Directory                SearchResult newSr = (SearchResult)enum.next();                String userDN = newSr.getName();                System.out.println("Found user entry: " + userDN);                Attribute servers = rootCtx.getAttributes(userDN, returnAttrs).get(groupAttr);                if (servers == null) { //should not happen                    getLogger().info("GroupAttribute missing from user: " + userName);                    // System.out.println("GroupAttribute missing from user: " + userName );                } else if (!servers.contains(baseNodeDN)) {//server not registered for user                    getLogger().info(baseNodeDN + " missing from users' Groups. " );                    //System.out.println(baseNodeDN + " missing from users' Groups. ");                } else {                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, authType);                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_PRINCIPAL, principal);                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_CREDENTIALS, password);                    ModificationItem[] mods = new ModificationItem[1];                    mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute(groupAttr, baseNodeDN));                    rootCtx.modifyAttributes(userDN, mods);                    //rootCtx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, changes);                    rootCtx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, "none");                    getLogger().info(baseNodeDN + " removed from users' groups " );                    //System.out.println(baseNodeDN + " removed from users' groups ");                }            } else {                StringBuffer infoBuffer =                    new StringBuffer(64)                            .append("User ")                            .append(userName)                            .append(" not in directory.");                getLogger().info(infoBuffer.toString());                //System.out.println(infoBuffer.toString());            }        } catch (NamingException e) {            StringBuffer exceptionBuffer =                new StringBuffer(256)                        .append("Problem removing user ")                        .append(userName)                        .append(e);            getLogger().error(exceptionBuffer.toString());            //System.out.println("Problem removing user " + userName);            //System.out.println(e.getMessage());            //e.printStackTrace();        } finally {            closeDirContext(rootCtx);            rootCtx = null;        }    }    public boolean contains(String name) {        boolean found = false;        String[] attrIDs = {membersAttr};        try {            Attribute members = ctx.getAttributes("", attrIDs).get(membersAttr);            if (members != null && members.contains(name)) {                found = true;                StringBuffer infoBuffer =                    new StringBuffer(64)                            .append("Found ")                            .append(name)                            .append(" in mailGroup. ");                getLogger().info(infoBuffer.toString());                //System.out.println(infoBuffer.toString());            }        } catch (NamingException e) {            StringBuffer exceptionBuffer =                new StringBuffer(256)                        .append("Problem finding user ")                        .append(name)                        .append(" : ")                        .append(e);            getLogger().error(exceptionBuffer.toString());            //System.out.println(exceptionBuffer.toString());        }        return found;    }    public boolean test(String name, Object attributes) {        boolean result = false;        boolean foundFlag = false;        String testPassword = (String) attributes;        String userDN = null;        try {            String[] returnAttrs = {identAttr, passwordAttr};            SearchControls ctls = new SearchControls();            ctls.setReturningAttributes(returnAttrs);            ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);            StringBuffer filterBuffer =                 new StringBuffer(128)                        .append(mailAddressAttr)                        .append("=")                        .append(name)                        .append("@")                        .append(usersDomain);            String filter = filterBuffer.toString();            Hashtable env = new Hashtable();            env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");            env.put(javax.naming.Context.PROVIDER_URL, rootURL);            DirContext rootCtx = null;            try {                rootCtx = new InitialDirContext(env);                    NamingEnumeration enum  = rootCtx.search("", filter, ctls);                if (enum.hasMore()) { // ie User is in Directory                    SearchResult sr = (SearchResult)enum.next();                    String userRDN = sr.getName();                    StringBuffer userDNBuffer =                        new StringBuffer(128)                                .append(userRDN)                                .append(", ")                                .append(rootNodeDN);                    userDN = userDNBuffer.toString();                    foundFlag = true;                    //System.out.println("UserDN is : " + userDN);                }            } finally {                closeDirContext(rootCtx);            }        } catch (Exception e) {            StringBuffer exceptionBuffer =                new StringBuffer(256)                        .append("Problem finding user ")                        .append(name)                        .append(" for password test.")                        .append(e);             getLogger().error(exceptionBuffer.toString());            //e.getMessage();            //e.printStackTrace();        }        if (foundFlag) { // ie User is in Directory            Hashtable env2 = new Hashtable();            env2.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");            env2.put(javax.naming.Context.PROVIDER_URL, rootURL);            env2.put(javax.naming.Context.SECURITY_AUTHENTICATION, "simple");            env2.put(javax.naming.Context.SECURITY_PRINCIPAL, userDN);            env2.put(javax.naming.Context.SECURITY_CREDENTIALS, testPassword);            //System.out.println("Creating initial context from " + baseURL);            DirContext testCtx = null;            try {                testCtx = new InitialDirContext(env2);                result = true;            } catch (AuthenticationException ae) {                result = false;                StringBuffer exceptionBuffer =                    new StringBuffer(256)                            .append("Attempt to authenticate with incorrect password for ")                            .append(name)                            .append(" : ")                            .append(ae);                 getLogger().error(exceptionBuffer.toString());                //System.out.println(exceptionBuffer.toString());                //System.out.println(ae.getMessage());                //ae.printStackTrace();            } catch (Exception e) {                StringBuffer exceptionBuffer =                    new StringBuffer(256)                            .append("Problem checking password for ")                            .append(name)                            .append(" : ")                            .append(e);                 getLogger().error(exceptionBuffer.toString());                //System.out.println(exceptionBuffer.toString());                //System.out.println(e.getMessage());                //e.printStackTrace();            } finally {                closeDirContext(testCtx);            }        }        return result;    }    public int countUsers() {        String[] attrIDs = {membersAttr};        int result = -1;        try {            Attribute members = ctx.getAttributes("", attrIDs).get(membersAttr);            if (members != null) {                result = members.size();            } else {                result = 0;            }        } catch (NamingException e) {            getLogger().error("Problem counting users: "  + e);            //System.out.println("Problem counting users. ");        }        return result;    }    public String getDomains() {        return usersDomain;    }    /**     * Disposes of all open directory contexts     *     * @throws Exception if an error is encountered during shutdown     */    public void dispose() throws Exception {        closeDirContext(ctx);        ctx = null;    }    private void closeDirContext(DirContext ctx) {        try {            if (ctx != null) {                ctx.close();            }        } catch (NamingException ne) {            getLogger().warn("UsersLDAPRepository: Unexpected exception encountered while closing directory context: " + ne);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -