導航:首頁 > 編程語言 > java連接ldap

java連接ldap

發布時間:2022-11-21 01:59:00

『壹』 LDAP在java中如何模糊查詢

/** * 獲得LDAP連接(不通過連接池,直接獲得連接) * @return * @throws Exception */ private javax.naming.directory.DirContext getDirContext() throws Exception { DirContext ctx = null; java.util.Hashtable env = new java.util.Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, user); env.put(Context.SECURITY_CREDENTIALS, pwd); ctx = new InitialDirContext(env); return ctx; } /** * 返回用戶查詢介面 * @param DN 查找范圍 ou=test,dc=abcd,dc=com * @param filter 過濾條件 cn=* * @param myserach 搜索范圍 * @return LDAP標准介面 * @throws Exception */ public javax.naming.NamingEnumeration getFilter(String DN,String filter,int myserach) throws Exception { DirContext ctx = getDirContext(); try { NamingEnumeration em; SearchControls con = new SearchControls(); con.setSearchScope(myserach); em = ctx.search(DN, filter, con); return em; } finally { // } } /** * 設置條件查找 * @param dn 查找的根結點 * @param filter 查找條件 * @param level 查找范圍 * @return 返回一個由DN組成的JAVA STRING 列表 * @throws Exception */ public java.util.Vector getfilterNodes(String dn,String filter,int level) throws Exception { java.util.Vector ve=new java.util.Vector(); NamingEnumeration em=getFilter(dn,filter,level); while(em!=null && em.hasMoreElements()) { SearchResult rs=(SearchResult)em.nextElement(); String db=rs.getName(); if (db.trim().equals("")) ve.add(dn); else ve.add(db+","+dn); } return ve; }

『貳』 java 連接 ldap 報錯

應該是用戶名寫的不對,env.put(Context.SECURITY_PRINCIPAL,"cn=" + username);
這裡面應該寫用戶的全路徑名,比如cn=xxxxx,dc=combatelecom,dc=com
說白了就是你怎麼從根節點找到的用戶,把用戶節點的dn拷貝出來就行了
另外那個URL部分寫成env.put(Context.PROVIDER_URL,"ldap://10.10.0.13:389")

『叄』 java操作ldap的方式有幾種

我寫過這類代碼,與你這段代碼有些不一樣。env.put(Context.SECURITY_AUTHENTICATION,"none");這段中我指定他的認證方式是"simple",也就是採用用戶名/密碼的方式認證,我也不是很清楚採用none是否能夠成功認證。認證用的用戶名應該要寫全,你需要查看每一個上級節點的屬性,打個比方,IS是CN,FADU是DC,funo.com.cn是DC,那你的認證用戶名就應該是:DC=funo.com.cn,DC=FADU,CN=IS,CN=caiwei你先試一下吧,還有問題再討論,令附上LDAPERRORCODE:/index.php?title=LDAP_Error_Codes

『肆』 使用java連接LDAP伺服器進行賬號驗證時,當用戶被禁用,返回什麼異常.

Context.SECURITY_PRINCIPAL這個相當於。不給你你怎麼登陸ldap伺服器。估計你能給出客戶給了哪些信息嗎。不需要你全部給出。

『伍』 java連接LDAP伺服器出錯

網路狀況不好,導致連接不正常

『陸』 如何使用java鏈接ldap資料庫

public static void main(String[] args) {
String url = "ldap://10.0.0.10:389/";
String domain = "dc=dtas,dc=com";
String user = "cn=administrator,cn=users";
String password = "111111";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP 工廠
env.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP訪問安全級別
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL, user+","+domain); // 填DN
env.put(Context.SECURITY_CREDENTIALS, password); // AD Password
env.put("java.naming.ldap.attributes.binary", "objectSid objectGUID");
LdapContext ldapCtx = null;
try {
ldapCtx = new InitialLdapContext(env , null);
queryGroup(ldapCtx);
//queryUser(ldapCtx);

} catch (NamingException e) {
e.printStackTrace();
} finally {
if(ldapCtx != null) {
try {
ldapCtx.close();
} catch (NamingException e) {
}
}
}
}

private static void queryGroup(LdapContext ldapCtx) throws NamingException {
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "objectClass=organizationalUnit";
String searchBase = "ou=myDeptSubDept,ou=myDept,dc=DS-66,dc=com";
String returnedAtts[] = {"distinguishedName", "objectGUID", "name"};
searchCtls.setReturningAttributes(returnedAtts);
NamingEnumeration<SearchResult> answer = ldapCtx.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes Attrs = sr.getAttributes();
if (Attrs != null) {
NamingEnumeration<?> ne = Attrs.getAll();
while(ne.hasMore()) {
Attribute Attr = (Attribute)ne.next();
String name = Attr.getID();
Enumeration<?> values = Attr.getAll();
if (values != null) { // 迭代
while (values.hasMoreElements()) {
String value = "";
if("objectGUID".equals(name)) {
value = UUID.nameUUIDFromBytes((byte[]) values.nextElement()).toString();
} else {
value = (String)values.nextElement();
}
System.out.println(name + " " + value);
}
}
}
System.out.println("=====================");
}
}

}

