PasswordPolicy

This commit is contained in:
Crystal.Sea
2020-08-26 19:56:31 +08:00
parent 05a517a7a0
commit 6058096896
15 changed files with 286 additions and 69 deletions

View File

@@ -39,6 +39,10 @@ public class PasswordGen {
public PasswordGen() {
length = DEFAULT_LENGTH;
}
public PasswordGen(int length) {
this.length = length;
}
public String gen() {
this.length = DEFAULT_LENGTH;

View File

@@ -104,7 +104,23 @@ public class PasswordPolicy extends JpaBaseDomain implements java.io.Serializabl
* not include password list
*/
@Column
private String simplePasswords;
private int history;
@Column
private int dictionary;
@Column
private int alphabetical;
@Column
private int numerical;
@Column
private int qwerty;
@Column
private int occurances;
/**
* @return the minLength
@@ -260,18 +276,57 @@ public class PasswordPolicy extends JpaBaseDomain implements java.io.Serializabl
this.username = username;
}
/**
* @return the simplePasswords
*/
public String getSimplePasswords() {
return simplePasswords;
public int getHistory() {
return history;
}
/**
* @param simplePasswords the simplePasswords to set
*/
public void setSimplePasswords(String simplePasswords) {
this.simplePasswords = simplePasswords;
public void setHistory(int history) {
this.history = history;
}
public int getDictionary() {
return dictionary;
}
public void setDictionary(int dictionary) {
this.dictionary = dictionary;
}
public int getAlphabetical() {
return alphabetical;
}
public void setAlphabetical(int alphabetical) {
this.alphabetical = alphabetical;
}
public int getNumerical() {
return numerical;
}
public void setNumerical(int numerical) {
this.numerical = numerical;
}
public int getQwerty() {
return qwerty;
}
public void setQwerty(int qwerty) {
this.qwerty = qwerty;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public int getOccurances() {
return occurances;
}
public void setOccurances(int occurances) {
this.occurances = occurances;
}
public void check(String username, String newPassword, String oldPassword) throws PasswordPolicyException {
@@ -319,17 +374,14 @@ public class PasswordPolicy extends JpaBaseDomain implements java.io.Serializabl
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "PasswordPolicy [minLength=" + minLength + ", maxLength=" + maxLength + ", lowerCase=" + lowerCase
+ ", upperCase=" + upperCase + ", digits=" + digits + ", specialChar=" + specialChar + ", attempts="
+ attempts + ", duration=" + duration + ", expiration=" + expiration + ", username=" + username
+ ", simplePasswords=" + simplePasswords + "]";
return "PasswordPolicy [id=" + id + ", minLength=" + minLength + ", maxLength=" + maxLength + ", lowerCase="
+ lowerCase + ", upperCase=" + upperCase + ", digits=" + digits + ", specialChar=" + specialChar
+ ", attempts=" + attempts + ", duration=" + duration + ", expiration=" + expiration + ", username="
+ username + ", history=" + history + ", dictionary=" + dictionary + ", alphabetical=" + alphabetical
+ ", numerical=" + numerical + ", qwerty=" + qwerty + "]";
}
}

View File

@@ -38,7 +38,12 @@ public class PasswordPolicyRowMapper implements RowMapper<PasswordPolicy> {
passwordPolicy.setDuration(rs.getInt("DURATION"));
passwordPolicy.setExpiration(rs.getInt("EXPIRATION"));
passwordPolicy.setUsername(rs.getInt("USERNAME"));
passwordPolicy.setSimplePasswords(rs.getString("SIMPLEPASSWORDS"));
passwordPolicy.setHistory(rs.getInt("HISTORY"));
passwordPolicy.setDictionary(rs.getInt("DICTIONARY"));
passwordPolicy.setAlphabetical(rs.getInt("ALPHABETICAL"));
passwordPolicy.setNumerical(rs.getInt("NUMERICAL"));
passwordPolicy.setQwerty(rs.getInt("QWERTY"));
passwordPolicy.setOccurances(rs.getInt("OCCURANCES"));
return passwordPolicy;
}

View File

@@ -15,11 +15,13 @@ import org.maxkey.constants.ConstantsPasswordSetType;
import org.maxkey.constants.ConstantsProperties;
import org.maxkey.constants.ConstantsStatus;
import org.maxkey.constants.ConstantsTimeInterval;
import org.maxkey.crypto.password.PasswordGen;
import org.maxkey.domain.PasswordPolicy;
import org.maxkey.domain.UserInfo;
import org.maxkey.util.StringUtils;
import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext;
import org.passay.CharacterOccurrencesRule;
import org.passay.CharacterRule;
import org.passay.DictionaryRule;
import org.passay.EnglishCharacterData;
@@ -64,7 +66,7 @@ public class PasswordPolicyValidator {
private static final String PASSWORD_POLICY_KEY = "PASSWORD_POLICY_KEY";
private static final String LOCK_USER_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET ISLOCKED = ? , UNLOCKTIME = ? WHERE ID = ?";
private static final String PASSWORD_POLICY_SELECT_STATEMENT = "SELECT ID,MINLENGTH,MAXLENGTH,LOWERCASE,UPPERCASE,DIGITS,SPECIALCHAR,ATTEMPTS,DURATION,EXPIRATION,USERNAME,SIMPLEPASSWORDS FROM MXK_PASSWORD_POLICY ";
private static final String PASSWORD_POLICY_SELECT_STATEMENT = "SELECT * FROM MXK_PASSWORD_POLICY ";
private static final String UNLOCK_USER_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET ISLOCKED = ? , UNLOCKTIME = ? WHERE ID = ?";
@@ -97,20 +99,28 @@ public class PasswordPolicyValidator {
if(passwordPolicy.getUpperCase()>0) {
passwordPolicyRuleList.add(new CharacterRule(EnglishCharacterData.UpperCase, passwordPolicy.getUpperCase()));
}
if(passwordPolicy.getLowerCase()>0) {
passwordPolicyRuleList.add(new CharacterRule(EnglishCharacterData.LowerCase, passwordPolicy.getLowerCase()));
}
if(passwordPolicy.getDigits()>0) {
passwordPolicyRuleList.add(new CharacterRule(EnglishCharacterData.Digit, passwordPolicy.getDigits()));
}
if(passwordPolicy.getSpecialChar()>0) {
passwordPolicyRuleList.add(new CharacterRule(EnglishCharacterData.Special, passwordPolicy.getSpecialChar()));
}
if(passwordPolicy.getUsername()>0) {
passwordPolicyRuleList.add(new UsernameRule());
}
if(passwordPolicy.getSimplePasswords().length()>0 ) {
if(passwordPolicy.getOccurances()>0) {
passwordPolicyRuleList.add(new CharacterOccurrencesRule(passwordPolicy.getOccurances()));
}
if(passwordPolicy.getDictionary()>0 ) {
try {
ClassPathResource dictFile=
new ClassPathResource(
@@ -201,9 +211,15 @@ public class PasswordPolicyValidator {
);
}
//initial password need change
if(userInfo.getLoginCount()<=0) {
WebContext.getSession().setAttribute(WebConstants.CURRENT_LOGIN_USER_PASSWORD_SET_TYPE,
ConstantsPasswordSetType.INITIAL_PASSWORD);
}
if (userInfo.getPasswordSetType() != ConstantsPasswordSetType.PASSWORD_NORMAL) {
WebContext.getSession().setAttribute(WebConstants.CURRENT_LOGIN_USER_PASSWORD_SET_TYPE,
userInfo.getPasswordSetType());
userInfo.getPasswordSetType());
return true;
} else {
WebContext.getSession().setAttribute(WebConstants.CURRENT_LOGIN_USER_PASSWORD_SET_TYPE,
@@ -232,12 +248,6 @@ public class PasswordPolicyValidator {
}
}
//initial password need change
if(userInfo.getLoginCount()<=0) {
WebContext.getSession().setAttribute(WebConstants.CURRENT_LOGIN_USER_PASSWORD_SET_TYPE,
ConstantsPasswordSetType.INITIAL_PASSWORD);
}
return true;
}
@@ -316,7 +326,23 @@ public class PasswordPolicyValidator {
}
}
public String generateRandomPassword() {
getPasswordPolicy();
PasswordGen passwordGen = new PasswordGen(
Math.round(
(
passwordPolicy.getMaxLength() +
passwordPolicy.getMinLength()
)/2
)
);
return passwordGen.gen(
passwordPolicy.getLowerCase(),
passwordPolicy.getUpperCase(),
passwordPolicy.getDigits(),
passwordPolicy.getSpecialChar());
}
public void setPasswordPolicy(PasswordPolicy passwordPolicy) {
this.passwordPolicy = passwordPolicy;

View File

@@ -16,7 +16,7 @@ public class PasswordPolicyValidatorTest {
passwordPolicy.setUpperCase(2);
passwordPolicy.setSpecialChar(1);
passwordPolicy.setUsername(1);
passwordPolicy.setSimplePasswords("admin,1qaz,2wsx,123456,12345678,1234567890");
passwordPolicy.setDictionary(0);
PasswordPolicyValidator passwordPolicyValidator =new PasswordPolicyValidator();
passwordPolicyValidator.setPasswordPolicy(passwordPolicy);