Anonymous

如前所述,如果未设置身份验证环境属性,则默认身份验证机制为"none"。如果 Client 端将Context.SECURITY_AUTHENTICATION环境属性设置为"none",则认证机制为"none",所有其他认证环境属性都将被忽略。您只需要明确地执行此操作,以确保可以忽略可能设置的任何其他身份验证属性。无论哪种情况,Client 端都将被视为“匿名”Client 端。这意味着服务器不知道或不关心 Client 端是谁,并且将允许 Client 端访问(读取和更新)已配置为可由任何未经身份验证的 Client 端访问的任何数据。

由于命名和目录操作类中的目录示例均未设置任何身份验证环境属性,因此它们都使用匿名身份验证。

这里是an example,将Context\.SECURITY_AUTHENTICATION属性显式设置为"none"(尽管并非必须这样做,因为这是默认设置)。

// Set up the environment for creating the initial context
Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

// Use anonymous authentication
env.put(Context.SECURITY_AUTHENTICATION, "none");

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

// ... do something useful with ctx