I am using Spring Security to authenticate a user on an Active Directory server. The UserUserContext user interface is also introduced in the ldapAuthenticationProvider bean to provide access to additional LDAP attributes. Everything works well. I have no problem with what I want to get from an authenticated user.
The problem is that I want to get some attributes, especially the email address, from the Active Directory server from a user other than the user who is logged in. Is it possible to achieve this using what I already have or is my only option to use a completely separate method to access the LDAP attributes of another user?
[edit] Configuration follows
security config.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:sec="http://www.springframework.org/schema/security" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://xxxx.xxxx.xxx:389" /> <property name="base" value="dc=corp,dc=global,dc=xxxxx,dc=com" /> <property name="userDn" value="CN=lna.authquery,OU=LDAPGroups,OU=NorthAmerica,DC=corp,DC=global,DC=xxxxx,DC=com" /> <property name="password" value="xxxxxxx" /> <property name="pooled" value="true" /> <property name="referral" value="follow" /> </bean> <bean id="ldapAuthenticationProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider" > <constructor-arg> <bean class="org.springframework.security.ldap.authentication.BindAuthenticator"> <constructor-arg ref="contextSource" /> <property name="userSearch"> <bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"> <constructor-arg index="0" value="" /> <constructor-arg index="1" value="(sAMAccountName={0})" /> <constructor-arg index="2" ref="contextSource" /> </bean> </property> </bean> </constructor-arg> <constructor-arg> <bean class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator"> <constructor-arg ref="contextSource" /> <constructor-arg value="" /> <property name="groupSearchFilter" value="(member={0})" /> <property name="searchSubtree" value="true" /> </bean> </constructor-arg> <property name="userDetailsContextMapper"> <bean class="net.xxxx.xxxxx.utilities.CustomUserDetailsContextMapper" /> </property> </bean> <bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager"> <constructor-arg> <list> <ref local="ldapAuthenticationProvider" /> </list> </constructor-arg> </bean> <sec:http pattern="/css/**" security="none"/> <sec:http pattern="/images/**" security="none"/> <sec:http auto-config="true" authentication-manager-ref="authenticationManager" > <sec:intercept-url pattern="/login.jsp*" requires-channel="https" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <sec:intercept-url pattern="/**" requires-channel="https" access="IS_AUTHENTICATED_FULLY"/> <sec:form-login login-page='/login.jsp' default-target-url="/home.html" authentication-failure-url="/login.jsp" /> </sec:http>
CustomeUserDetails.java
package net.xxxx.xxxx.utilities; import java.util.Collection; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.User; public class CustomUserDetails extends User { private static final long serialVersionUID = 1416132138315457558L;
CustomUserDetailsContextMapper.java
package net.xxxx.xxxxx.utilities; import java.util.Collection; public class CustomUserDetailsContextMapper implements UserDetailsContextMapper { public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) { String fullname = ""; String email = ""; String title = ""; Attributes attributes = ctx.getAttributes(); try { fullname = (String) attributes.get("displayName").get(); email = (String) attributes.get("mail").get(); title = (String) attributes.get("title").get(); } catch (NamingException e) { e.printStackTrace(); } CustomUserDetails details = new CustomUserDetails(username, "", true, true, true, true, authorities, fullname, email, title); return details; } public void mapUserToContext(UserDetails user, DirContextAdapter ctx) { } }