Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Authentication authenticate(Authentication authentication)
- throws AuthenticationException {
- final String username = (String) authentication.getPrincipal();
- final String password = (String) authentication.getCredentials();
- System.out.println(username + password);
- String searchFilter = "(&(objectClass=user)(sAMAccountName=" + username
- + "))";
- System.out.println(searchFilter);
- SearchControls searchControls = new SearchControls();
- searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
- Hashtable<String, String> environment = new Hashtable<String, String>();
- environment.put(Context.INITIAL_CONTEXT_FACTORY,
- "com.sun.jndi.ldap.LdapCtxFactory");
- environment.put(Context.PROVIDER_URL, host);
- environment.put(Context.SECURITY_AUTHENTICATION, "simple");
- environment.put(Context.SECURITY_PRINCIPAL, username + "@" + domain);
- environment.put(Context.SECURITY_CREDENTIALS, password);
- System.out.println(environment);
- try {
- LdapContext ldapContext = new InitialLdapContext(environment, null);
- // Search objects in GC using filters
- NamingEnumeration<SearchResult> answer = ldapContext.search(
- searchBase, searchFilter, searchControls);
- while (answer.hasMoreElements()) {
- SearchResult result = (SearchResult) answer.next();
- Attributes resultAttributes = result.getAttributes();
- Map<String, Object> response = null;
- System.out.println(resultAttributes);
- if (resultAttributes != null) {
- response = new HashMap<String, Object>();
- NamingEnumeration ne = resultAttributes.getAll();
- while (ne.hasMore()) {
- Attribute resultAttribute = (Attribute) ne.next();
- response.put(resultAttribute.getID(),
- resultAttribute.get());
- }
- ne.close();
- }
- User user = userService.getUser(username, true);
- return new LdapAuthentication(user, true);
- }
- } catch (Exception ex) {
- System.out.println(ex);
- }
- LdapAuthentication ldapAuthentication = new LdapAuthentication();
- ldapAuthentication.setName(username);
- ldapAuthentication.setPrincipal(username);
- ldapAuthentication.setCredentials(password);
- ldapAuthentication.setAuthenticated(false);
- return ldapAuthentication;
- }
- public boolean supports(Class<? extends Object> authentication) {
- return LdapAuthentication.class.isAssignableFrom(authentication
- .getClass());
- }
Add Comment
Please, Sign In to add comment