Spring LdapTemplate讀取以及Freemark簡單合成頁面

來源:互聯網
上載者:User

客戶近期頻繁要求修改組織機構,維護部門苦不堪言。更新群組織機構就要更新郵箱的通訊錄,使用的應該是某流行的郵件系統,php的,版本挺老的,其中有個功能,在寫收件者的時候輸入的東西會autocomplate,這個東西維護部門需要維護一個超大的檔案夾,如下圖:一級目錄是第一個字,二級目錄是前兩個字,三級目錄是前三個字,以此類推,其中每個檔案夾下包含一個result.php檔案,內容為該級目錄中所對應的全部人員。

那我需要做什麼呢?對,協助他們產生這個東西。縷了一下思路,需要用到ldap、freemark的知識。

Spring大家應該不陌生,這裡ldap資料的讀取,我選用的是Spring的ldapTemplate 1.3.1,配置如jdbcTemplate:

<bean id="contextSource"    class="org.springframework.ldap.core.support.LdapContextSource">      <property name="url" value="ldap://192.168.101.179:389"  />      <property name="base" value=""  />      <property name="userDn" value="cn=platoptuser,ou=內建帳號,ou=系統,o=北京XX局"  />      <property name="password" value="111111"  />  </bean>  <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">      <constructor-arg ref="contextSource"  />  </bean>

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

讀取Ldap這塊我遇到一個痛點,讀取出來人如何得到這個人屬於哪個組織機構,網上搜尋無果,通過查看包裡的類找靠譜的方法,最後終於找到了ContextMapper,可以通過擷取dn來取得組織機構

public List<Person> getAllPersonNames() {      // Regex,為擷取組織機構      String regEx_script = "ou=([^,]*)?";      final Pattern p = Pattern.compile(regEx_script, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);// 修改模式                @SuppressWarnings("unchecked")      List<Person> persons = ldapTemplate.search("o=北京XX局", "(objectClass=Person)",              new ContextMapper() {                  @Override                public Object mapFromContext(Object arg0) {                      DirContextAdapter adapter = (DirContextAdapter)arg0;                      Attributes attrs = adapter.getAttributes();                      Person bean = new Person();                      // 取值                      Attribute fullName = attrs.get("cn");                      Attribute mail = attrs.get("mail");                      Attribute title = attrs.get("title");                      Name dn = adapter.getDn();                      String org = "";                      // 取組織機構                      if(dn != null){                          Matcher m = p.matcher(dn.toString());                          MatchResult mr = null;                          // 擷取匹配                          while (m.find()) {                              mr = m.toMatchResult();                              org = org + mr.group(1) + "/";                          }                      }                      // 給對象賦值                      try {                          bean.setFullName(fullName == null ? "" : fullName.get().toString());                          // ldap中有的郵箱有尾碼有的沒有                          String mailStr = mail == null ? "" : mail.get().toString();                                bean.setMail(mailStr);                          bean.setTitle(title == null ? "" : title.get().toString());                          bean.setOrg(org.equals("") ? "" : org.substring(0, org.length() - 1));                      } catch (NamingException e) {                          e.printStackTrace();                      }                      return bean;                  }              });      return persons;  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.