随机生成8位密码:必须由大写字母、小写字母、数字和特殊符号共同组成。以及检验密码是否由大写字母、小写字母、数字和特殊符号共同组成。
package com.primecredit.util;
import java.util.Random;
/**
* @author hongk
* 随机生成8位密码
*/
public class MakeRandomPasswordUtil {
//随机生成 8 位由大写字母,小写字母,数字和特殊符号组成的密码
public static String makeRandomPassword(){
char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()_-+={}[]/?,.\"<>\\|:;\'`".toCharArray(); //L20170131 update by hong.k
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int x = 0; x < 8; ++x) {
sb.append(charr[random.nextInt(charr.length)]);
}
String randomPassword = sb.toString();
if (randomPassword.matches(".*[a-z]{1,}.*") && randomPassword.matches(".*[A-Z]{1,}.*") && randomPassword.matches(".*\\d{1,}.*") && randomPassword.matches(".*[<>(){}|~!@#$%^&*\\.\'\"`_?-]{1,}.*") ) { //L20170131 update by hong.k
return randomPassword;
}else{
randomPassword = makeRandomPassword();
}
return randomPassword;
}
//验证密码是否由大写字母,小写字母,数字和特殊符号组成
public static boolean checkPassword(String strPass){
if (strPass.matches(".*[a-z]{1,}.*") && strPass.matches(".*[A-Z]{1,}.*") && strPass.matches(".*\\d{1,}.*") && strPass.matches(".*[<>(){}|~!@#$%^&*\\.\'\"`_?-]{1,}.*") ) {//L20170131 update by hong.k
return true;
}
return false;
}
}