Commit 89d3dbf2 by 唐功亮

【新增】 用户注册-密码加密MD5

parent c8443882
...@@ -18,6 +18,6 @@ public interface TimescodeRegiDao extends BaseMapper<TimescodeRegiEntity> { ...@@ -18,6 +18,6 @@ public interface TimescodeRegiDao extends BaseMapper<TimescodeRegiEntity> {
TimescodeRegiEntity findByPhone(@Param("phone") String phone); TimescodeRegiEntity findByPhone(@Param("phone") String phone);
@Update("UPDATE timescode_regi SET times_validate = #{times_validate} WHERE phone=#{phone}") @Update("UPDATE timescode_regi SET times_validate = #{times_validate} WHERE phone=#{phone}")
int updateById(@Param("phone") String phone,@Param("times_validate") Integer times_validate); int updateByIdTimes_validate(@Param("phone") String phone,@Param("times_validate") Integer times_validate);
} }
...@@ -4,6 +4,7 @@ import io.office.common.exception.RRException; ...@@ -4,6 +4,7 @@ import io.office.common.exception.RRException;
import io.office.common.utils.IPUtils; import io.office.common.utils.IPUtils;
import io.office.modules.manage.dao.TimescodeRegiDao; import io.office.modules.manage.dao.TimescodeRegiDao;
import io.office.modules.manage.entity.TimescodeRegiEntity; import io.office.modules.manage.entity.TimescodeRegiEntity;
import io.office.modules.manage.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Map; import java.util.Map;
...@@ -76,8 +77,12 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl ...@@ -76,8 +77,12 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl
String subjoinReq = request.getSession().getAttribute("subjoin")==null?"":String.valueOf(request.getSession().getAttribute("subjoin")); String subjoinReq = request.getSession().getAttribute("subjoin")==null?"":String.valueOf(request.getSession().getAttribute("subjoin"));
String telReq = request.getSession().getAttribute("tel")==null?"":String.valueOf(request.getSession().getAttribute("tel")); String telReq = request.getSession().getAttribute("tel")==null?"":String.valueOf(request.getSession().getAttribute("tel"));
Long dateReq = request.getSession().getAttribute("date")==null?0:Long.valueOf(String.valueOf(request.getSession().getAttribute("date")));//毫秒值 Long dateReq = request.getSession().getAttribute("date")==null?0:Long.valueOf(String.valueOf(request.getSession().getAttribute("date")));//毫秒值
//获取验证码验证次数 //获取验证码验证次数
TimescodeRegiEntity timescodeRegiEntity = timescodeRegiDao.selectById(phone); TimescodeRegiEntity timescodeRegiEntity = timescodeRegiDao.selectById(phone);
if (timescodeRegiEntity==null){
throw new RRException("该手机格未获取过验证码");
}
Integer timesValidate = timescodeRegiEntity.getTimesValidate();//验证码输入错误次数; Integer timesValidate = timescodeRegiEntity.getTimesValidate();//验证码输入错误次数;
//校验验证码 //校验验证码
...@@ -85,8 +90,8 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl ...@@ -85,8 +90,8 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl
//存入数据库 密码加密 //存入数据库 密码加密
MemberEntity memberEntity = new MemberEntity(); MemberEntity memberEntity = new MemberEntity();
memberEntity.setUsername(userName); memberEntity.setUsername(userName);
//TODO tgl MD5加密密码 //MD5加密密码
memberEntity.setPassword("MD5加密"); memberEntity.setPassword(MD5Util.md5Encrypt32Upper(passWord));
memberEntity.setEmail(emiail); memberEntity.setEmail(emiail);
memberEntity.setPhone(phone); memberEntity.setPhone(phone);
memberEntity.setOicq(""); memberEntity.setOicq("");
...@@ -115,7 +120,7 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl ...@@ -115,7 +120,7 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl
if (!sub.equals(reSub)){ if (!sub.equals(reSub)){
if (times_validate<=3){ if (times_validate<=3){
times_validate+=1; times_validate+=1;
timescodeRegiDao.updateById(reTel,times_validate); timescodeRegiDao.updateByIdTimes_validate(reTel,times_validate);
throw new RRException("验证码已输入错误" + times_validate + "次(输入错误满3次时,该验证码失效)!"); throw new RRException("验证码已输入错误" + times_validate + "次(输入错误满3次时,该验证码失效)!");
} }
} }
...@@ -140,7 +145,7 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl ...@@ -140,7 +145,7 @@ public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> impl
//用户名校验 //用户名校验
public boolean checkUserName(String logname){ public boolean checkUserName(String logname){
String t1 = "^[0-9A-Za-z]{2,8}$"; //判断密码用户名和密码是否为数字,字母(数字或字母) String t1 = "^[0-9A-Za-z]{2,8}$"; //判断密码用户名和密码是否为数字,字母(数字或字母)
String t2="^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{2,8}$";//(数字和字母) String t2="^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{2,10}$";//(数字和字母)
if(logname.matches(t2)) { if(logname.matches(t2)) {
return logname.matches(t2); return logname.matches(t2);
}else { }else {
......
package io.office.modules.manage.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) {
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().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) {
String md5Encrypt32Lower = MD5Util.md5Encrypt32Lower("0123456789");
String md5Encrypt32Upper1 = MD5Util.md5Encrypt32Upper("1234");
System.out.println(md5Encrypt32Lower);
System.out.println(md5Encrypt32Upper1);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment