Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| java [2025/05/05 15:41] – créée huracan | java [2025/05/05 15:45] (Version actuelle) – [Compiler et exécuter le code Java avec javac et java] huracan | ||
|---|---|---|---|
| Ligne 5: | Ligne 5: | ||
| ---- | ---- | ||
| - | ==== Compiler et exécuter le code Java avec javac et java, comme suit ==== | + | ==== Compiler et exécuter le code Java avec javac et java ==== |
| Exemple de code Java pour tester l’accès LDAP : | Exemple de code Java pour tester l’accès LDAP : | ||
| - | import javax.naming.*; | + | |
| - | import javax.naming.directory.*; | + | import javax.naming.directory.*; |
| - | import java.util.Hashtable; | + | import java.util.Hashtable; |
| - | + | ||
| - | public class LdapTest { | + | public class LdapTest { |
| - | public static void main(String[] args) { | + | public static void main(String[] args) { |
| - | String ldapUrl = " | + | String ldapUrl = " |
| - | String baseDn = " | + | String baseDn = " |
| - | String username = " | + | String username = " |
| - | String bindDn = "REG\\ldap-reader"; | + | String bindDn = "DOMAIN\\ldap-reader"; |
| - | String password = " | + | String password = " |
| - | + | ||
| - | Hashtable< | + | Hashtable< |
| - | env.put(Context.INITIAL_CONTEXT_FACTORY, | + | env.put(Context.INITIAL_CONTEXT_FACTORY, |
| - | env.put(Context.PROVIDER_URL, | + | env.put(Context.PROVIDER_URL, |
| - | env.put(Context.SECURITY_AUTHENTICATION, | + | env.put(Context.SECURITY_AUTHENTICATION, |
| - | env.put(Context.SECURITY_PRINCIPAL, | + | env.put(Context.SECURITY_PRINCIPAL, |
| - | env.put(Context.SECURITY_CREDENTIALS, | + | env.put(Context.SECURITY_CREDENTIALS, |
| - | + | ||
| - | try { | + | try { |
| - | DirContext ctx = new InitialDirContext(env); | + | DirContext ctx = new InitialDirContext(env); |
| - | + | ||
| - | String searchFilter = " | + | String searchFilter = " |
| - | SearchControls sc = new SearchControls(); | + | SearchControls sc = new SearchControls(); |
| - | sc.setSearchScope(SearchControls.SUBTREE_SCOPE); | + | sc.setSearchScope(SearchControls.SUBTREE_SCOPE); |
| - | + | ||
| - | NamingEnumeration< | + | NamingEnumeration< |
| - | + | ||
| - | if (results.hasMore()) { | + | if (results.hasMore()) { |
| - | SearchResult result = results.next(); | + | SearchResult result = results.next(); |
| - | System.out.println(" | + | System.out.println(" |
| - | } else { | + | } else { |
| - | System.out.println(" | + | System.out.println(" |
| - | } | + | } |
| - | + | ||
| - | ctx.close(); | + | ctx.close(); |
| - | } catch (NamingException e) { | + | } catch (NamingException e) { |
| - | System.err.println(" | + | System.err.println(" |
| - | e.printStackTrace(); | + | e.printStackTrace(); |
| - | } | + | } |
| - | } | + | } |
| - | } | + | } |
| Ligne 60: | Ligne 60: | ||
| #javac LdapTest.java | #javac LdapTest.java | ||
| + | |||
| + | Exécution du programme: | ||
| + | |||
| + | #java LdapTest | ||