這取決於你的用例。 如果您只想知道AD中的角色,則可以在LDAP上執行手動搜索。
實施例:
import javax.naming.*;
import javax.naming.directory.*;
public class test {
public String ldapUri = "ldap://localhost";
public String usersContainer = "cn=users,dc=example,dc=com"; // base DN for search
public String username = "user";
public String password = "pass";
public static void main(String args[]){
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUri);
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.REFERRAL, "follow");
try {
DirContext ctx = new InitialDirContext(env);
SearchControls ctls = new SearchControls();
String name = "your_username"; // full DN name
NamingEnumeration answer = ctx.search(usersContainer, "(member=" + name + ")",ctls);
while(answer.hasMore()) {
SearchResult rslt = (SearchResult)answer.next();
Attributes attrs = rslt.getAttributes();
System.out.println(attrs.get("cn"));
}
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
注意,用戶名必須是用戶的完整DN例如CN=user1,CN=users,DC=company,DC=com
如果您想在應用程序中將這些組用於角色分配,那麼事情會有點棘手。爲了達到這個目的,您可能需要定製ServerAuthModule(示例here),或者您必須修改SPNEGO部分以根據來自AD/LDAP的數據分配角色。
你能做到嗎?如果是,請介意在此添加答案? –