Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void changeUserPassword(String username, String oldPassword, String newPassword) {
- byte[] encodedNewPassword = encode(newPassword);
- ModificationItem[] mods = new ModificationItem[1];
- mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("UnicodePwd", encodedNewPassword));
- String base = "OU=someOtherOU,OU=someOu,DC=domain,DC=dom";
- String userCN = "CN="+username;
- String userDN = userCN + "," + base;
- LdapContextSource contextSource = new LdapContextSource();
- contextSource.setUrl("ldap://my-domain.com:389");
- contextSource.setBase(base);
- contextSource.setUserDn(userDN);
- contextSource.setPassword(oldPassword);
- contextSource.afterPropertiesSet();
- LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
- ldapTemplate.modifyAttributes(UserCN, mods);
- }
- private byte[] encode(String password) {
- String quotedPassword = """ + password + """;
- char[] unicodePassword = quotedPassword.toCharArray();
- byte[] passwordArray = new byte[unicodePassword.length * 2];
- for (int i = 0; i < unicodePassword.length; i++) {
- passwordArray[i * 2 + 1] = (byte) (unicodePassword[i] >>> 8);
- passwordArray[i * 2] = (byte) (unicodePassword[i] & 0xff);
- }
- return passwordArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement