手机号授权: 增加区域字段并进行手机格式校验

This commit is contained in:
lzm
2022-11-24 16:20:45 +08:00
committed by yeungchihang
parent 449d247356
commit 3a81e32f24
18 changed files with 126 additions and 30 deletions

View File

@@ -7,6 +7,7 @@ import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author PaperCut
@@ -38,6 +39,12 @@ public class SystemConfig {
// 线性奖池中,可用的奖品线最小数
public static Long minAvailableLinearlyPoolLineCount = 2L;
public static Map<String, String> phoneRegex;
public void setPhoneRegex(Map<String, String> phoneRegex) {
SystemConfig.phoneRegex = phoneRegex;
}
public void setFlowTeamGenInviteCodeUid(String flowTeamGenInviteCodeUid) {
SystemConfig.flowTeamGenInviteCodeUid = flowTeamGenInviteCodeUid;
}

View File

@@ -1,6 +1,13 @@
package com.accompany.common.utils;
import com.accompany.common.config.SystemConfig;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
@@ -9,6 +16,7 @@ import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class CommonUtil {
public static String phoneRegx="1(3|4|6|5|7|8|9)[0-9]{9}";
public static String numberOnly="^[0-9]*$";
@@ -234,4 +242,47 @@ public class CommonUtil {
return true;
}
}
/**
* 確認手機號格式是否正確
* @param areaCode
* @param phone
* @return
*/
public static boolean checkPhoneFormat(String areaCode, String phone) {
if (!checkNumberOnly(phone)) {
return false;
}
String realPhone = phone;
if (realPhone.startsWith(areaCode)) {
realPhone = realPhone.replaceFirst(areaCode, "");
}
// 优先以配置的正则校验
if (!CollectionUtils.isEmpty(SystemConfig.phoneRegex)) {
String phoneRegex = SystemConfig.phoneRegex.get(areaCode);
if (StringUtils.isNotBlank(phoneRegex)) {
return checkValidPhone(realPhone, phoneRegex);
}
}
// 使用工具库校验
phone = "+" + phone;
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
log.info("checkPhoneFormat, phone {}", phone);
Phonenumber.PhoneNumber swissNumberProto = phoneUtil.parse(phone, null);
return phoneUtil.isValidNumber(swissNumberProto);
} catch (NumberParseException e) {
log.error("checkPhoneFormat error", e);
return false;
}
}
private static boolean checkValidPhone(String phone, String phoneRegex) {
if (org.springframework.util.StringUtils.isEmpty(phone)) {
return false;
}
return phone.matches(phoneRegex);
}
}

View File

