2016-03-16 4 views
0

Я уже подключаюсь к AD с весенней загрузки для целей входа, но я не могу выполнять поиск. Моя конфигурацияВесенняя загрузка Active Directory/LDAP-соединение SOLVED

@Configuration 
public class LdapTemplateConfig { 

    @Bean 
    public LdapTemplate ldapTemplate() { 
     LdapTemplate ldapTemplate = new LdapTemplate(ldapContextSource()); 
     return ldapTemplate; 
    } 

    @Bean 
    public LdapContextSource ldapContextSource() { 

     String url = "ldap://127.0.0.1:389"; 
     String base = "DC=demo1,DC=demo2,DC=demo3,DC=demo4"; 

     LdapContextSource ldapContextSource = new LdapContextSource(); 
     ldapContextSource.setUrl(url); 
     ldapContextSource.setUserDn(
       "CN=User Name,OU=Common Users OU,OU=RDP enabled Users OU,OU=Operator Users OU,OU=Admin Users OU,DC=demo1,DC=demo2,DC=demo3,DC=demo4"); 
     ldapContextSource.setPassword("password"); 
    // ldapContextSource.setReferral("follow"); 
     ldapContextSource.afterPropertiesSet(); 
     return ldapContextSource; 
    } 

} 



@Service 
public class LDAPServiceImpl { 

    @Autowired 
    private LdapTemplate ldapTemplate; 

    public void getUserDetails(String userName) { 
     if (null != ldapTemplate) { 
      List<String> vals = ldapTemplate.search(query().where("objectclass").is("person"), 
        new AttributesMapper<String>() { 
         @Override 
         public String mapFromAttributes(Attributes attributes) throws NamingException { 
          return attributes.get("sAMAccountName").get().toString(); 
         } 
        }); 
      for (String s : vals) { 
       log.info("attr : " + s); 
      } 
     } else { 
      log.info("Templates is null"); 
     } 
    } 
} 

Так что, когда я вызываю функцию getUserDetails() от контроллера возвращает «Шаблоны является нулевым» инъекция зависимостей

+0

попробуйте добавить геттер и сеттер для ldapTemplate –

+0

Вы установили 'component-scan' правильно? – Jens

+0

Я пробовал геттер и сеттер, но он не работает. Я использую проект с https://jhipster.github.io/, а в Application.class есть аннотация @ComponentScan. –

ответ

0

Я решил проблему спасибо всем за помощь

@Configuration 
public class LdapTemplateConfig { 

    private final Logger log = LoggerFactory.getLogger(LdapTemplateConfig.class); 

    @Bean(name = "ldapTemplate") 
    // @Scope("singleton") 
    public LdapTemplate ldapTemplate() { 
     LdapTemplate ldapTemplate = new LdapTemplate(ldapContextSource()); 
     return ldapTemplate; 
    } 

    @Bean(name = "contextSource") 
    // @Scope("singleton") 
    public LdapContextSource ldapContextSource() { 

     String url = "ldap://127.0.0.1:389"; 
     String base = "DC=intranet,DC=demo,DC=com"; 

     if (isConfigurationValid(url, base)) { 
      LdapContextSource ldapContextSource = new LdapContextSource(); 
      ldapContextSource.setUrl(url); 
      ldapContextSource.setBase(base); 
      ldapContextSource.setUserDn(
        "CN=Test User,OU=Common Users OU,OU=RDP enabled Users OU,DC=intranet,DC=demo,DC=com"); 
      ldapContextSource.setPassword("password"); 
      ldapContextSource.setReferral("follow"); 
      // lcs.setPooled(false); 
      // lcs.setDirObjectFactory(DefaultDirObjectFactory.class); 
      ldapContextSource.afterPropertiesSet(); 
      return ldapContextSource; 
     } 
     return null; 
    } 

    public boolean isConfigurationValid(String url, String base) { 
     if ((url == null) || url.isEmpty() || (base == null) || base.isEmpty()) { 
      log.error("Warning! Your LDAP server is not configured."); 
      log.info("Did you configure your LDAP settings in your application.yml?"); 
      return false; 
     } else { 
      return true; 
     } 
    } 
} 




@Service 
public class LDAPServiceImpl { 

    private final Logger log = LoggerFactory.getLogger(LDAPServiceImpl.class); 

    @Autowired 
    LdapTemplate ldapTemplate; 

    public User getUserDetails(String userName) { 
     AndFilter filter = new AndFilter(); 
     filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("employeeID", "700335")); 
     List<User> users = ldapTemplate.search("", filter.encode(), new UaserAttributesMapper()); 
     if (!users.isEmpty()) { 
      return users.get(0); 
     } 
     return null; 

