此代码中没有包含对密码长度验证,可以在 获取密码处进行验证,代码中的if(StringHandle.isNotEmpty(newPwd) && newPwd.length() > 0)判断可以去掉,在调用该方法钱验证,该方法时验证获取到的密码是否为空、null,以及长度是否大于0,否则遍历会报错,根据代码返回的字符串来判断该如何在前台提示
/**
* 密码复杂性进行验证 可以包含以下( 0~9,a~z,A~Z,(,),-,.,: )
* @return
*/
public String isPwdComplex(String newPwd) {
int digit = 0;
int lowerString = 0;
int upperString = 0;
int special = 0;
Boolean flag = true;
if(StringHandle.isNotEmpty(newPwd) && newPwd.length() > 0){
for(int i = 0; i < newPwd.length(); i++){
if(newPwd.charAt(i) >= '0' && newPwd.charAt(i) <= '9') {
digit = 1;
} else if (newPwd.charAt(i) >= 'a' && newPwd.charAt(i) <= 'z'){
lowerString = 1;
} else if (newPwd.charAt(i) >= 'A' && newPwd.charAt(i) <= 'Z'){
upperString = 1;
}else if(newPwd.charAt(i) == '(' || newPwd.charAt(i) == ')' || newPwd.charAt(i) == '-' || newPwd.charAt(i) == '.' || newPwd.charAt(i) == ':') {
special = 1;
} else {
flag = false; // 包含特殊字符,但是不被允许
}
}
if(digit != 1 || lowerString + upperString == 0){ // 必须由字母数字组成
return "incomplete";
} else if (digit == 1 && lowerString + upperString != 0 && flag == true) { // 包含不被允许的特殊字符
return "correct";
} else { // 验证通过
return "nonsupport";
}
} else {
return "";
}
}
最终的代码是:
1、在该方法中调用验证
String newpassword = ServletActionContext.getRequest().getParameter("newpassword");
if(StringHandle.isNotEmpty(newpassword) && newpassword.length() >= 6) {
String verificationResults = isPwdComplex(newpassword);
if(id!=""&&!id.equals("") && "correct".equals(verificationResults)){
user=userService.getUserById(Integer.parseInt(id));
user.setPassword((MD5.getMD5(newpassword.getBytes())));
userService.addUser(user);
logService.addOperationLog("管理员修改用户密码,id为:"+user.getUserId());
user=null;
out.write(ActionResult.SUCCESS);
} else if("incomplete".equals(verificationResults)) {
out.write("{\"result\":\"incomplete\"}");
} else if("nonsupport".equals(verificationResults)) {
out.write("{\"result\":\"nonsupport\"}");
}
} else {
out.write("{\"result\":\"isnotLong\"}");
}
out.flush();
out.close();
2、在该出进行验证
/**
* 密码复杂性进行验证 可以包含以下( 0~9,a~z,A~Z,(,),-,.,: )
* @return
*/
public String isPwdComplex(String newPwd) {
int digit = 0;
int lowerString = 0;
int upperString = 0;
int special = 0;
Boolean flag = true;
for(int i = 0; i < newPwd.length(); i++){
if(newPwd.charAt(i) >= '0' && newPwd.charAt(i) <= '9') {
digit = 1;
} else if (newPwd.charAt(i) >= 'a' && newPwd.charAt(i) <= 'z'){
lowerString = 1;
} else if (newPwd.charAt(i) >= 'A' && newPwd.charAt(i) <= 'Z'){
upperString = 1;
}else if(newPwd.charAt(i) == '(' || newPwd.charAt(i) == ')' || newPwd.charAt(i) == '-' || newPwd.charAt(i) == '.' || newPwd.charAt(i) == ':') {
special = 1;
} else {
flag = true; // 包含特殊字符,但是不被允许
}
}
if(digit != 1 || lowerString + upperString == 0){
return "incomplete";
} else if (digit == 1 && lowerString + upperString != 0 && flag == true) {
return "correct";
} else {
return "nonsupport";
}
}
前台返回数据提示
success : function(result) {
eval('var obj = '+ result);
if (obj.result == "success") {
alert("密码修改成功!");
window.location.href="";
}else if(obj.result == "nonsupport"){
alert("某些特殊字符不支持!");
}else if(obj.result == "incomplete"){
alert("密码必须包含数字和字母!");
}else if(obj.result == "isnotLong"){
alert("密码至少6位");
}else{
alert(obj.result);
}
}