LdapCustomAuthenticationProvider extends LdapAuthenticationProvider { protected Principal mergeUsers(LdapUserDetails ldapUser, Principal principalFromDB, String password) { User user; Boolean console = false; if(principalFromDB.getUser() == null){ user = new User(); console = true; }else { user = principalFromDB.getUser(); } user.setDn(ldapUser.getDn()); user.setLdapName(ldapUser.getDn().replace(",dc=autentia,dc=com", "")); user.setLdapPassword(password); user.setActive(ldapUser.isEnabled()); user.setPasswordExpired(checkExpiredPassword(ldapUser.getAttributes())); user.setResetPassword(checkResetPassword(ldapUser.getAttributes())); Principal ret; if(console){ ret = new Principal(999, 999, user.getLdapName(), user.getLdapPassword(), user.isActive(),user.getName(), 999, principalFromDB.getAuthorities()); }else{ ret = new Principal(user, user.getLdapPassword(), principalFromDB.getAuthorities()); } return ret; } LdapCustomAuthenticationProvider(LdapAuthenticator authenticator,
                                            LdapAuthoritiesPopulator authoritiesPopulator); void setUserDetailsService(UserDetailsService userDetailsService);  }
@Test public void shouldMergeDbUserWithLdapCredentialsTest(){ Attribute pwdGraceUseTime = new BasicAttribute("pwdGraceUseTime"); pwdGraceUseTime.add(new Date()); Attributes attributes = new BasicAttributes(); attributes.put(pwdGraceUseTime); when(ldapUserDetails.getAttributes()).thenReturn(attributes); User user = getUserForTest(); final Principal principal = new Principal(user, new GrantedAuthority[]{}); final String ldapPassword = "ldapPassword"; final Principal ldapPrincipal = sut.mergeUsers(ldapUserDetails, principal, ldapPassword); assertThat(ldapPrincipal.getPassword(),is(ldapPassword)); assertThat(ldapPrincipal.getUser().getLdapPassword(),is(ldapPassword)); assertThat(ldapPrincipal.getUser().getPassword(),is(nullValue())); assertThat(ldapPrincipal.getUser().isActive(),is(true)); }
@Test public void shouldMergeDbUserWithLdapCredentialsWhenMigrationTest(){ Attribute pwdGraceUseTime = new BasicAttribute("pwdGraceUseTime"); pwdGraceUseTime.add(new Date()); Attributes attributes = new BasicAttributes(); attributes.put(pwdGraceUseTime); when(ldapUserDetails.getAttributes()).thenReturn(attributes); User user = getUserForTest(); Principal principal = new Principal(user, new GrantedAuthority[]{}); principal.dto = null; final String ldapPassword = "ldapPassword"; final Principal ldapPrincipal = sut.mergeUsers(ldapUserDetails, principal, ldapPassword); assertThat(ldapPrincipal.getId(),is(999)); assertThat(ldapPrincipal.getPassword(),is(ldapPassword)); }