若依分离版添加手机号登录
手机号加密码登录
- (1) 找到登录接口
- (2)找到实际做登录验证的地方(代码里面注释的地方说的很明白)
- (3) 全局搜索该方法 loadUserByUsername
- (4) 找到用户验证的地方 SysUser user = userService.selectUserByUserName(username);
-
(5)添加手机号登录的逻辑
@Override
public SysUser selectUserByUserName(String userName) {
if (PhoneUtils.isMobile(userName)) {
// 手机号登录
return userMapper.selectUserByPhone(userName);
}
return userMapper.selectUserByUserName(userName);
}
手机号校验方法类
package com.ruoyi.common.utils;
import java.util.regex.Pattern;
public class PhoneUtils {
private static final String REGEX_MOBILE = "((\\+86|0086)?\\s*)((134[0-8]\\d{7})|(((13([0-3]|[5-9]))|(14[5-9])|15([0-3]|[5-9])|(16(2|[5-7]))|17([0-3]|[5-8])|18[0-9]|19([0-9]))\\d{8})|(14(0|1|4)0\\d{7})|(1740([0-5]|[6-9]|[10-12])\\d{7}))";
/**
* 正则:固定电话号码,可带区号,然后至少6,8位数字
*/
private static final String REGEX_TEL = "^(\\d{3,4}-)?\\d{6,8}$";
private static final Pattern PATTERN_REGEX_TEL = Pattern.compile(REGEX_TEL);
/**
* 判断是否是手机号
*
* @param tel 手机号
* @return boolean true:是 false:否
*/
public static boolean isMobile(String tel) {
if (StringUtils.isEmpty(tel)) {
return false;
}
return Pattern.matches(REGEX_MOBILE, tel);
}
/**
* 验证固定电话号码
*/
public static boolean isTel(String str) {
return isMatch(PATTERN_REGEX_TEL, str);
}
public static boolean isMatch(Pattern pattern, String str) {
return StringUtils.isNotEmpty(str) && pattern.matcher(str).matches();
}
public static void main(String[] args) {
System.out.println(isTel("2887438"));
}
}
- (6) 新建通过手机号查询的mapper和sql
<select id="selectUserByPhone" resultMap="SysUserResult">
<include refid="selectUserVo"/>
where u.phonenumber = #{phone}
</select>