Guest User

Untitled

a guest
Oct 22nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. class AuthLDAPService {
  2.  
  3.     /*
  4.     *
  5.     * Überprüft ob sich User einlochen kann oder abgelehnt wird
  6.     *
  7.     /*
  8.  
  9.     //Admin Name+Passwort
  10.     final static String ADMIN_NAME = "sampleUser";
  11.     final static String ADMIN_PASSWORD = "samplePassword";
  12.    
  13.     //Standardpfad im Active Directory
  14.     final static String SEARCH_BASE = "ou=_fhwue,dc=fhwue,dc=de";
  15.    
  16.     static LdapContext ctx;
  17.     static transactional = true
  18.  
  19.     public boolean CheckLDAPConnection(String user_name, String user_password) {
  20.        
  21.         //da auf den lokalen entwicklerrechnern kein LDAP verf¸gbar ist
  22.         if (GrailsUtil.getEnvironment().equalsIgnoreCase("development")) {
  23.             return true;
  24.         }
  25.         if (!user_name || !user_password) {
  26.             return false;
  27.         }
  28.         try {
  29.             //Mit dem LDAP Server verbinden
  30.             LDAPConnect();
  31.  
  32.             //Pr¸fen, ob ¸bergebener User auf dem LDAP-Server angelegt ist
  33.             List list = findUsersByAccountName(user_name);
  34.  
  35.             //Pr¸fung nur, wenn User nicht mehrfach angelegt ist
  36.             if (list.size() == 1) {
  37.  
  38.                 //Verbdinungs-Einstellungen f¸r den LDAP Zugriff mit ¸bergebenen Userdaten setzen
  39.                 Hashtable<String, String> env1 = new Hashtable<String, String>();
  40.                 env1.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  41.                 env1.put(Context.SECURITY_AUTHENTICATION, "simple");
  42.                 env1.put(Context.SECURITY_PRINCIPAL, user_name);
  43.                 env1.put(Context.SECURITY_CREDENTIALS, user_password);
  44.                 env1.put(Context.PROVIDER_URL, "ldap://192.168.0.5:389");
  45.  
  46.                 try {
  47.                     //Beim LDAP
  48.                     new InitialLdapContext(env1, null);
  49.  
  50.                     //Erfolgreiche Verbindung
  51.                     return true;
  52.                 } catch (AuthenticationException e) {
  53.  
  54.                     //Fehlgeschlagene Verbindung
  55.                     return false;
  56.                 }
  57.             }
  58.             ctx.close();
  59.         } catch (Exception e1) {
  60.             e1.printStackTrace();
  61.         }
  62.         return false;
  63.     }
  64. }
Add Comment
Please, Sign In to add comment