Package org.beangle.security.monitor.auth.provider.ldap

Source Code of org.beangle.security.monitor.auth.provider.ldap.DefaultLdapAuthenticationProvider

//$Id: LdapMoniteeServiceImpl.java May 16, 2008 1:41:37 PM chaostone Exp $
/*
*
* Copyright c 2005-2009
* Licensed under the Apache License, Version 2.0 (the "License")
* http://www.apache.org/licenses/LICENSE-2.0
*
*
*/
/********************************************************************************
* @author chaostone
*
* MODIFICATION DESCRIPTION
*
* Name           Date          Description
* ============   ============  ============
* chaostone      May 16, 2008  Created
********************************************************************************/
package org.beangle.security.monitor.auth.provider.ldap;

import java.security.NoSuchAlgorithmException;

import org.beangle.security.monitor.Authentication;
import org.beangle.security.monitor.AuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ldap.ContextMapper;
import org.springframework.ldap.EntryNotFoundException;
import org.springframework.ldap.LdapTemplate;
import org.springframework.ldap.support.DirContextAdapter;
import org.springframework.ldap.support.DistinguishedName;

/**
* 读取ldap的用户信息<br>
*
* @author chaostone
*
*/
public class DefaultLdapAuthenticationProvider extends AbstractLdapAuthenticationProvider {
  private static final  Logger logger = LoggerFactory.getLogger(DefaultLdapAuthenticationProvider.class);
 
  protected LdapTemplate ldapTemplate;

  protected String nameAttrName = "uid";

  protected String passwordAttrName = "userPassword";

  protected boolean doVerify(Authentication auth) {
    String userName = (String) auth.getPrincipal();
    String password = (String) auth.getCredentials();
    DistinguishedName dn = new DistinguishedName(nameAttrName + "=" + userName);
    String ldapPassword = null;
    try {
      ldapPassword = (String) ldapTemplate.lookup(dn, new String[] { "userPassword" },
          new ContextMapper() {
            public Object mapFromContext(Object ctx) {
              DirContextAdapter context = (DirContextAdapter) ctx;
              return new String((byte[]) context.getObjectAttribute("userPassword"));
            }
          });
    } catch (EntryNotFoundException e) {
      logger.info("cannot found {} in ldap", userName);
      throw new AuthenticationException("ldap user " + userName + " not found");
    }
    try {
      return (LdapPasswordHandler.getInstance().verify(ldapPassword, password));
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }

  public String getNameAttrName() {
    return nameAttrName;
  }

  public void setNameAttrName(String nameAttrName) {
    this.nameAttrName = nameAttrName;
  }

  public String getPasswordAttrName() {
    return passwordAttrName;
  }

  public void setPasswordAttrName(String passwordAttrName) {
    this.passwordAttrName = passwordAttrName;
  }

  public void setLdapTemplate(LdapTemplate ldapTemplate) {
    this.ldapTemplate = ldapTemplate;
  }

  public LdapTemplate getLdapTemplate() {
    return ldapTemplate;
  }

}
TOP

Related Classes of org.beangle.security.monitor.auth.provider.ldap.DefaultLdapAuthenticationProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.