如何在 websphere 上使用 JNDI 操作 LDAP?

发布于 2025-01-03 15:14:45 字数 2974 浏览 6 评论 0原文

我在 LDAP 操作中遇到问题。我想在用户从 GUI/浏览器中选择成员时动态地将成员添加到 LDAP 组。我粘贴了下面的代码,当我在测试类中运行它时(使用com.sun.jndi.ldap.LdapCtxFactory),该代码运行得非常好。但是,当我将其打包到构建中,部署到 websphere 应用程序服务器 7.0 上(使用 com.ibm.websphere.naming.WsnInitialContextFactory ),并根据用户的选择调用此方法时,我收到错误以下。我想知道我做错了什么。 WAS不提供ldap连接工厂的实现吗?我还尝试使用 sun 的 ldap 在 WAS 上进行部署,否则它可以在 Test 类上运行,但我得到了与下面相同的异常。如果有人能提供线索,我将不胜感激。

添加成员时出现问题:javax.naming.OperationNotSupportedException:[LDAP:错误代码 53 - 00000561:SvcErr:DSID-031A120C,问题 5003 (WILL_NOT_PERFORM),数据 0

My Code:

public class LDAPManager
{
    String GROUPS_OU =  "cn=users,dc=mit,dc=hq,dc=com";

    public Boolean addMember(String user, String group)
    {

        Hashtable env = new Hashtable();
        String adminName = "CN=Administrator,CN=Users,DC=mit,DC=hq,DC=com";
        String adminPassword = "asdfasdf21Q";
        String ldapURL = "ldap://mybox451Dev.mit.hq.com:389";
        String userName = "CN="+user+",CN=Users,DC=mit,DC=hq,DC=com";
        String groupName = "CN="+group+",CN=Users,DC=mit,DC=hq,DC=com";


        //env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

        //set security credentials, note using simple cleartext authentication
        env.put(Context.SECURITY_AUTHENTICATION,"simple");
        env.put(Context.SECURITY_PRINCIPAL,adminName);
        env.put(Context.SECURITY_CREDENTIALS,adminPassword);

        //connect to my domain controller
        env.put(Context.PROVIDER_URL, "ldap://mybox451Dev.mit.hq.com:389");

        try {

            // Create the initial directory context
            InitialDirContext ctx = new InitialDirContext(env);

            //Create a LDAP add attribute for the member attribute
            ModificationItem mods[] = new ModificationItem[1];
            mods[0]= new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", userName)); 

            //update the group
            ctx.modifyAttributes(groupName,mods);

            ctx.close();

            //System.out.println("Added " + userName + " to " + groupName);

        } 

        catch (NamingException e) {
            System.err.println("Problem adding member: " + e);
        }

        return true;
    }

}

我解决了。在这里发布解决方案,希望这对某人有帮助。

  1. 使用sun的标准JNDI上下文,而不是websphere。
  2. 我在哈希表中缺少的其他属性,一旦我添加它们,它就像一个魅力。

    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");  
    
    //env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");  
    
    //设置安全凭证,注意使用简单的明文身份验证  
    env.put(Context.SECURITY_AUTHENTICATION,"简单");  
    env.put(Context.SECURITY_PRINCIPAL,adminName);  
    env.put(Context.SECURITY_CREDENTIALS,adminPassword);  
    env.put(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");  
    env.put(Context.REFERRAL, "忽略");  
    

I am facing a problem with an LDAP operation. I want to dynamically add a member to an LDAP group when selected by the user from GUI / browser. I paste the code below which works perfectly well when I run it in a Test class (using com.sun.jndi.ldap.LdapCtxFactory). But, when I package it in my build, deploy on websphere app server 7.0 (using com.ibm.websphere.naming.WsnInitialContextFactory), and invoke this method according to user's selection, then I get the error below. I wonder what's wrong I am doing. Doesn't WAS provide implementation of ldap connection factory? I also tried deploying on WAS with the sun's ldap which otherwise works on the Test class, but I am getting the same exception as below. I'd appreciate if anybody can give a clue.

Problem adding member: javax.naming.OperationNotSupportedException: [LDAP: error code 53 - 00000561: SvcErr: DSID-031A120C, problem 5003 (WILL_NOT_PERFORM), data 0

My Code:

public class LDAPManager
{
    String GROUPS_OU =  "cn=users,dc=mit,dc=hq,dc=com";

    public Boolean addMember(String user, String group)
    {

        Hashtable env = new Hashtable();
        String adminName = "CN=Administrator,CN=Users,DC=mit,DC=hq,DC=com";
        String adminPassword = "asdfasdf21Q";
        String ldapURL = "ldap://mybox451Dev.mit.hq.com:389";
        String userName = "CN="+user+",CN=Users,DC=mit,DC=hq,DC=com";
        String groupName = "CN="+group+",CN=Users,DC=mit,DC=hq,DC=com";


        //env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

        //set security credentials, note using simple cleartext authentication
        env.put(Context.SECURITY_AUTHENTICATION,"simple");
        env.put(Context.SECURITY_PRINCIPAL,adminName);
        env.put(Context.SECURITY_CREDENTIALS,adminPassword);

        //connect to my domain controller
        env.put(Context.PROVIDER_URL, "ldap://mybox451Dev.mit.hq.com:389");

        try {

            // Create the initial directory context
            InitialDirContext ctx = new InitialDirContext(env);

            //Create a LDAP add attribute for the member attribute
            ModificationItem mods[] = new ModificationItem[1];
            mods[0]= new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", userName)); 

            //update the group
            ctx.modifyAttributes(groupName,mods);

            ctx.close();

            //System.out.println("Added " + userName + " to " + groupName);

        } 

        catch (NamingException e) {
            System.err.println("Problem adding member: " + e);
        }

        return true;
    }

}

I got it solved. Posting solution here, hope this helps someone.

  1. Use the standard JNDI context of sun, not websphere.
  2. Additional properties I was missing in the hashtable, once I added them, it worked like a charm.

    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");  
    
    //env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");  
    
    //set security credentials, note using simple cleartext authentication  
    env.put(Context.SECURITY_AUTHENTICATION,"simple");  
    env.put(Context.SECURITY_PRINCIPAL,adminName);  
    env.put(Context.SECURITY_CREDENTIALS,adminPassword);  
    env.put(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");  
    env.put(Context.REFERRAL, "ignore");  
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2025-01-10 15:14:45

好吧,距离提出这个问题已有一年多了;所以,我不知道回答会增加任何价值。但是,就在这里。请参阅 WAS Javadocs 了解有关该工厂类实际功能及其工作原理的详细信息。您可能需要调整 WAS 的 jndiprovider.properties 文件。

Well, it's been more than a year since this question has been asked; so, I don't know answering will add any value. But, here it is. See WAS Javadocs for details on how what that factory class actually does and how it works. You may need to adjust your jndiprovider.properties file for WAS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文