A. 用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
希望可以幫到你
B. JAVA連接LDAP(Windows AD)驗證時,密碼為空驗證通過的問題
密碼為空 ,實際值是null
任何值與null比較時,記得返回值都是true,
故在程序里需要預先把空值過濾掉
if(pwd==null)
System.out.println("輸入密碼為空,驗證不通過!");
C. 如何使用Java操作LDAP之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("=====================");
}
}
}