您可以使用 HashMap options = new HashMap(); 看看我的 ldap 测试连接
package com.company.project.test;
import java.lang.reflect.Method;
import java.security.acl.Group;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.security.auth.Subject;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.security.SimplePrincipal;
import org.jboss.security.auth.callback.UsernamePasswordHandler;
import org.junit.Ignore;
import org.junit.Test;
/** Tests of the LoginModule classes.
*/
public class LoginModulesTestCase extends TestCase
{
static
{
try
{
Configuration.setConfiguration(new TestConfig());
System.out.println("Installed TestConfig as JAAS Configuration");
}
catch(Exception e)
{
e.printStackTrace();
}
}
/** Hard coded login configurations for the test cases. The configuration
name corresponds to the unit test function that uses the configuration.
*/
static class TestConfig extends Configuration
{
public void refresh()
{
}
public AppConfigurationEntry[] getAppConfigurationEntry(String name)
{
AppConfigurationEntry[] entry = null;
try
{
Class[] parameterTypes = {};
Method m = getClass().getDeclaredMethod(name, parameterTypes);
Object[] args = {};
entry = (AppConfigurationEntry[]) m.invoke(this, args);
}
catch(Exception e)
{
}
return entry;
}
AppConfigurationEntry[] testLdapExample1()
{
String name = "org.jboss.security.auth.spi.LdapLoginModule";
HashMap options = new HashMap();
options.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
options.put("java.naming.provider.url", "ldap://127.0.0.1:10389/");
options.put("java.naming.security.authentication", "simple");
options.put("principalDNPrefix", "uid=");
options.put("principalDNSuffix", ",ou=users,o=myorg");
options.put("rolesCtxDN", "ou=groups,o=myorg");
options.put("uidAttributeID", "member");
options.put("matchOnUserDN", "true");
options.put("roleAttributeID", "cn");
options.put("roleAttributeIsDN", "false");
AppConfigurationEntry ace = new AppConfigurationEntry(name,
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
AppConfigurationEntry[] entry = {ace};
return entry;
}
}
public LoginModulesTestCase(String testName)
{
super(testName);
}
@Test
public void testLdapExample1() throws Exception
{
System.out.println("testLdapExample1");
UsernamePasswordHandler handler = new UsernamePasswordHandler("nameuser", "passduser".toCharArray());
LoginContext lc = new LoginContext("testLdapExample1", handler);
lc.login();
Subject subject = lc.getSubject();
System.out.println("Subject: "+subject);
Set groups = subject.getPrincipals(Group.class);
assertTrue("Principals contains nameuser", subject.getPrincipals().contains(new SimplePrincipal("nameuser")));
Group roles = (Group) groups.iterator().next();
assertTrue("admin is a role", roles.isMember(new SimplePrincipal("admin")));
lc.logout();
}
}