如何从 ldap 服务器读取操作属性
Posted
技术标签:
【中文标题】如何从 ldap 服务器读取操作属性【英文标题】:How to read operational attributes from ldap server 【发布时间】:2019-07-07 01:56:13 【问题描述】:我必须从 LDAP 服务器读取操作属性(createTimeStamp、entryUUID 等)。
我尝试实现自己的 UserDetailContextMapper 但没有成功。
目前我的代码如下所示
Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
auth
.ldapAuthentication()
.userSearchFilter("uid=0")
.groupSearchBase("ou=users")
.userDetailsContextMapper(userContextMapper())
.contextSource(contextSource());
@Bean
public LdapContextSource contextSource()
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://ldap.company.pl:389");
contextSource.setBase("dc=company,dc=com");
contextSource.afterPropertiesSet();
return contextSource;
@Bean
public UserDetailsContextMapper userContextMapper()
return new CustomUserDetailContextMapper();
我的 UserDetailContextMapper 实现:
public class CustomUserDetailContextMapper implements UserDetailsContextMapper
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<?
extends GrantedAuthority> authorities)
AutoUser user = new AutoUser();
user.setCreateTimeStamp(ctx.getStringAttribute("createTimestamp"));
user.setUUID(ctx.getStringAttribute("entryUUID"));
user.setEmail(ctx.getStringAttribute("mail"));
return user;
@Override
public void mapUserToContext(UserDetails user, DirContextAdapter ctx)
//default impl
我试过Attributes attributes = ctx.getAttributes(username, new String[] "*", "+");
在mapUserFromContext()
方法中,但如果我通过username
,我会收到NamingException
。
我正在考虑实现 ldapTemplate.lookup(),但我不确定在哪里实现它,我尝试使用 ldapTemplate 执行 userRepo 类,但我通过任何 DN 都得到了NameNotFoundException
。
来自 ctx.getAttributes() 的用于命名异常的 StackTrace
javax.naming.NameNotFoundException
at org.springframework.ldap.core.DirContextAdapter.getAttributes(DirContextAdapter.java:865)
at pl.consileon.training.config.CustomUserDetailContextMapper.mapUserFromContext(CustomUserDetailContextMapper.java:21)
at org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider.authenticate(AbstractLdapAuthenticationProvider.java:87)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:200)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
我可以使用这样的查询从 LDAP 中检索对象:
public Object getAll()
return ldapTemplate.search(query()
.where("entryUUID").is("<HasToPassUUIDFromLDAP"), new PersonAttributesMapper());
但我需要传递完整的属性值,这不是重点,因为我要检索此值,而不是具有此值的对象。
【问题讨论】:
你收到了什么NamingException
?请完整的错误消息和堆栈跟踪。注意getAttributes()
的第一个参数是 RDN,而不是用户名。
使用堆栈跟踪编辑帖子。实际上,如果传递了任何带有名称的名称/字符串,ctx.getAttributes 会抛出异常。
这是一个 RDN。不是 DN。相对专有名称。相对于您调用它的上下文的名称空间。您似乎在这里混合使用了几种 API。
【参考方案1】:
ctx.getAttributes(username, new String[] "*", "+");
用户名 - 根据文档,这应该是 DN 这就是 LDAP 知道您从哪条记录中提取数据的方式
第二个参数,如果设置为null
,则检索所有属性
我相信您的错误被抛出是因为您传递的是 用户名 而不是 专有名称。
/**
* Retrieves selected attributes associated with a named object.
* See @link #getAttributes(Name, String[]) for details.
*
* @param name
* The name of the object from which to retrieve attributes
* @param attrIds
* the identifiers of the attributes to retrieve.
* null indicates that all attributes should be retrieved;
* an empty array indicates that none should be retrieved.
* @return the requested attributes; never null
*
* @throws NamingException if a naming exception is encountered
*/
public Attributes getAttributes(String name, String[] attrIds)
throws NamingException;
【讨论】:
以上是关于如何从 ldap 服务器读取操作属性的主要内容,如果未能解决你的问题,请参考以下文章
Talend - 从 xml 文件中读取 Ldap 的 xml 架构
企业分布式微服务云SpringCloud SpringBoot mybatis (十六)Spring Boot中使用LDAP来统一管理用户信息