[英]LDAP authentication with Java
我有一個Web應用程序,我嘗試使用LDAP身份驗證登錄(用戶使用他們的Windows會話ID登錄)
我試過這堂課:
public static boolean ad (String log,String pass) throws NamingException
{
try
{
System.out.println("Début du test Active Directory");
Hashtable<String, String> ldapEnv = new Hashtable<String, String>();
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, "ldap://LDAPserver:389");
ldapEnv.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");
ldapEnv.put("java.naming.security.sasl.realm","MyCompany.com");
ldapEnv.put("javax.security.sasl.qop", "auth");
ldapEnv.put("javax.security.sasl.strength","high");
ldapEnv.put(Context.SECURITY_PRINCIPAL,log.toLowerCase());
System.out.println(pass);
ldapEnv.put(Context.SECURITY_CREDENTIALS,pass);
ldapContext = new InitialDirContext(ldapEnv);
return true;
}
catch (Exception e)
{
return false;
}
}
它適用於某些用戶,但不適用於所有用戶,我不明白為什么。
我在我的項目中做了同樣的事情,它可能對你有所幫助。
package com.agileinfotech.bsviewer.ldap;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
public class LDAPLoginAuthentication {
public LDAPLoginAuthentication() {
// TODO Auto-generated constructor
}
public String authenticateUser(String username, String password) {
String strUrl = "success";
System.out.print("username :" + username + " password" + password);
Hashtable env = new Hashtable(11);
boolean b = false;
String Securityprinciple = "cn=" + username + ",ou=users,o=agile-infotech,ou=system";
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:10389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, Securityprinciple);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
// Close the context when we're done
b = true;
ctx.close();
} catch (NamingException e) {
b = false;
} finally {
if (b) {
strUrl = "success";
} else {
strUrl = "failure";
}
}
return strUrl;
}
}
我的答案來得很晚,但在任何情況下都可以幫助一些用戶......
開發人員指定身份驗證適用於某些用戶,而不適用於其他用戶。 因此,這里可能出現的錯誤是生成md5的方式:某些算法用於以數字格式存儲值,這在值以0開頭時會出現問題。這是因為數字格式(整數等)將被刪除這些值來自值,並使MD5無效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.