How to change AEM User password in java

Qamar_khan

09-04-2019

Hello All,

               I need help to how to change the password of existing user AEM. I am implementing it in my java bundle package. So far I have done two ways

***********************************************************************************************

1.

            ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);   

            UserManager userManager = resourceResolver.adaptTo(UserManager.class);

            Session sessionrr = resourceResolver.adaptTo(Session.class);

            User user = (User) userManager.getAuthorizable(UserName);

             user.changePassword(NewPassword);

               if (!userManager.isAutoSave()) {

                    sessionrr.save();

            }

**********************************************************************************************

2.

          JackrabbitSession session1 = (JackrabbitSession) resourceResolver.adaptTo(javax.jcr.Session.class);   

          UserManager userManager = session1.getUserManager();   

          User user = (User) userManager.getAuthorizable(UserName);

          user.changePassword(NewPassword.toString());

          session1.save();

***************************************************************************************************

Issue:

Now with above 2 ways I am able to successfully changing the normal password But I am getting the issue in case if password containing special characters

like a password!@#.

ERROR which I am getting  as

org.apache.sling.auth.core.impl.SlingAuthenticator handleLoginFailure: Unable to authenticate null: UserId/Password mismatch.

403

Can someone help me with this?

Thanks

Qamar

Accepted Solutions (1)

Accepted Solutions (1)

Qamar_khan

09-04-2019

I would like to thank both of you people for the quick reply.

I have successfully resolved the issue with the help of below Adobe help link.

Adobe Experience Manager Help | Posting Special Characters to Adobe Experience Manager

The main cause is to encode/decode special characters in AEM.

Solution:

Encode -> While setting a password

String encodedString = URLEncoder.encode(passwordspecialchars, "UTF-8");

Decode -> Decode existing password while checking from AEM

String pwd = java.net.URLDecoder.decode(request.getParameter("password"), "UTF-8");

Thanks

Qamar

Answers (2)

Answers (2)

Prince_Shivhare

09-04-2019

Could you please check this:

Why am I getting the error SlingAuthenticator handleLoginFailure

There could be a couple of reasons causing this issue. Kindly check for the below details:

1. Check to see if the user is correctly mapped in AEM.
2. See if correct Active Directory attributes are mapped in AEM.
3. See if it is happening for one user or all the users.
4. See the crx/quickstart logs for any entries.

jbrar

Employee

09-04-2019

The official user API documentation does not specify any limitation related to special characters. Can you try setup debug logging and check which steps is failing?

Also, If there are any issues with the "changePassword" method, it will throw a "RepositoryException" not the one you shared.

For testing purpose, can you try using "admin" session and check if that works?