Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. public void changeUserPassword(String username, String oldPassword, String newPassword) {
  2. byte[] encodedNewPassword = encode(newPassword);
  3. ModificationItem[] mods = new ModificationItem[1];
  4. mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("UnicodePwd", encodedNewPassword));
  5.  
  6. String base = "OU=someOtherOU,OU=someOu,DC=domain,DC=dom";
  7. String userCN = "CN="+username;
  8. String userDN = userCN + "," + base;
  9. LdapContextSource contextSource = new LdapContextSource();
  10. contextSource.setUrl("ldap://my-domain.com:389");
  11. contextSource.setBase(base);
  12. contextSource.setUserDn(userDN);
  13. contextSource.setPassword(oldPassword);
  14. contextSource.afterPropertiesSet();
  15.  
  16. LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
  17.  
  18. ldapTemplate.modifyAttributes(UserCN, mods);
  19. }
  20.  
  21. private byte[] encode(String password) {
  22. String quotedPassword = """ + password + """;
  23. char[] unicodePassword = quotedPassword.toCharArray();
  24. byte[] passwordArray = new byte[unicodePassword.length * 2];
  25. for (int i = 0; i < unicodePassword.length; i++) {
  26. passwordArray[i * 2 + 1] = (byte) (unicodePassword[i] >>> 8);
  27. passwordArray[i * 2] = (byte) (unicodePassword[i] & 0xff);
  28. }
  29. return passwordArray;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement