Spring ldap applicationContext.xml кажется неиспользованным - PullRequest
0 голосов
/ 21 сентября 2018

Я пытаюсь найти пользователей с помощью запроса ldap и их информации в списке на основе этой конфигурации.

Вот так выглядит мой класс Java:

public class UserDaoImpl implements UserDao {

private LdapTemplate ldapTemplate = new LdapTemplate();

@Override
public List<User> findAll() {
    return ldapTemplate.findAll(User.class);
}

public List<User> findAllBySRCriteria() {
    LdapQuery ldapQuery = LdapQueryBuilder.query().where("employeeType").is("INTERNAL").and("userAccountControl").is("512").and("accountExpires").is("0");
    return ldapTemplate.search(ldapQuery, USER_CONTEXT_MAPPER);
}

private final static ContextMapper<User> USER_CONTEXT_MAPPER = new AbstractContextMapper<User>() {
    @Override
    public User doMapFromContext(DirContextOperations context) {
        User user = new User();

        ToolBox toolBox = new ToolBox();
        Map<String, String> srRoleMap = toolBox.getSRRoleMap();

        String vorname = toolBox.getSplittedDisplayName(context.getStringAttribute("displayName"))
                .get("Vorname");
        String nachname = toolBox.getSplittedDisplayName(context.getStringAttribute("displayName"))
                .get("Nachname");
        String srRole = toolBox.getRoleFromAttribute(context.getStringAttribute("memberOf"));

        user.setFirstname(vorname);
        user.setSurename(nachname);
        user.setMail(context.getStringAttribute("mail").toLowerCase());
        user.setIsoCode("DE");
        user.setSsoId(context.getStringAttribute("mail"));

        if (!context.getStringAttribute("memberOf").contains("CN=HR_SR_")) {
            user.setRole("HR_SR_Employee");
        } else {
            for (Map.Entry<String, String> entry : srRoleMap.entrySet()) {
                if (entry.getKey().equals(srRole)) {
                    user.setRole(entry.getValue());
                }
            }
        }

        return user;
    }
};

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

А это мое приложениеContext.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:application.properties"/>

<ldap:context-source
        id="contextSource"
        username="${spring.ldap.username}"
        password="${spring.ldap.password}"
        url="${spring.ldap.urls}"
        base="${spring.ldap.base}"/>

<ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource"/>

<bean id="userDao" class="com.breuninger.it.ldapx.dao.UserDaoImpl">
    <property name="ldapTemplate" ref="ldapTemplate"/>
</bean>

У меня естьполучил другой класс, который выполняет метод каждый раз, когда программа начинает получать всю необходимую информацию из ldap и отображать их в JSON.Это выглядит так:

@Component
public class StartupHousekeeper {
    private static final Logger LOGGER = Logger.getLogger(StartupHousekeeper.class.getName());

  @EventListener(ContextRefreshedEvent.class)
    public void contextRefreshEvent() {
        generateSRUsers();
}

private void generateSRUsers() {
    HttpActionController httpActionController = new HttpActionController();
    List<User> ldapUsers = requireNonNull(new UserDaoImpl().findAllBySRCriteria());

    for(User user : ldapUsers) {

        CloseableHttpResponse closeableHttpResponse = null;
        requireNonNull(user);
        try {
            closeableHttpResponse = httpActionController.createSRUser(user);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (closeableHttpResponse.getStatusLine().getStatusCode() == 409) {

            LOGGER.log(Level.WARNING,"Statuscode: " + closeableHttpResponse + ", User " + user.getFirstname() + " "
                    + user.getSurename() + " already exists!");
        }
        if (closeableHttpResponse.getStatusLine().getStatusCode() == 422) {
            LOGGER.log(Level.WARNING,"Status: " + closeableHttpResponse.getStatusLine()
                    + ", User " + user.getFirstname() + " " + user.getSurename());
        }
    }
}}

Всегда, когда я запускаю приложение, я получаю NPE с таким выводом:

java.lang.NullPointerException: null
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:357) ~[spring-ldap-core-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:309) ~[spring-ldap-core-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:642) ~[spring-ldap-core-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:578) ~[spring-ldap-core-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:1617) ~[spring-ldap-core-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at com.breuninger.it.ldapx.dao.UserDaoImpl.findAllBySRCriteria(UserDaoImpl.java:69) ~[classes/:na]
at com.breuninger.it.ldapx.StartupHousekeeper.generateSRUsers(StartupHousekeeper.java:29) ~[classes/:na]
at com.breuninger.it.ldapx.StartupHousekeeper.contextRefreshEvent(StartupHousekeeper.java:24) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:261) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:180) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:142) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:888) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at com.breuninger.it.ldapx.LdapxApplication.main(LdapxApplication.java:10) [classes/:na]
...