spring-ldap基本使用總結

 

正文

之前學習過spring-ldap的官方文檔:2017.4.10 spring-ldap官方文檔學習

現在是對實際使用的spring-ldap及使用過程中遇到的問題,進行總結。

 

1.spring-ldap的pom依賴

複製代碼
         <!-- 添加Spring-ldap-->
         <dependency>
             <groupId>org.springframework.ldap</groupId>
             <artifactId>spring-ldap-core</artifactId>
             <version>2.3.1.RELEASE</version>
         </dependency>
複製代碼

 

下面的網址是spring的ldap頁,裏面有一個quick start。其中顯示了spring-ldap最近的版本,並且勾選不同的版本,會自動生成依賴。

http://projects.spring.io/spring-ldap/

 

2.ldapTemplate的生成--方式1自動注入

2.1 spring-ldap聲明

複製代碼
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:ldap="http://www.springframework.org/schema/ldap"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
複製代碼

 

2.2 ldapTemplate bean

複製代碼
 <context:property-placeholder location="classpath:/ldap.properties" />
<ldap:context-source id="contextSource" password="${sample.ldap.password}" url="${sample.ldap.url}" username="${sample.ldap.userDn}" base="${sample.ldap.base}" />
<ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource"/>
複製代碼

或者

複製代碼
  <bean id="contextSource"    
      class="org.springframework.ldap.core.support.LdapContextSource">    
      <property name="url" value="${sample.ldap.url}" />    
      <property name="base" value="${sample.ldap.base}" />    
      <property name="userDn" value="${sample.ldap.userDn}" />    
      <property name="password" value="${sample.ldap.password}" />  
  </bean>  
    
  <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">    
     <constructor-arg ref="contextSource" />  
 </bean>
複製代碼

 

2.3 使用

 @Autowired
 private LdapTemplate ldapTemplate;

 

3.ldapTemplate的生成--方式2代碼生成

複製代碼
  public LdapTemplate getLdapTemplate(){
          LdapTemplate template = null;
          try {
              LdapContextSource contextSource = new LdapContextSource();
              contextSource.setUrl(getSync_ldap_url());
              contextSource.setBase(getSync_ldap_base());
              contextSource.setUserDn(getSync_ldap_userDn());
              contextSource.setPassword(getSync_ldap_password());
              contextSource.setPooled(false);
             contextSource.afterPropertiesSet(); // important
             template = new LdapTemplate(contextSource);
         }catch (Exception e){
             e.printStackTrace();
         }
         return template;
     }
複製代碼

 

4.spring-ldap的使用

api的使用在上次的文檔中已經學習過了:2017.4.10 spring-ldap官方文檔學習

一些簡單示例:

複製代碼
/**
     * 動態創建dn
     * @param user
     * @return
     */
    private Name buildDn(User user) {
        return LdapNameBuilder.newInstance()
                .add("ou", "Users")
                .add("uid",user.getFdUserid().toString())
                .build();
    }

    /**
     * 在ldap裏更新用戶
     * @param user
     */
    private void updateUser(User user) {
        Name dn = buildDn(user);
        getLdapTemplate().rebind(dn, null, buildAttributes(user));
    }

    /**
     * 在ldap裏刪除用戶
     * @param user
     */
    private void deleteUser(User user) {
        Name dn = buildDn(user);
        getLdapTemplate().unbind(dn);
    }

    /**
     * 在ldap裏創建用戶
     * @param user
     */
    private void createUser(User user) {
        Name dn = buildDn(user);
        getLdapTemplate().bind(dn, null, buildAttributes(user));
    }

    /**
     * 動態構建屬性
     * @param user
     * @return
     */
    private Attributes buildAttributes(User user) {

        Attributes attrs = new BasicAttributes();
        try {
            BasicAttribute objectclass = new BasicAttribute("objectclass");
            objectclass.add("top");
            objectclass.add("posixAccount");
            objectclass.add("inetOrgPerson");
            attrs.put(objectclass);

            attrs.put("userPassword", user.getFdLdapPassword() == null ? "" : user.getFdLdapPassword());
            attrs.put("cn", user.getFdUsername() + "@" + user.getFdTenantName());
            attrs.put("sn", user.getFdUsername() + "@" + user.getFdTenantName());
            attrs.put("displayName", user.getFdDisplayName()== null? "":user.getFdDisplayName());
            attrs.put("homeDirectory", "/root");
            attrs.put("uidNumber", "0");
            attrs.put("uid", user.getFdUserid().toString());
            attrs.put("gidNumber", "0");
        }catch (Exception e){
            e.printStackTrace();
        }
        return attrs;
    }
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章