『柒』 用java操作ldap是報錯,求大神解救

出自 http://blog.csdn.net/techchan/article/details/5440775

1. error code 53
===========================================================================
問題:創建新用戶時出現數據後端異常
在 WebSphere Portal Express 中,您可以設置密碼的最短和最長長度。如果設置的密碼長度與 LDAP 伺服器的策略不相同,則在創建用戶時您可能會看到以下異常:
EJPSG0015E: Data Backend Problem com.ibm.websphere.wmm.exception.WMMSystemException:
The following Naming Exception occurred ring processing:
"javax.naming.: [LDAP: error code 53 - 0000052D:
SvcErr: DSID-031A0FBC, problem 5003 (WILL_NOT_PERFORM), data 0
]; remaining name 'cn=see1anna,cn=users,dc=wps510,dc=rtp,dc=raleigh,dc=ibm,dc=com';
resolved object com.sun.jndi.ldap.LdapCtx@7075b1b4".

原因:這是由於「密碼不能滿足密碼策略的要求」導致

解決方案:
1. 打開域安全策略-安全設置-賬戶策略-密碼策略-密碼必須符合復雜性要求。定義這個策略設置為:已禁用。/ 密碼長度最小值:定義這個策略設置為0。

2. 打開域控制器安全策略-安全設置-賬戶策略-密碼策略-密碼必須符合復雜性要求。定義這個策略設置為:已禁用。/ 密碼長度最小值:定義這個策略設置為0。

3. 最後運行刷新組策略命令為:gpupdate /force

===========================================================================

2. Need to specify class name
===========================================================================
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

原因:LdapContext在處理完上個環節被close(),LdapContext=null;
解決方案:不close;

3. error code 50
===========================================================================
javax.naming.NoPermissionException: [LDAP: error code 50 - 00002098: SecErr: DSID-03150A45, problem 4003 (INSUFF_ACCESS_RIGHTS), data 0

4. error code 68
===========================================================================
javax.naming.NameAlreadyBoundException: [LDAP: error code 68 - 00000524: UpdErr: DSID-031A0F4F, problem 6005 (ENTRY_EXISTS), data 0

原因:創建的用戶已經存在了

7. No trusted certificate
===========================================================================
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
1.cas機器A,A上a,b,c服務運行良好
2.website 位於B機器,cas可以截獲請求,跳轉javax.net.ssl.SSLHandshakeException

將A上生生成的客戶端密鑰,導入B
A運行
sudo keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore server.keystore -validity 3600

$ keytool -export -trustcacerts -alias tomcat -file server.cer -keystore server.keystore -storepass changeit

$ sudo keytool -import -trustcacerts -alias tomcat -file server.cer -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit
B運行最後一句即可

建立信任關系,客戶,服務密鑰,客戶多處

8. error code 1
===========================================================================
javax.naming.NamingException: [LDAP: error code 1 - 00000000: LdapErr: DSID-0C090AE2, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece

原因:新增域用戶的時候,ctx沒有綁定管理員用戶
解決方法:ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, adminUser + "@" + ldapProperty.getDomain());
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, adminPwd);

9. error code 50
==========================================================================
javax.naming.NoPermissionException: [LDAP: error code 50 - 00000005: SecErr: DSID-03151E04, problem 4003 (INSUFF_ACCESS_RIGHTS)

原因:新建域用戶時候,ctx綁定到一個普通用戶(該用戶沒有新建用戶的許可權)
解決方法:使用管理員用戶進行綁定:
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, adminUser + "@" + ldapProperty.getDomain());
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, adminPwd);

