37 lines
1.3 KiB
Plaintext
37 lines
1.3 KiB
Plaintext
![]() |
include::../description.adoc[]
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
This rule raises an issue when an LDAP connection is created with <code>Context.SECURITY_AUTHENTICATION</code> set to <code>"none"</code>.
|
||
|
----
|
||
|
// 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"); // Noncompliant
|
||
|
|
||
|
// Create the initial context
|
||
|
DirContext ctx = new InitialDirContext(env);
|
||
|
----
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
// 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 simple authentication
|
||
|
env.put(Context.SECURITY_AUTHENTICATION, "simple");
|
||
|
env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
|
||
|
env.put(Context.SECURITY_CREDENTIALS, getLDAPPassword());
|
||
|
|
||
|
// Create the initial context
|
||
|
DirContext ctx = new InitialDirContext(env);
|
||
|
----
|
||
|
|
||
|
include::../see.adoc[]
|