注册校验我采用是Jquery插件validate
validate语法
<script src="${pageContext.request.contextPath}/js/jquery.validate.min.js"></script>
<script src="${pageContext.request.contextPath}/js/messages_zh.min.js"></script>
<script type="text/javascript">
//自定义校验规则
$.validator.addMethod(
//规则
"checkUsername",
function(value,element,params){
var isExist = false;
//alert(value)
$.ajax({
"async":false,//如果此处为true则为ajax的异步加载,但是在success方法中isExist赋值过程中会出现异步问题,所以要改成同步
"url":"${pageContext.request.contextPath}/user/checkUsername",
"data":{"user_name":value},
"type":"POST",
"dataType":"json",
"success":function(data){
isExist = data;
}
});
return !isExist;
}
);
$(function () {
$("#registForm").validate({
rules: {
user_name: {
"required": true,
"checkUsername":true
},
password: {
"required": true,
"rangelength": [6, 12]
},
repeatpwd: {
"required": true,
"rangelength": [6, 12],
"equalTo": "#Upassword"
},
phone: {
"required": true
}
},
messages: {
user_name: {
"required": "用户名不能为空",
"checkUsername":"用户名已存在"//自定义校验规则
},
password: {
"required": "密码不能为空",
"rangelength": "密码长度6-12位"
},
repeatpwd: {
"required": "确认密码不能为空",
"rangelength": "密码长度6-12位",
"equalTo": "两次密码不一致"
},
phone: {
"required": "电话号码不能为空"
}
}
});
});
</script>
目前没有采用手机号注册之后会加入该功能
登录的话就是普通的登录,使用MD5对密码进行了加密
MD5工具类
package ssm.hmily_blog.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 1.MD5加密字符串(32位大写)
* 2.MD5加密字符串(32位小写)
* <p>
* MD5在线加密:https://md5jiami.51240.com/
* 3.将二进制字节数组转换为十六进制字符串
* 4.Unicode中文编码转换成字符串
*/
public class MD5Util {
/**
* MD5加密字符串(32位大写)
*
* @param string 需要进行MD5加密的字符串
* @return 加密后的字符串(大写)
*/
public static String md5Encrypt32Upper(String string) {
byte[] hash;
try {
//创建一个MD5算法对象,并获得MD5字节数组,16*8=128位
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Huh, MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Huh, UTF-8 should be supported?", e);
}
//转换为十六进制字符串
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString().toUpperCase();
}
/**
* MD5加密字符串(32位小写)
*
* @param string 需要进行MD5加密的字符串
* @return 加密后的字符串(小写)
*/
public static String md5Encrypt32Lower(String string) {
//直接上面的方法转换成小写就可以了
return md5Encrypt32Upper(string).toLowerCase();
}
/**
* 将二进制字节数组转换为十六进制字符串
*
* @param bytes 二进制字节数组
* @return 十六进制字符串
*/
public static String bytesToHex(byte[] bytes) {
StringBuffer hexStr = new StringBuffer();
int num;
for (int i = 0; i < bytes.length; i++) {
num = bytes[i];
if (num < 0) {
num += 256;
}
if (num < 16) {
hexStr.append("0");
}
hexStr.append(Integer.toHexString(num));
}
return hexStr.toString().toUpperCase();
}
/**
* Unicode中文编码转换成字符串
*/
public static String unicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
return str;
}
public static void main(String[] args) {
System.out.println(md5Encrypt32Lower("123456"));
System.out.println(md5Encrypt32Upper("123456"));
}
}