Boat
Boat

Reputation: 535

How to encrypt password while saving Users in Liferay 6.1?

I am trying to consume Liferay's User entity to add users by writing code. The password is not encrypting, so log in is failing. The code is pasting below.

    int countOfUsr = UserLocalServiceUtil.getUsersCount();
    User user = UserLocalServiceUtil.createUser(countOfUsr + 1);
    Date date = new Date();
    user.setCompanyId(countOfUsr + 1);
    user.setCreateDate(date);
    user.setModifiedDate(date);
    user.setDefaultUser(false);
    user.setContactId(countOfUsr + 1);
    user.setPasswordEncrypted(true);
    user.setAgreedToTermsOfUse(true);
    user.setPassword("123");
    user.setPasswordReset(false);
    user.setPasswordModifiedDate(date);
    user.setReminderQueryQuestion("what-is-your-father's-middle-name");
    user.setReminderQueryAnswer("daddad");
    user.setGraceLoginCount(0);
    user.setScreenName("shibu");
    user.setFirstName("SHIBU");
    user.setEmailAddress("[email protected]");
    user.setFacebookId(0);
    user.setOpenId("");
    user.setPortraitId(0);
    user.setLanguageId("en_US");
    user.setTimeZoneId("GMT");
    UserLocalServiceUtil.addUser(user);
  1. How to modify the code to save the user properly?
  2. What to do for pssword encryption?
  3. How to give value for user.setDigest(arg)?

Upvotes: 0

Views: 3374

Answers (2)

TahitianGabriel
TahitianGabriel

Reputation: 762

I think you should use UserLocalServiceUtil.addUser(whole bunch of arguments) instead of UserLocalServiceUtil.addUser(User). It will do what you want : create your user and encrypt the password.

The method signature is :

public User addUser(
        long creatorUserId, long companyId, boolean autoPassword,
        String password1, String password2, boolean autoScreenName,
        String screenName, String emailAddress, long facebookId,
        String openId, Locale locale, String firstName, String middleName,
        String lastName, int prefixId, int suffixId, boolean male,
        int birthdayMonth, int birthdayDay, int birthdayYear,
        String jobTitle, long[] groupIds, long[] organizationIds,
        long[] roleIds, long[] userGroupIds, boolean sendEmail,
        ServiceContext serviceContext)
    throws PortalException, SystemException

Beware : if your password do not validate the password policie, it will throw a UserPasswordException

Upvotes: 0

Vikas V
Vikas V

Reputation: 3186

Specify the encryption algorithm to encrypt passwords in portal-ext.properties file.

For eg.,

passwords.encryption.algorithm=SHA //Check out different algorithms in portal.properties

Upvotes: 1

Related Questions