10. error code 19
==========================================================================
javax.naming.directory.: [LDAP: error code 19 - 0000052D: AtrErr: DSID-03190F00, #1:
0: 0000052D: DSID-03190F00, problem 1005 (CONSTRAINT_ATT_TYPE)

原因:這個最大的可能是不滿足域安全策略:如密碼復雜性、密碼最短使用期限、強制密碼歷史。即長度、包含的字元、多久可以修改密碼、是否可以使用歷史密碼等。

11. LDAP: error code 50
==========================================================================
javax.naming.NoPermissionException: [LDAP: error code 50 - 00000005: SecErr: DSID-031A0F44, problem 4003 (INSUFF_ACCESS_RIGHTS)

原因:這個是最初代碼使用的replace操作,這個在AD里對應的是密碼重設(普通用戶默認沒有這個許可權,管理員可以操作),另外remove操作時提供的舊密碼錯誤也可能報這個異常

12. RSA premaster secret error
==========================================================================
javax.naming.CommunicationException: simple bind failed: 172.18.20.4:636 [Root exception is javax.net.ssl.SSLKeyException: RSA premaster secret error]

原因:Tomcat 配置的JDK與添加證書的的JDK不一致。如:證書存放路徑為C:/Java/jdk1.6.0_10/jre/lib/cacerts 而Tomcat 配置的JDK為C:/Java/jre6 ,使得兩者路徑不一致,SSL驗證的時候,找不到證書

13.No trusted certificate found
==========================================================================

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found

原因:信任證書庫文件路徑不正確
解決方法:將正確工程中 /WEB-INF/classes目錄下

14. error code 49
==========================================================================
javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece
希望可以幫到你

『捌』 如何通過java操作ldap實現登錄

catch (NamingException e) {
e.printStackTrace();
} finally {
if(ldapCtx != null) {
try {
ldapCtx.close();
} catch (NamingException e) {
}
}

閱讀全文

與java連接ldap相關的資料

熱點內容
下班之後的程序員 瀏覽:69
檢測支持ssl加密演算法 瀏覽:340
衢州發布新聞什麼APP 瀏覽:80
中國移動長沙dns伺服器地址 瀏覽:249
wifi密碼加密了怎麼破解嗎 瀏覽:596
linux命令cpu使用率 瀏覽:67
linux實用命令 瀏覽:238
傳奇引擎修改在線時間命令 瀏覽:109
php取域名中間 瀏覽:897
cad命令欄太小 瀏覽:830
php開發環境搭建eclipse 瀏覽:480
qt文件夾名稱大全 瀏覽:212
金山雲伺服器架構 瀏覽:230
安卓系統筆記本怎麼切換系統 瀏覽:618
u盤加密快2個小時還沒有搞完 瀏覽:93
小米有品商家版app叫什麼 瀏覽:94
行命令調用 瀏覽:436
菜鳥裹裹員用什麼app 瀏覽:273
窮查理寶典pdf下載 瀏覽:514
csgo您已被禁用此伺服器怎麼辦 瀏覽:398