2013-01-23 93 views
0

我想通過編寫代碼來消耗Liferay的用戶實體來添加用戶。密碼不加密,因此登錄失敗。代碼粘貼在下面。如何在Liferay 6.1中保存用戶時加密密碼?

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. 如何修改代碼以正確保存用戶?
  2. 如何做密碼加密?
  3. 如何給user.setDigest(arg)賦值?

回答

1

指定在portal-ext.properties文件中加密密碼的加密算法。

對於例如,

passwords.encryption.algorithm=SHA //檢查出不同的算法portal.properties

0

我認爲你應該使用UserLocalServiceUtil.addUser(whole bunch of arguments),而不是UserLocalServiceUtil.addUser(User)。 它會做你想做的:創建你的用戶並加密密碼。

的方法簽名是:

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 

請注意:如果您的密碼不驗證密碼policie,它會拋出一個UserPasswordException