用JAVA通过JNDI操作Domino中LDAP
2009-05-31 08:31
337 查看
由于现在好多企业都在使用Domino系统。这里我就使用JAVA语言直接调用Domino中用户信息,进行常见的认证,增加,
修改和删除操作。
一、获取Domino连接
二、认证用户信息
三、添加用户信息
或者:
四、修改用户信息
五、删除用户信息
修改和删除操作。
一、获取Domino连接
/** * 从连接池中获取一个连接. * * @return LdapContext * @throws NamingException */ public LdapContext getConnectionFromFool() throws NamingException { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://192.168.0.189:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "CN=administrator,O=fall"); env.put(Context.SECURITY_CREDENTIALS, "123456"); env.put("com.sun.jndi.ldap.connect.pool", "true"); env.put("java.naming.referral", "follow"); return new InitialLdapContext(env, null); }
二、认证用户信息
/** * 校验用户登录. * * @param userDn * String * @param password * String * @return boolean */ public boolean authenticate(String userDn, String password) { LdapContext ctx = null; try { Control[] connCtls = new Control[] {}; ctx = getConnectionFromFool(); ctx.getRequestControls(); ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDn); ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password); ctx.reconnect(connCtls); return true; } catch (AuthenticationException e) { return false; } catch (NamingException e) { return false; } finally { if (ctx != null) { try { ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, "CN=administrator,O=fall"); ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, "123456"); ctx.reconnect(ctx.getConnectControls()); ctx.close(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ctx = null; } } }
三、添加用户信息
/** * 添加用户. * * @param userDN * String用户DN * @param userName * String 用户登录名 * @param userPwd * String 用户密码 * @return boolean 添加是否成功. * */ public boolean addUser(String userDN, String userName, String userPwd) { LdapContext ctx = null; try { ctx = getConnectionFromFool(); // Create attributes to be associated with the new user Attributes attrs = new BasicAttributes(true); attrs.put("objectClass", "person"); attrs.put("userPassword", userPwd); attrs.put("cn", userName); attrs.put("sn", userName); return true; } catch (NamingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ctx = null; } } return false; }
或者:
/** * 添加用户. * * @param userDN * String用户DN * @param attrs * Attributes 用户属性 * @return boolean 添加是否成功. * */ public boolean addUser(String userDN, Attributes attrs) { LdapContext ctx = null; try { ctx = getConnectionFromFool(); // 补充必须的用户属性. if (attrs.get("objectClass") == null ||attrs.get("objectClass").get() == null) { attrs.put("objectClass", "person"); } if (attrs.get("sn") == null ||attrs.get("sn").get() == null) { attrs.put("sn", userName); } ctx.createSubcontext(userDN, attrs); return true; } catch (NamingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ctx = null; } } return false; }
四、修改用户信息
/** * 修改用户信息. * * @param attrs * Attributes 需要修改的用户属性. * @param userDN * String 用户DN * @return */ public boolean modify(Attributes attrs, String userDN) { LdapContext ctx = null; try { ctx = getConnectionFromFool(); attrs.remove("cn"); ctx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs); return true; } catch (NamingException e) { System.err.println("Problem changing password: " + e); } catch (Exception e) { System.err.println("Problem: " + e); } finally { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ctx = null; } } return false; }
五、删除用户信息
/** * 删除用户. * * @param userDN * String 用户DN * @return */ public boolean del(String userDN) { LdapContext ctx = null; try { ctx = getConnectionFromFool(); ctx.destroySubcontext(userDN); return true; } catch (NamingException e) { System.err.println("Problem changing password: " + e); } catch (Exception e) { System.err.println("Problem: " + e); } finally { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } ctx = null; } } return false; }
相关文章推荐
- 用JAVA通过JNDI操作活动目录(AD)中LDAP
- Java通过Ldap操作AD的增删改查询
- Lotus的JAVA与DOMINO通过LDAP集成方法!
- Lotus的JAVA与DOMINO通过LDAP集成方法!
- JAVA系统和DOMINO通过LDAP集成方
- Lotus的JAVA与DOMINO通过LDAP集成方法
- 用Java通过DIIOP远程操作Domino服务器
- JAVA系统和DOMINO通过LDAP集成方
- 用Java通过DIIOP远程操作Domino服务器
- 通过java实现对Ldap的增删改操作
- Java通过Ldap操作AD的增删改查询
- java 两个通过java代码操作FTP的类,上传下载删除,获取文件
- LDAP操作之java篇(2)
- svnkit:通过java操作svn
- java通过JDBC驱动连接操作hive实例
- JNDI操作LDAP实现(二)
- JAVA通过JXL工具操作EXCEL文件
- java中dao层的通用层,通过反射机制,操作数据库的增删改,适用的范围是不包含属性类
- 黑马程序员--Java基础加强--15.利用反射操作泛型IV【通过反射Method解析泛型方法思路】【通过Method对四种Type子接口类型进行解剖】【使用递归对任意复合泛型类型进行彻底解剖】【个人
- 黑马程序员--Java基础加强--17.利用反射操作泛型VI【泛型类型变量的语义】【GenericDeclaration接口】【泛型接口TypeVariable】【通过Class反射解析泛型类】