I have a user that has logged in through AD and now I want to get some of their information. Here's a sample test endpoint I am playing with:
@RequestMapping(value={"/secure/test"}, method=RequestMethod.GET)
public ResponseEntity<?> getSecureTest(HttpServletRequest request) {
String str = "Test Response";
request.getSession().setAttribute("testVar", "SessionVariable");
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
str = str + "\n -- " + currentUserName + "\n\n";
str = str + userDetails.getUsername(); // matches authentication.getName()
return new ResponseEntity<>(str, HttpStatus.OK);
} else {
str = str + "failed auth";
return new ResponseEntity<>(str, HttpStatus.UNAUTHORIZED);
}
}
I can get the authentication and from that, a UserDetails, but the implementation that comes out I believe is a LdapUserDetailsImpl
This doesn't appear to have any methods to like "getAttribute" or whatever. If I wanted to get one of the AD attributes such as "mail" or "telephoneNumber", how can I get it?
Edit:
So, just to try to extract the "title" attribute I extended the LdapUserDetailsImpl:
public class CustomUserDetails extends LdapUserDetailsImpl {
private String title;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}
And I extended the LdapUserDetailsMapper:
public class CustomDetailsContextMapper extends LdapUserDetailsMapper {
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
LdapUserDetailsImpl ldapUserDetailsImpl = (LdapUserDetailsImpl) super.mapUserFromContext(ctx, username, authorities);
CustomUserDetails customUserDetails = new CustomUserDetails();
customUserDetails.setTitle(ctx.getStringAttribute("title"));
return customUserDetails;
}
}
In my controller, I try to get this object:
CustomUserDetails userDetails = (CustomUserDetails) authentication.getPrincipal();
but this gives me a casting error... What am I missing?
WebSecurityConfigurerAdapter has this stuff in it:
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
authManagerBuilder.userDetailsService(userDetailsService());
}
@Bean
public AuthenticationManager authenticationManager() {
return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProviderES()));
}
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(ldapdomain, ldapurl);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}