尝试访问LDAP数据时出现ConverterNotFoundException

时间:2018-05-09 07:27:03

标签: spring-boot spring-ldap

我正在尝试从LDAP服务器获取数据。直到现在,我已成功连接到服务器并获取所有记录的列表。但是当我尝试获取单个记录时,我收到以下错误

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [javax.naming.Name]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convertId(ReflectionRepositoryInvoker.java:289) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindById(CrudRepositoryInvoker.java:92) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.lambda$invokeFindById$2(UnwrappingRepositoryInvokerFactory.java:95) ~[spring-data-rest-core-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
...

我有这样的LDAP配置设置

@Configuration
@EnableLdapRepositories(basePackages = "com.sayak.repository.ldap")
public class LdapConfig {
    @Value("${ldap.urls}")
    private String ldapUrls;

    @Value("${ldap.base.dn}")
    private String ldapBaseDn;

    @Value("${ldap.username}")
    private String ldapSecurityPrincipal;

    @Value("${ldap.password}")
    private String ldapPrincipalPassword;

    @Bean
    ContextSource contextSource() {
        LdapContextSource ldapContextSource = new LdapContextSource();
        ldapContextSource.setUrl(ldapUrls);
        ldapContextSource.setBase(ldapBaseDn);
        ldapContextSource.setUserDn(ldapSecurityPrincipal);
        ldapContextSource.setPassword(ldapPrincipalPassword);
        return ldapContextSource;
    }

    @Bean
    LdapTemplate ldapTemplate(ContextSource contextSource) {
        return new LdapTemplate(contextSource);
    }
}

LDAP数据的条目是

@Entry(objectClasses = {"inetOrgPerson", "organizationalPerson", "person", "top"})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LdapUser {
    @Id
    private Name distinguishedName;

    @Attribute(name = "cn")
    private String commonName;

    @Attribute(name = "sn")
    private String surname;

    @Attribute(name = "givenName")
    private String givenName;

    @Attribute(name = "ou")
    private String organisationalUnit;

    @Attribute(name = "uid")
    private String userId;

    @Attribute(name = "mail")
    private String email;

    public LdapUser(LdapUser other) {
        this.distinguishedName = other.distinguishedName;
        this.commonName = other.commonName;
        this.surname = other.surname;
        this.givenName = other.givenName;
        this.organisationalUnit = other.organisationalUnit;
        this.userId = other.userId;
        this.email = other.email;
    }
}

我的存储库设置为

@Repository
public interface LdapUserRepository extends LdapRepository<LdapUser> {
    LdapUser findByCommonName(String commonName);
    List<LdapUser> findByCommonNameContainingIgnoreCase(String commonName);
}

当我在/ldapUsers发出GET请求时,我会得到一个有效的回复,例如

{
    "_embedded": {
        "ldapUsers": [
            {
                "commonName": "Katherine Ito",
                "surname": "Ito",
                "givenName": "Katherine",
                "organisationalUnit": "Peons",
                "userId": "ItoK",
                "email": "ItoK@ns-mail3.com",
                "_links": {
                    "self": {
                        "href": "http://localhost:6332/api/v1/ldapUsers/cn=Katherine%20Ito,ou=Peons"
                    },
                    "ldapUser": {
                        "href": "http://localhost:6332/api/v1/ldapUsers/cn=Katherine%20Ito,ou=Peons"
                    }
                }
            },
            ...
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:6332/api/v1/ldapUsers"
        },
        "profile": {
            "href": "http://localhost:6332/api/v1/profile/ldapUsers"
        },
        "search": {
            "href": "http://localhost:6332/api/v1/ldapUsers/search"
        }
    }
}

但是当我向/ldapUsers/cn=Katherine%20Ito,ou=Peons发出GET请求时(第一个响应中的self.href显而易见,它会抛出错误。

我知道ConversionServiceConverterManager.StringToNameConverter的存在,但由于某种原因,它似乎没有被触发。有什么指针吗?

0 个答案:

没有答案