java通过ldap添加用户后_ldap连接不上改用户_JAVA通过LDAP做用户登录认证,怎么做业务的异常处理?...
通過java.namming包實現LDAP用戶登錄認證,怎么區分賬號被凍結、停用、不存在等異常的業務情況呢?
參考的是:網上普遍流傳的LDAP連接的代碼
public boolean auth(String username, String password) {
//設置相關常量
String initialContextFactory ="com.sun.jndi.ldap.LdapCtxFactory";
String ad4ProviderURL ="ldap://ip:port";
String securityAuthentication ="simple";
String domain ="ad4";
if (!username.startsWith(domain)) {
username = domain+"\"+ username;
}
/*
* 組織參數集合
*/
Hashtable env = new Hashtable();
//set the initializing information of the context
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
//set the URL of ldap server
env.put(javax.naming.Context.PROVIDER_URL, ad4ProviderURL);
//set the authentication mode
env.put(javax.naming.Context.SECURITY_AUTHENTICATION, securityAuthentication);
//set user of AD
env.put(javax.naming.Context.SECURITY_PRINCIPAL, username);
//set password of user
env.put(javax.naming.Context.SECURITY_CREDENTIALS, password);
/*
* 進行LDAP連接
*/
javax.naming.ldap.LdapContext ctx = null;
//initialize the ldap context
try {
ctx = new javax.naming.ldap.InitialLdapContext(env, null);
} catch (javax.naming.NamingException ex) {
System.out.println("Authentication error, username is:"+ username);
return false;
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (javax.naming.NamingException ex) {
System.out.println("Close Authentication context error");
ex.printStackTrace();
}
return true;? ? //獲取的LdapContext對象不為空,則為登錄成功
}
}
return false;? ? //否則登錄失敗
}
總結
以上是生活随笔為你收集整理的java通过ldap添加用户后_ldap连接不上改用户_JAVA通过LDAP做用户登录认证,怎么做业务的异常处理?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为音乐宣布与北京环球音像达成精品音乐合
- 下一篇: vivo Y78+新机再曝 现身Goog