@@ -25,6 +25,8 @@ public class Account {
private String phone;
private String phoneAreaCode;
private Long erbanNo;
private String password;

View File

@@ -14,6 +14,8 @@ public class Users implements ReplaceDomainInterface {
private String phone;
private String phoneAreaCode;
private Date birth;
private Byte star;
@@ -632,4 +634,12 @@ public class Users implements ReplaceDomainInterface {
public void setInviteUid(Long inviteUid) {
this.inviteUid = inviteUid;
}
public String getPhoneAreaCode() {
return phoneAreaCode;
}
public void setPhoneAreaCode(String phoneAreaCode) {
this.phoneAreaCode = phoneAreaCode;
}
}

View File

@@ -281,14 +281,16 @@ public class UsersBaseService extends BaseService {
}
/* 绑定手机 */
public void boundPhone(Long uid, String phone) throws Exception {
public void boundPhone(Long uid, String phone,String phoneAreaCode) throws Exception {
Users user = new Users();
user.setUid(uid);
user.setPhone(phone);
user.setPhoneAreaCode(phoneAreaCode);
usersMapper.updateByPrimaryKeySelective(user);
Account account = new Account();
account.setUid(uid);
account.setPhone(phone);
account.setPhoneAreaCode(phoneAreaCode);
accountService.updateById(account);
Users userDb = usersMapper.selectByPrimaryKey(user.getUid());
saveUserCache(userDb);

View File

@@ -230,7 +230,7 @@ public class ActivitiesAnnualService extends ActivityBaseService {
* @return
* @throws Exception
*/
public BusiResult bindPhone(Long uid, String phone) throws Exception{
public BusiResult bindPhone(Long uid, String phone,String phoneAreaCode) throws Exception{
BusiResult busiResult = new BusiResult(BusiStatus.SUCCESS);
Users users = usersService.getUsersByUid(uid);
if (CommonUtil.checkValidPhone(users.getPhone())){
@@ -241,7 +241,7 @@ public class ActivitiesAnnualService extends ActivityBaseService {
busiResult.setMessage("手机号码已经注册");
return busiResult;
}
usersBaseService.boundPhone(uid, phone);
usersBaseService.boundPhone(uid, phone,phoneAreaCode);
return busiResult;
}

View File

@@ -792,6 +792,10 @@ public class UsersService extends BaseService {
if (StringUtils.isNotBlank(phone)) {
users.setPhone(phone);
}
String phoneAreaCode = account.getPhoneAreaCode();
if (StringUtils.isNotBlank(phoneAreaCode)) {
users.setPhoneAreaCode(phoneAreaCode);
}
if (deviceInfo != null) {
fillDeviceInfo(users, deviceInfo);
}

View File

@@ -149,12 +149,12 @@ public class WithdrawService extends ServiceImpl<WithdrawMapper,WithdrawRecord>
}
public Boolean boundPhone(Long uid, String phone, String code) throws Exception {
public Boolean boundPhone(Long uid, String phone, String code, String phoneAreaCode) throws Exception {
if(usersBaseService.checkPhoneExists(phone)){
throw new ServiceException("手機號碼已經註冊");
}
if (smsService.verifySmsCode(phone, code)) {
usersBaseService.boundPhone(uid, phone);
usersBaseService.boundPhone(uid, phone,phoneAreaCode);
//记录最近30天内绑定手机号
jedisService.setex(RedisKey.blind_phone_sign.getKey(String.valueOf(uid)), 30 * 24 * 60 *60, String.valueOf(new Date().getTime()));

View File

@@ -104,12 +104,13 @@ public class WithdrawController extends BaseController {
@Authorization
public BusiResult boundPhone(@RequestParam("phone") String phone,
@RequestParam("code") String code,
@RequestParam("phoneAreaCode") String phoneAreaCode,
HttpServletRequest request) throws Exception{
phone = decryptSensitiveInfo(request, phone);
if (phoneBlackService.checkIsNeedIntercept(phone)) {
throw new ServiceException(BusiStatus.PHONE_BE_INTERCEPTED);
}
return new BusiResult(withdrawService.boundPhone(getUid(), phone, code));
return new BusiResult(withdrawService.boundPhone(getUid(), phone, code,phoneAreaCode));
}
/**

View File

@@ -38,14 +38,14 @@ public class ActivitiesAnnualController extends BaseController {
}
@RequestMapping(value = "/bindPhone")
public BusiResult bindPhone(Long uid, String phone) throws Exception{
if (uid == null || StringUtils.isEmpty(phone)){
public BusiResult bindPhone(Long uid, String phone,String phoneAreaCode) throws Exception{
if (uid == null || StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneAreaCode)){
return new BusiResult(BusiStatus.PARAMERROR);
}
if (!CommonUtil.checkValidPhone(phone)){
return new BusiResult(BusiStatus.PARAMERROR);
}
return activitiesAnnualService.bindPhone(uid, phone);
return activitiesAnnualService.bindPhone(uid, phone,phoneAreaCode);
}
@GetMapping("/queryTaskStatus")

View File

@@ -11,7 +11,7 @@ import org.springframework.security.core.userdetails.UserDetailsService;
public interface MyUserDetailsService extends UserDetailsService {
UserDetails loadUserByPhone(String phone, String smsCode, DeviceInfo deviceInfo, String ipAddress) throws Exception;
UserDetails loadUserByPhone(String phone,String phoneAreaCode, String smsCode, DeviceInfo deviceInfo, String ipAddress) throws Exception;
UserDetails loadUserByOpenId(String openid, Byte type, DeviceInfo deviceInfo, String ipAddress, String unionId,
YiDunDto yiDunDto, ShuMeiDto shuMeiDto, String appleFullName) throws Exception;

View File

@@ -105,11 +105,11 @@ public class MyUserDetailsServiceImpl implements MyUserDetailsService {
}
@Override
public UserDetails loadUserByPhone(String phone, String smsCode, DeviceInfo deviceInfo, String ipAddress)
public UserDetails loadUserByPhone(String phone,String phoneAreaCode, String smsCode, DeviceInfo deviceInfo, String ipAddress)
throws Exception {
Account account = null;
if (CommonUtil.checkValidPhone(phone)) {
account = accountManageService.getOrGenAccountByPhone(phone,smsCode,deviceInfo,ipAddress);
if (CommonUtil.checkPhoneFormat(phoneAreaCode,phone)) {
account = accountManageService.getOrGenAccountByPhone(phone,phoneAreaCode,smsCode,deviceInfo,ipAddress);
} else if (CommonUtil.checkNumberOnly(phone)) {
account = accountService.getAccountByErBanNo(Long.valueOf(phone));
}

View File

@@ -7,6 +7,7 @@ import com.accompany.common.exception.ApiException;
import com.accompany.common.exception.BusinessException;
import com.accompany.common.redis.RedisKey;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.CommonUtil;
import com.accompany.common.utils.UUIDUitl;
import com.accompany.core.exception.ServiceException;
import com.accompany.core.model.Account;
@@ -182,7 +183,7 @@ public class AccountH5LoginService {
return map;
}
public void register(String mobile, String code, String inviteCode, Long inviteUid, DeviceInfo deviceInfo, String ipAddress) {
public void register(String mobile, String code, String inviteCode, Long inviteUid, DeviceInfo deviceInfo, String ipAddress,String phoneAreaCode) {
// 校验验证码
if (!smsService.verifySmsCode(mobile, code)) {
throw new ServiceException(BusiStatus.SMSCODEERROR);
@@ -191,9 +192,12 @@ public class AccountH5LoginService {
if (null != account) {
throw new ServiceException(BusiStatus.PHONE_REGISTERED);
}
if (CommonUtil.checkPhoneFormat(phoneAreaCode,mobile)){
throw new ServiceException(BusiStatus.PHONEINVALID);
}
try {
accountManageService.saveSignUpByPhone(mobile, null, deviceInfo, inviteCode, inviteUid, ipAddress);
accountManageService.saveSignUpByPhone(mobile, null, deviceInfo, inviteCode, inviteUid, ipAddress,phoneAreaCode);
} catch (Exception e) {
log.error("h5 注册失败。mobile{}", mobile);
throw new ServiceException("注册失败", e);

View File

@@ -105,9 +105,9 @@ public class AccountManageService {
protected Gson gson = new Gson();
public Account getOrGenAccountByPhone(String phone, String smsCode, DeviceInfo deviceInfo, String ipAddress)
public Account getOrGenAccountByPhone(String phone,String phoneAreaCode, String smsCode, DeviceInfo deviceInfo, String ipAddress)
throws Exception {
log.info("getOrGenAccountByPhone phone:{},smsCode:{}",phone,smsCode);
log.info("getOrGenAccountByPhone phone:{},smsCode:{},phoneAreaCode{}",phone,smsCode,phoneAreaCode);
final String lockVal = jedisLockService.lock(RedisKey.lock_register_by_phone.getKey(phone));
try {
if (BlankUtil.isBlank(lockVal)){
@@ -115,7 +115,7 @@ public class AccountManageService {
}
Account account = accountService.getAccountByPhone(phone);
if (account == null) {
account = saveSignUpByPhone(phone,null, deviceInfo,ipAddress);
account = saveSignUpByPhone(phone,null, deviceInfo,ipAddress,phoneAreaCode);
} else {
//account = fillDeviceInfo(account, deviceInfo);
String state = account.getState();
@@ -318,8 +318,8 @@ public class AccountManageService {
* @return
*/
public Account saveSignUpByPhone(String phone, String password, DeviceInfo deviceInfo,
String ipAddress) throws Exception {
return saveSignUpByPhone(phone, password, deviceInfo, null, null, ipAddress);
String ipAddress,String phoneAreaCode) throws Exception {
return saveSignUpByPhone(phone, password, deviceInfo, null, null, ipAddress,phoneAreaCode);
}
@@ -331,7 +331,7 @@ public class AccountManageService {
* @return
*/
public Account saveSignUpByPhone(String phone, String password, DeviceInfo deviceInfo, String prefillInviteCode, Long prefillInviteUid,
String ipAddress) throws Exception {
String ipAddress,String phoneAreaCode) throws Exception {
IpMaxRegisterLimitConfig config = getLimitConfig();
if (null != config && config.getOpen()){
int count = accountService.getRegisterIpCountByOneDay(ipAddress);
@@ -342,6 +342,7 @@ public class AccountManageService {
Date date = new Date();
Account account = new Account();
account.setPhone(phone);
account.setPhoneAreaCode(phoneAreaCode);
if(!StringUtils.isEmpty(password)){
account.setPassword(encryptPassword(password));
}

View File

@@ -61,6 +61,7 @@ public class PasswordAuthenticationProvider implements AuthenticationProvider{
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Map params = (Map) authentication.getDetails();
String smsCode = (String) params.get("code");
String phoneAreaCode = (String) params.get("phoneAreaCode");
String ipAddress = RequestContextHolderUtils.getRemoteAddr();
DeviceInfo deviceInfo = new DeviceInfo();
String yiDunToken = null;
@@ -103,7 +104,7 @@ public class PasswordAuthenticationProvider implements AuthenticationProvider{
}
UserDetails userDetails;
try {
userDetails = myUserDetailsService.loadUserByPhone(username, smsCode, deviceInfo, ipAddress);
userDetails = myUserDetailsService.loadUserByPhone(username,phoneAreaCode, smsCode, deviceInfo, ipAddress);
if (LoginTypeEnum.PASSWORD.getValue() == loginTypeEnum.getValue()) {
try {
password = DESUtils.DESAndBase64Decrypt(password, KeyStore.DES_ENCRYPT_KEY);

View File

@@ -8,10 +8,7 @@ import com.accompany.common.exception.BusinessException;
import com.accompany.common.redis.RedisKey;
import com.accompany.common.result.BusiResult;
import com.accompany.common.status.BusiStatus;
import com.accompany.common.utils.AppVersionUtil;
import com.accompany.common.utils.BlankUtil;
import com.accompany.common.utils.DESUtils;
import com.accompany.common.utils.IPUitls;
import com.accompany.common.utils.*;
import com.accompany.core.dto.ShuMeiDto;
import com.accompany.core.dto.YiDunDto;
import com.accompany.core.exception.ServiceException;
@@ -147,7 +144,7 @@ public class AccountController extends BaseController {
@ResponseBody
public BusiResult<Account> register(String phone, @RequestParam String password, String smsCode,
String verifyCode, DeviceInfo deviceInfo, HttpServletRequest request, String token,
String shuMeiDeviceId,Double gpsLongitude,Double gpsLatitude) {
String shuMeiDeviceId,Double gpsLongitude,Double gpsLatitude,String phoneAreaCode) {
phone = decryptSensitiveInfo(request, phone);
String ipAddress = IPUitls.getRealIpAddress(request);
accountManageService.checkSystemBlock(phone, deviceInfo.getDeviceId(), ipAddress);
@@ -208,8 +205,13 @@ public class AccountController extends BaseController {
return new BusiResult<>(BusiStatus.SHUMEI_REGISTER_ERROR);
}
}
if (CommonUtil.checkPhoneFormat(phoneAreaCode,phone)){
throw new ServiceException(BusiStatus.PHONEINVALID);
}
return new BusiResult<>(accountManageService.
saveSignUpByPhone(phone, password, deviceInfo, ipAddress));
saveSignUpByPhone(phone, password, deviceInfo, ipAddress,phoneAreaCode));
} catch (Exception e) {
logger.error("注册失败当前注册手机号码phone=" + phone + "失败原因:" + e.getMessage());
return new BusiResult<>(BusiStatus.SERVER_BUSY);

View File

@@ -110,11 +110,11 @@ public class H5LoginController extends BaseController {
}
@PostMapping("/register")
public BusiResult<Void> registerByPhone(String mobile, String code, String inviteCode, Long inviteUid, HttpServletRequest request) {
public BusiResult<Void> registerByPhone(String phoneAreaCode,String mobile, String code, String inviteCode, Long inviteUid, HttpServletRequest request) {
logger.info("h5 registerByPhone, mobile:{}, code:{}, inviteCode: {}", mobile, code, inviteCode);
DeviceInfo deviceInfo = getDeviceInfo(request);
String ipAddress = IPUitls.getRealIpAddress(request);
accountH5LoginService.register(mobile, code, inviteCode, inviteUid, deviceInfo, ipAddress);
accountH5LoginService.register(mobile, code, inviteCode, inviteUid, deviceInfo, ipAddress,phoneAreaCode);
return new BusiResult<>(BusiStatus.SUCCESS);
}

11
pom.xml
View File

@@ -112,6 +112,7 @@
<redisson.version>3.16.8</redisson.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<esayexcel.version>3.1.1</esayexcel.version>
<googlecode-libphonenumber.version>8.12.43</googlecode-libphonenumber.version>
</properties>
<dependencyManagement>
@@ -579,6 +580,11 @@
<artifactId>redisson-spring-data-20</artifactId>
<version>${redisson.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>${googlecode-libphonenumber.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
@@ -1151,6 +1157,11 @@
<version>${esayexcel.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
</dependency>
</dependencies>
<build>