     // List<User> list = 
     // ldapTemplate.search(query().where("sAMAccountName").is("a.keskempes"), 
     // new UserAttributesMapper()); 
     // if ((list != null) && !list.isEmpty()) { 
     // return list.get(0); 
     // } 
     // return null; 

    } 

    private class UaserAttributesMapper implements AttributesMapper<User> { 

     @Override 
     public User mapFromAttributes(Attributes attributes) throws NamingException { 
      User user; 
      if (attributes == null) { 
       return null; 
      } 
      user = new User(); 

      if (attributes.get("objectclass") != null) { 
       user.setObjectclass(attributes.get("objectclass").get().toString()); 
      } 
      if (attributes.get("distinguishedname") != null) { 
       user.setDistinguishedname(attributes.get("distinguishedname").get().toString()); 
      } 
      if (attributes.get("userPassword") != null) { 
       user.setUserPassword(attributes.get("userPassword").get().toString()); 
      } 
      if (attributes.get("cn") != null) { 
       user.setCn(attributes.get("cn").get().toString()); 
      } 
      if (attributes.get("telephoneNumber") != null) { 
       user.setTelephoneNumber(attributes.get("telephoneNumber").get().toString()); 
      } 
      // if (attributes.get("lastlogoff") != null) { 
      // // user.setLastlogoff(DateTimeFormat.forPattern("yyyy-MM-dd 
      // // HH:mm:ss") 
      // // 
      // .parseDateTime(attributes.get("lastlogoff").get().toString())); 
      // DateTimeFormatter formatter = 
      // DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"); 
      // DateTime dt = 
      // formatter.parseDateTime(attributes.get("lastlogoff").get().toString()); 
      // user.setLastlogoff(new DateTime(
      // 
      // dt 
      // 
      //)); 
      // } 
      if (attributes.get("userprincipalname") != null) { 
       user.setUserprincipalname(attributes.get("userprincipalname").get().toString()); 
      } 
      if (attributes.get("department") != null) { 
       user.setDepartment(attributes.get("department").get().toString()); 
      } 
      if (attributes.get("company") != null) { 
       user.setCompany(attributes.get("company").get().toString()); 
      } 
      if (attributes.get("mail") != null) { 
       user.setMail(attributes.get("mail").get().toString()); 
      } 
      if (attributes.get("streetAddress") != null) { 
       user.setStreetAddress(attributes.get("streetAddress").get().toString()); 
      } 
      if (attributes.get("st") != null) { 
       user.setSt(attributes.get("st").get().toString()); 
      } 
      if (attributes.get("postalCode") != null) { 
       user.setPostalCode(attributes.get("postalCode").get().toString()); 
      } 
      if (attributes.get("l") != null) { 
       user.setL(attributes.get("l").get().toString()); 
      } 
      if (attributes.get("description") != null) { 
       user.setDescription(attributes.get("description").get().toString()); 
      } 
      if (attributes.get("c") != null) { 
       user.setC(attributes.get("c").get().toString()); 
      } 
      if (attributes.get("countryCode") != null) { 
       user.setCountryCode(attributes.get("countryCode").get().toString()); 
      } 
      if (attributes.get("cn") != null) { 
       user.setCn(attributes.get("cn").get().toString()); 
      } 
      if (attributes.get("sn") != null) { 
       user.setSn(attributes.get("sn").get().toString()); 
      } 
      if (attributes.get("employeeID") != null) { 
       user.setEmployeeId(attributes.get("employeeID").get().toString()); 
      } 
      if (attributes.get("lastLogon") != null) { 
       // user.setLastLogon(DateTimeFormat.forPattern("yyyy-MM-dd 
       // HH:mm:ss")/* 
       // .parseDateTime(attributes.get("lastLogon").get().toString()));*/ 

       DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"); 
       DateTime dt = formatter.parseDateTime(attributes.get("lastLogon").get().toString()); 
       user.setLastLogon(new DateTime(

         dt 

       )); 
      } 
      if (attributes.get("memberof") != null) { 
       user.setMemberof(attributes.get("memberof").get().toString()); 
      } 
      if (attributes.get("givenname") != null) { 
       user.setGivenname(attributes.get("givenname").get().toString()); 
      } 
      if (attributes.get("logoncount") != null) { 
       user.setLogoncount(attributes.get("logoncount").get().toString()); 
      } 
      if (attributes.get("displayName") != null) { 
       user.setDisplayname(attributes.get("displayName").get().toString()); 
      } 

      return user; 
     } 
    } 

} 

и в контроллер я поставил

@Autowired 
    private LDAPServiceImpl lDAPServiceImpl; 
com.ppc.ptol2.service.impl.User find = lDAPServiceImpl.getUserDetails("sgad"); 

как вы можете видеть решение работает отлично, но нуждается в некоторых улучшениях

+0

Не могли бы вы отметить его как принятый, если он работает? – kamaci

0

Spring может быть сделана с помощью конструкторы или сеттеров.

@Autowired 
    private LdapTemplate ldapTemplate; 

Ваш класс LDAPServiceImpl не имеет конструктор, LdapTemplate в качестве аргумента & ldapTemplate объявлен как частные. Таким образом, инжектор конструктора & инжектор установки невозможен. Чтобы использовать сеттер инъекции, там должен быть метод установки для ldapTemplate так:

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

Да, я попробовал решение, которое вы предлагаете, но с той же ошибкой, что и ldapTemplate, является нулевым –

+0

. Отправьте код, который вызывает метод getUserDetails, в подробностях сканирования служб и компонентов. –

Смежные вопросы