Guest User

Untitled

a guest
Apr 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1.     public Authentication authenticate(Authentication authentication)
  2.             throws AuthenticationException {
  3.         final String username = (String) authentication.getPrincipal();
  4.         final String password = (String) authentication.getCredentials();
  5.         System.out.println(username + password);
  6.  
  7.         String searchFilter = "(&(objectClass=user)(sAMAccountName=" + username
  8.                 + "))";
  9.  
  10.         System.out.println(searchFilter);
  11.         SearchControls searchControls = new SearchControls();
  12.         searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  13.  
  14.         Hashtable<String, String> environment = new Hashtable<String, String>();
  15.         environment.put(Context.INITIAL_CONTEXT_FACTORY,
  16.                 "com.sun.jndi.ldap.LdapCtxFactory");
  17.         environment.put(Context.PROVIDER_URL, host);
  18.         environment.put(Context.SECURITY_AUTHENTICATION, "simple");
  19.         environment.put(Context.SECURITY_PRINCIPAL, username + "@" + domain);
  20.         environment.put(Context.SECURITY_CREDENTIALS, password);
  21.  
  22.         System.out.println(environment);
  23.         try {
  24.             LdapContext ldapContext = new InitialLdapContext(environment, null);
  25.  
  26.             // Search objects in GC using filters
  27.             NamingEnumeration<SearchResult> answer = ldapContext.search(
  28.                     searchBase, searchFilter, searchControls);
  29.  
  30.             while (answer.hasMoreElements()) {
  31.                 SearchResult result = (SearchResult) answer.next();
  32.                 Attributes resultAttributes = result.getAttributes();
  33.                 Map<String, Object> response = null;
  34.                 System.out.println(resultAttributes);
  35.                 if (resultAttributes != null) {
  36.                     response = new HashMap<String, Object>();
  37.                     NamingEnumeration ne = resultAttributes.getAll();
  38.  
  39.                     while (ne.hasMore()) {
  40.                         Attribute resultAttribute = (Attribute) ne.next();
  41.                         response.put(resultAttribute.getID(),
  42.                                 resultAttribute.get());
  43.                     }
  44.  
  45.                     ne.close();
  46.                 }
  47.  
  48.                 User user = userService.getUser(username, true);
  49.  
  50.                 return new LdapAuthentication(user, true);
  51.             }
  52.         } catch (Exception ex) {
  53.             System.out.println(ex);
  54.         }
  55.  
  56.         LdapAuthentication ldapAuthentication = new LdapAuthentication();
  57.         ldapAuthentication.setName(username);
  58.         ldapAuthentication.setPrincipal(username);
  59.         ldapAuthentication.setCredentials(password);
  60.         ldapAuthentication.setAuthenticated(false);
  61.  
  62.         return ldapAuthentication;
  63.     }
  64.  
  65.     public boolean supports(Class<? extends Object> authentication) {
  66.         return LdapAuthentication.class.isAssignableFrom(authentication
  67.                 .getClass());
  68.     }
Add Comment
Please, Sign In to add comment