注册-限制设备数和ip

This commit is contained in:
khalil
2024-06-03 14:54:44 +08:00
parent 86a353e72b
commit 637371f56a
4 changed files with 62 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
package com.accompany.oauth2.dto;
public class IpMaxRegisterLimitConfig {
public class DayIpMaxRegisterLimitConfig {
private boolean open;
private long max;

View File

@@ -0,0 +1,10 @@
package com.accompany.oauth2.dto;
import lombok.Data;
@Data
public class RepeatedDeviceIpRegisterLimitConfig {
private boolean open;
private int repeatedDeviceNumLimit;
private int repeatedIpNumLimit;
}

View File

@@ -25,10 +25,12 @@ import com.accompany.core.service.user.UserCancelRecordService;
import com.accompany.core.service.user.UsersBaseService;
import com.accompany.core.util.MD5;
import com.accompany.oauth2.constant.LoginTypeEnum;
import com.accompany.oauth2.dto.IpMaxRegisterLimitConfig;
import com.accompany.oauth2.dto.RepeatedDeviceIpRegisterLimitConfig;
import com.accompany.oauth2.dto.DayIpMaxRegisterLimitConfig;
import com.accompany.oauth2.event.UserRegisterSuccessEvent;
import com.accompany.oauth2.exception.CustomOAuth2Exception;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -119,13 +121,7 @@ public class AccountManageService {
Account account = accountService.getAccountByThird(type, unionId,
LoginTypeEnum.GOOGLE.getValue() == type? deviceInfo.getApp(): null);
if (account == null) {
IpMaxRegisterLimitConfig config = getLimitConfig();
if (null != config && config.getOpen()){
int count = accountService.getRegisterIpCountByOneDay(ipAddress);
if (count >= config.getMax()) {
throw new CustomOAuth2Exception(CustomOAuth2Exception.SIGN_IP_TO_OFTEN, BusiStatus.REGISTER_FREQUENT.getReasonPhrase());
}
}
checkRegisterLimit(deviceInfo.getDeviceId(), ipAddress);
Date date = new Date();
account = new Account();
@@ -145,10 +141,6 @@ public class AccountManageService {
account = fillDeviceInfo(account, deviceInfo);
if (deviceInfo != null) {
}
accountMapper.insert(account);
//写缓存
accountService.writeAche(account);
@@ -185,6 +177,34 @@ public class AccountManageService {
}
}
private void checkRegisterLimit(String deviceId, String ipAddress){
if (!StringUtils.hasText(deviceId)){
throw new CustomOAuth2Exception(CustomOAuth2Exception.SIGN_IP_TO_OFTEN, BusiStatus.DEVICE_ERROR.getReasonPhrase());
}
RepeatedDeviceIpRegisterLimitConfig repeatedConfig = getRepeatedDeviceIpLimitConfig();
if (repeatedConfig.isOpen()){
int repeatedDeviceNum = accountService.lambdaQuery().eq(Account::getDeviceId, deviceId).count();
if (repeatedDeviceNum >= repeatedConfig.getRepeatedDeviceNumLimit()){
throw new CustomOAuth2Exception(CustomOAuth2Exception.SIGN_IP_TO_OFTEN, BusiStatus.REGISTER_FREQUENT.getReasonPhrase());
}
int repeatedIpNum = accountService.lambdaQuery().eq(Account::getRegisterIp, ipAddress).count();
if (repeatedIpNum >= repeatedConfig.getRepeatedIpNumLimit()){
throw new CustomOAuth2Exception(CustomOAuth2Exception.SIGN_IP_TO_OFTEN, BusiStatus.REGISTER_FREQUENT.getReasonPhrase());
}
}
//当日单个ip注册数
DayIpMaxRegisterLimitConfig config = getIpMaxLimitConfig();
if (config.getOpen()){
int count = accountService.getRegisterIpCountByOneDay(ipAddress);
if (count >= config.getMax()) {
throw new CustomOAuth2Exception(CustomOAuth2Exception.SIGN_IP_TO_OFTEN, BusiStatus.REGISTER_FREQUENT.getReasonPhrase());
}
}
}
private String encryptPassword(String password) {
return MD5.getMD5(password);
}
@@ -211,13 +231,8 @@ public class AccountManageService {
*/
public Account saveSignUpByPhone(String phone, String password, DeviceInfo deviceInfo, String prefillInviteCode, Long prefillInviteUid,
String ipAddress,String phoneAreaCode) throws Exception {
IpMaxRegisterLimitConfig config = getLimitConfig();
if (null != config && config.getOpen()){
int count = accountService.getRegisterIpCountByOneDay(ipAddress);
if (count >= config.getMax()) {
throw new CustomOAuth2Exception(CustomOAuth2Exception.SIGN_IP_TO_OFTEN, BusiStatus.REGISTER_FREQUENT.getReasonPhrase());
}
}
checkRegisterLimit(deviceInfo.getDeviceId(), ipAddress);
Date date = new Date();
Account account = new Account();
account.setPhone(phone);
@@ -398,12 +413,20 @@ public class AccountManageService {
throw exception;
}
private IpMaxRegisterLimitConfig getLimitConfig(){
private DayIpMaxRegisterLimitConfig getIpMaxLimitConfig(){
String config = sysConfService.getSysConfValueById(Constant.SysConfId.IP_MAX_REGISTER_LIMIT_CONFIG);
if (StringUtils.isEmpty(config)){
return null;
if (!StringUtils.hasText(config)){
throw new ServiceException(BusiStatus.ALREADY_NOTEXISTS_CONFIG);
}
return gson.fromJson(config, IpMaxRegisterLimitConfig.class);
return gson.fromJson(config, DayIpMaxRegisterLimitConfig.class);
}
private RepeatedDeviceIpRegisterLimitConfig getRepeatedDeviceIpLimitConfig(){
String config = sysConfService.getSysConfValueById(Constant.SysConfId.REPEATED_DEVICE_IP_REGISTER_LIMIT_CONFIG);
if (!StringUtils.hasText(config)){
throw new ServiceException(BusiStatus.ALREADY_NOTEXISTS_CONFIG);
}
return gson.fromJson(config, RepeatedDeviceIpRegisterLimitConfig.class);
}
}