`

Spring Ldap 域认证

    博客分类:
  • Web
阅读更多
核心提示:近段时间接触了一个项目,用户认证部分是通过域认证的方式,我们的项目是用Java语言开发的,在以前从未接触过这方面的知识,首先了解了这个域的概念,然后去看这个域到低是个什么样的东西.登录到域服务器,一看,哦,原来是一活动目录(Active Directory),这下看到了

近段时间接触了一个项目,用户认证部分是通过域认证的方式,我们的项目是用Java语言开发的,在以前从未接触过这方面的知识,首先了解了这个域的概念,然后去看这个域到低是个什么样的东西.登录到域服务器,一看,哦,原来是一活动目录(Active Directory),这下看到了它的全部面目,通过查询,还看到了我的名字也在这个域服务器上,顿时感觉到很新奇,再后来又知道了,域服务器除了AD,之外还有专门的服务器Open Ldap,还有其它的几个,在这里我就不写了,基本知道了这些东西后,通过查资料,知道了spring ldap 已很好的支持对域服务器的访问,我们的项目也采用了Spring 框架,于是下载了Spring-Ldap 1.3 这个版本的所有文档,开始研究,基本上还都能看懂,最主要是有例子,最终也通过写程序实现了,但我想通过Spring 的配置来实现,但在文档中没有找到相关说明,用户登录的信息肯定是动态的,如果只把用户名和密码配置在Spring 的配置文件中,通过这个配置也能查询其它用户信息,但毕竟不是最好的办法,后来通过仔细的研究,发现可以通过实现AuthenticationSource接口,动态的注入用户名和密码,总体的实现方式如下:

  1. <!-- spring ldap source配置 -->  
  2.  <bean id="ContextSource" class="org.springframework.ldap.core.support.LdapContextSource">  
  3.   <property name="url" value="ldap://ptr.petrochina:389" />  
  4.   <property name="base" value="dc=ptr,dc=petrochina" />  
  5.   <property name="referral" value="follow"></property> //让我折腾了很久的地方   
  6.   <property name="authenticationSource" ref="AuthenticationSource" />  
  7.  </bean>  
  8.  <bean id="AuthenticationSource"  
  9.   class="org.springframework.ldap.authentication.DefaultValuesAuthenticationSourceDecorator">  
  10.   <property name="target" ref="SpringSecurityAuthenticationSource" />  
  11.   <property name="defaultUser" value="***@petrochina.com.cn" />  
  12.   <property name="defaultPassword" value="***" />  
  13.  </bean>  
  14.  <bean id="SpringSecurityAuthenticationSource"  
  15.   class="com.BRM.right.component.LdapAuthenticationSource" />  
  16.  <bean id="LdapTemplate" class="org.springframework.ldap.core.LdapTemplate">  
  17.   <constructor-arg ref="ContextSource" />  
  18.  </bean>  
  19.   
  20. <!-- spring ldap source配置结束 -->  
  21.   

实现AuthenticationSource接口

  1. package com.BRM.right.component;   
  2.   
  3. import org.springframework.ldap.core.AuthenticationSource;   
  4.   
  5. public class LdapAuthenticationSource implements AuthenticationSource {   
  6.   
  7.  private static ThreadLocal login = new ThreadLocal();   
  8.  private static ThreadLocal password = new ThreadLocal();   
  9.   
  10.  public static void setLogin(String login) {   
  11.   LdapAuthenticationSource.login.set(login);   
  12.  }   
  13.   
  14.  public static void setPassword(String password) {   
  15.   LdapAuthenticationSource.password.set(password);   
  16.  }   
  17.   
  18.  public String getPrincipal() {   
  19.   String login = (String) LdapAuthenticationSource.login.get();   
  20.   if (login == null || "".equals(login)) {   
  21.    return null;   
  22.   }   
  23.   return login;   
  24.  }   
  25.   
  26.  public String getCredentials() {   
  27.   String password = (String) LdapAuthenticationSource.password.get();   
  28.   if (password == null || "".equals(password)) {   
  29.    return null;   
  30.   }   
  31.   return password;   
  32.  }   
  33.   
  34. }   
  35.   

记住要设置值:

  1. LdapAuthenticationSource.setLogin(loginForm.getUserName()+"@petrochina.com.cn");   
  2. LdapAuthenticationSource.setPassword(loginForm.getPassword());  

验证用户密码是否正确的方法:

  1. AndFilter filter = new AndFilter();   
  2.   filter.and(new EqualsFilter("userprincipalname", userprincipalname));   
  3.   return ldapTemplate.authenticate("", filter.toString(), password);  

由用户查询结果:

  1. public List findInfoByUserPrincipalName(String userprincipalname) {   
  2.   AndFilter filter = new AndFilter();   
  3.   // filter.and(new EqualsFilter("objectclass", "*"));   
  4.   filter.and(new EqualsFilter("userprincipalname", userprincipalname));   
  5.   
  6.   return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter   
  7.     .encode(), new LdapUserMapper());   
  8.  }   

实现接口AttributesMapper

  1. private class LdapUserMapper implements AttributesMapper {   
  2.   public Object mapFromAttributes(Attributes attrs) {   
  3.    LdapUserBean user = new LdapUserBean();   
  4.   
  5.    String yhm = "", yhmqc = "", email = "", dn = "", szdw = "";   
  6.    String[] memberOf = null;   
  7.    try {   
  8.     yhm = (String) attrs.get("samaccountname").get();   
  9.    } catch (Exception e) {   
  10.        
  11.    }   
  12.    try {   
  13.     yhmqc = (String) attrs.get("cn").get();   
  14.    } catch (Exception e) {   
  15.        
  16.    }   
  17.   
  18.    try {   
  19.     email = (String) attrs.get("mail").get();   
  20.    } catch (Exception e) {   
  21.        
  22.    }   
  23.    try {   
  24.     dn = (String) attrs.get("distinguishedname").get();   
  25.    } catch (Exception e) {   
  26.        
  27.    }   
  28.    try {   
  29.     szdw = (String) attrs.get("company").get();   
  30.    } catch (Exception e) {   
  31.        
  32.    }   
  33.    try {   
  34.     NamingEnumeration temp = attrs.get("memberof").getAll();   
  35.     List list = new ArrayList();   
  36.     while (temp.hasMoreElements()) {   
  37.      String t = temp.nextElement().toString();   
  38.      list.add(t);   
  39.     }   
  40.     // System.out.println(attrs.get("memberof").getAll());   
  41.     if (list != null) {   
  42.      memberOf = (String[]) list.toArray(new String[0]);   
  43.     }   
  44.   
  45.    } catch (Exception e) {   
  46.        
  47.    }   
  48.   
  49.    user.setYhm(yhm);   
  50.    user.setYhmqc(yhmqc);   
  51.    user.setEmail(email);   
  52.    user.setDisginguishedname(dn);   
  53.    user.setSzdw(szdw);   
  54.    user.setMemberof(memberOf);   
  55.    return user;   
  56.   }   
  57.  }   
  58.   
  1. public class LdapUserBean {   
  2.     
  3.  private String yhm;   
  4.     
  5.  private String yhmqc;   
  6.     
  7.  private String szdw;   
  8.     
  9.  private String email;   
  10.     
  11.  private String bz;   
  12.     
  13.  //CN=徐金召,OU=其他,OU=黑油山信息技术分公司,OU=新疆油田分公司,OU=新疆区域中心,DC=ptr,DC=petrochina   
  14.  private String disginguishedname;   
  15.     
  16.  /**  
  17.   * CN=twekty73testhbtest-A厂级查询,OU=hbtest,OU=a2,OU=公司总部,DC=ptr,DC=petrochina  
  18.   * CN=twehba2site1HBA2TWS-A油公司领导,OU=HBA2TWS,OU=a2,OU=公司总部,DC=ptr,DC=petrochina  
  19.   * CN=xjscm,OU=scm,OU=新疆油田分公司,OU=新疆区域中心,DC=ptr,DC=petrochina  
  20.   */  
  21.  private String[] memberof;    
  22.     
  23.  private String userprincipalname; //xujzh@petrochina.com.cn   
  24.   
  25.  public String getYhm() {   
  26.   return yhm;   
  27.  }   
  28.   
  29.  public void setYhm(String yhm) {   
  30.   this.yhm = yhm;   
  31.  }   
  32.   
  33.  public String getYhmqc() {   
  34.   return yhmqc;   
  35.  }   
  36.   
  37.  public void setYhmqc(String yhmqc) {   
  38.   this.yhmqc = yhmqc;   
  39.  }   
  40.   
  41.  public String getSzdw() {   
  42.   return szdw;   
  43.  }   
  44.   
  45.  public void setSzdw(String szdw) {   
  46.   this.szdw = szdw;   
  47.  }   
  48.   
  49.  public String getEmail() {   
  50.   return email;   
  51.  }   
  52.   
  53.  public void setEmail(String email) {   
  54.   this.email = email;   
  55.  }   
  56.   
  57.  public String getBz() {   
  58.   StringBuffer bs=new StringBuffer();   
  59.   for(int i=0;i<memberof.length;i++){   
  60.    bs.append(memberof[i]);   
  61.   }   
  62.   bs.append("用户所在路径:"+disginguishedname);   
  63.   this.bz = bs.toString();   
  64.   return bz;   
  65.  }   
  66.   
  67.  public void setBz(String bz) {   
  68.   this.bz=bz;   
  69.  }   
  70.   
  71.  public String getDisginguishedname() {   
  72.   return disginguishedname;   
  73.  }   
  74.   
  75.  public void setDisginguishedname(String disginguishedname) {   
  76.   this.disginguishedname = disginguishedname;   
  77.  }   
  78.   
  79.  public String[] getMemberof() {   
  80.   return memberof;   
  81.  }   
  82.   
  83.  public void setMemberof(String[] memberof) {   
  84.   this.memberof = memberof;   
  85.  }   
  86.   
  87.  public String getUserprincipalname() {   
  88.   return userprincipalname;   
  89.  }   
  90.   
  91.  public void setUserprincipalname(String userprincipalname) {   
  92.   this.userprincipalname = userprincipalname;   
  93.  }   
  94.     
  95.     
  96. }   
  97.   

罗列了这么多代码,也算是自己作一个记录吧.当然了还有很多实现方式,只不过这种实现方式自我感觉很良好.总体来说吧自己水平有限,请看到这文章的大侠们多提意见.

分享到:
评论
1 楼 GSD159 2014-10-14  
楼主,有源代码么?

相关推荐

Global site tag (gtag.js) - Google Analytics