2

I am having a challenge extracting original user details from the LdapUserDetailsImpl such as the getUsername() returns null

I have the following Java classes

  1. Custom User Class
public class AppUserDetails extends LdapUserDetailsImpl {
   public AppUserDetails() {
        super();
    }
   private String mail; //mail

}
  1. Custom User Details Mapper
    public class AppUserDetailsContextMapper extends LdapUserDetailsMapper {

        public AppUserDetailsContextMapper() {
            super();
        }


        @Override
        public AppUserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {

            UserDetails details = super.mapUserFromContext(ctx, username, authorities);


            String mail = ctx.getStringAttribute("mail");

            AppUserDetails appUserDetails = new AppUserDetails();
            appUserDetails.setMail(mail);

            return appUserDetails;
        }
    }
  1. Web security configuration
 @Configuration
   public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

       @Autowired
       Environment env;

       @Bean
       public UserDetailsContextMapper userDetailsContextMapper() {
           return new AppUserDetailsContextMapper();
       }

       @Bean
       public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
           ActiveDirectoryLdapAuthenticationProvider provider =
                   new ActiveDirectoryLdapAuthenticationProvider(
                           env.getRequiredProperty("spring.ldap.domain"),
                           env.getRequiredProperty("spring.ldap.urls")
                   );
           provider.setConvertSubErrorCodesToExceptions(true);
           provider.setUseAuthenticationRequestCredentials(true);
           provider.setUserDetailsContextMapper(userDetailsContextMapper());
           return provider;
       }

       @Override
       protected void configure(AuthenticationManagerBuilder auth) throws Exception {
           auth
                   .authenticationProvider(activeDirectoryLdapAuthenticationProvider())
                   .userDetailsService(userDetailsService());
       }

       @Bean
       public AuthenticationManager authenticationManager() {
           return new ProviderManager(
                   Collections
                           .singletonList(activeDirectoryLdapAuthenticationProvider())
           );
       }

   }

However I am having a serious challenge trying to getting Custom User Details in the controller:

@Controller
public class HomeController {
    @GetMapping(value = {"/home"})
    public String home(Model model) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication.getPrincipal()); 
        AppUserDetails appUserDetails = (AppUserDetails) authentication.getPrincipal();
        System.out.println(appUserDetails);
        return "home";
    }
}

I am getting a NullPointer exception if I try to any property from the LdapUserDetailsImpl class. However, I am accurately getting all the properties from the AppUserDetails - which extends the LdapUserDetailsImpl class.

Where might I be missing it?

FreddCha
  • 465
  • 2
  • 8
  • 15

0 Answers0