1

我需要有关 java 类的帮助。我正在做一个有许多类的项目,这些类必须连接到我的服务器的 LDAP。在每个类中,我必须指定导入 javax,即绑定到 LDAP 服务器的上下文。有一种方法可以仅在一个类中指定 javax 和 context,而在所有其他类上调用此 javax 和 context 而无需重写所有内容?

我只想为我的所有班级指定一次 javax、hastable 和 env.put 的部分导入:

package ldapUserManagement;


import javax.naming.*;
import javax.naming.directory.*;

import java.util.Hashtable;
import java.util.List;

public class Usagers
{

    private static final String String = null;
    private static Context result;

    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, "ldap://192.168.0.5:389/dc=java,dc=local");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "java\\administrator");
        env.put(Context.SECURITY_CREDENTIALS, "*password*");
        env.put("java.naming.ldap.deleteRDN", "true");  


    try {...

我的目标是仅在一处指定要导入的管理员凭据和 javax

感谢回复一个明确的例子

感谢您的快速帮助!

4

2 回答 2

0

您可以使用 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();
   }

}
于 2012-12-10T12:20:24.760 回答
0

为您使用静态变量Hashtable

import javax.naming.*;

import java.util.Hashtable;

public class Usagers {

    private static Hashtable table = init();

    public static final Hashtable getEnv() {
        return table;
    }

    private static Hashtable init() {
        Hashtable env = new Hashtable();
        table.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        table.put(Context.PROVIDER_URL, "ldap://192.168.0.5:389/dc=java,dc=local");
        table.put(Context.SECURITY_AUTHENTICATION, "simple");
        table.put(Context.SECURITY_PRINCIPAL, "java\\administrator");
        table.put(Context.SECURITY_CREDENTIALS, "*password*");
        table.put("java.naming.ldap.deleteRDN", "true");

        return env;
    }
}

您将始终可以访问它Usagers.getEnv()

于 2012-12-08T21:45:50.207 回答