[英]Spring security Ldap authentication Exception : Not an instance of DirContext
I'm trying to connect to a Ldap server (host by the company, don't have much info about it), using Spring Security, I have this bean:我正在尝试使用 Spring Security 连接到 Ldap 服务器(由公司托管,没有太多关于它的信息),我有这个 bean:
@Override public void configure(AuthenticationManagerBuilder auth) throws Exception { DefaultDirObjectFactory factory = new DefaultDirObjectFactory(); LdapContextSource ldapContextSource = new LdapContextSource(); ldapContextSource.setAnonymousReadOnly(true); ldapContextSource.setUrl("ldap://ldap.company.domain.com:xxxx/dc=company,dc=com"); ldapContextSource.setDirObjectFactory(factory.getClass()); auth.ldapAuthentication().userSearchFilter("uid={0}").contextSource(ldapContextSource); }
But I got this error:但我得到了这个错误:
Caused by: org.springframework.ldap.NotContextException: Not an instance of DirContext;
引起:org.springframework.ldap.NotContextException:不是DirContext的实例; nested exception is javax.naming.NotContextException: Not an instance of DirContext at backend-1.0.0-RC1.war//org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:209) at backend-1.0.0-RC1.war//org.springframework.ldap.core.LdapTemplate.executeWithContext(LdapTemplate.java:824) at backend-1.0.0-RC1.war//org.springframework.ldap.core.LdapTemplate.executeReadOnly(LdapTemplate.java:807) at backend-1.0.0-RC1.war//org.springframework.security.ldap.SpringSecurityLdapTemplate.searchForSingleEntry(SpringSecurityLdapTemplate.java:260) at backend-1.0.0-RC1.war//org.springframework.security.ldap.search.FilterBasedLdapUserSearch.searchForUser(FilterBasedLdapUserSearch.java:100) at backend-1.0.0-RC1.war//org.springframework.security.ldap.authentication.BindAuthenticator.authenticate(BindAuthenticator.java:86) at backend-1.0.0-RC1.war//org.springframework.security.ldap.authentication.LdapAuthenticationProvider.doAuthentication(LdapAuthenticationProvider.java:174) ... 101 mo
嵌套异常是 javax.naming.NotContextException: Not an instance of DirContext at backend-1.0.0-RC1.war//org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:209) at backend-1.0.0 -RC1.war//org.springframework.ldap.core.LdapTemplate.executeWithContext(LdapTemplate.java:824) at backend-1.0.0-RC1.war//org.springframework.ldap.core.LdapTemplate.executeReadOnly(LdapTemplate. java:807) 在 backend-1.0.0-RC1.war//org.springframework.security.ldap.SpringSecurityLdapTemplate.searchForSingleEntry(SpringSecurityLdapTemplate.java:260) 在 backend-1.0.0-RC1.war//org.springframework。 security.ldap.search.FilterBasedLdapUserSearch.searchForUser(FilterBasedLdapUserSearch.java:100) at backend-1.0.0-RC1.war//org.springframework.security.ldap.authentication.BindAuthenticator.authenticate(BindAuthenticator.java:86) 在后端-1.0.0-RC1.war//org.springframework.security.ldap.authentication.LdapAuthenticationProvider.doAuthentication(LdapAuthenticationProvider.java:174) ... 101 个月re Caused by: javax.naming.NotContextException: Not an instance of DirContext at java.naming/javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(InitialDirContext.java:154) at java.naming/javax.naming.directory.InitialDirContext.search(InitialDirContext.java:326) at java.naming/javax.naming.directory.InitialDirContext.search(InitialDirContext.java:326) at backend-1.0.0-RC1.war//org.springframework.security.ldap.SpringSecurityLdapTemplate.searchForSingleEntryInternal(SpringSecurityLdapTemplate.java:271) at backend-1.0.0-RC1.war//org.springframework.security.ldap.SpringSecurityLdapTemplate.lambda$searchForSingleEntry$3(SpringSecurityLdapTemplate.java:260) at backend-1.0.0-RC1.war//org.springframework.ldap.core.LdapTemplate.executeWithContext(LdapTemplate.java:821) ... 106 more
重新引起:javax.naming.NotContextException:不是 java.naming/javax.naming.directory.InitialDirContext.getURLOrDefaultInitDirCtx(InitialDirContext.java:154) 处的 DirContext 实例 java.naming/javax.naming.directory.InitialDirContext.search (InitialDirContext.java:326) 在 java.naming/javax.naming.directory.InitialDirContext.search(InitialDirContext.java:326) 在 backend-1.0.0-RC1.war//org.springframework.security.ldap.SpringSecurityLdapTemplate。 searchForSingleEntryInternal(SpringSecurityLdapTemplate.java:271) 在 backend-1.0.0-RC1.war//org.springframework.security.ldap.SpringSecurityLdapTemplate.lambda$searchForSingleEntry$3(SpringSecurityLdapTemplate.java:260) 在 backend-1.0.0-RC1。战争//org.springframework.ldap.core.LdapTemplate.executeWithContext(LdapTemplate.java:821) ... 106 更多
when doing:做的时候:
AuthenticationManager.authenticate(authenticateToken)
AuthenticationManager.authenticate(authenticateToken)
Because it is working on another projet, using jndi, I know that Ldap info are correct.因为它正在另一个项目上工作,使用 jndi,我知道 Ldap 信息是正确的。
Edit: I tried to add:编辑:我试图添加:
Map<String, Object> baseEnvironmentProperties = new HashMap<String, Object>(); baseEnvironmentProperties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
But still get the same error.但仍然得到同样的错误。
Do you have any idea why I got this error?你知道我为什么会收到这个错误吗?
I have found a working solution using a LdapAuthenticationProvider:我找到了一个使用 LdapAuthenticationProvider 的工作解决方案:
@Bean
public AuthenticationProvider ldapAuthenticationProvider() throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(List.of("ldap://ldap.company.domain.com:xxx"),"dc=company,dc=com");
contextSource.setAnonymousReadOnly(true);
contextSource.afterPropertiesSet();
LdapUserSearch ldapUserSearch = new FilterBasedLdapUserSearch("", "uid={0}", contextSource);
BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource);
bindAuthenticator.setUserSearch(ldapUserSearch);
LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator);
return ldapAuthenticationProvider;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.