diff --git a/accompany-base/accompany-core/src/main/java/com/accompany/common/constant/Constant.java b/accompany-base/accompany-core/src/main/java/com/accompany/common/constant/Constant.java index bed3674ca..b0551af8a 100644 --- a/accompany-base/accompany-core/src/main/java/com/accompany/common/constant/Constant.java +++ b/accompany-base/accompany-core/src/main/java/com/accompany/common/constant/Constant.java @@ -1268,6 +1268,11 @@ public class Constant { * google退款策略配置 */ public static final String GOOGLE_PAY_REFUND_CONFIG = "google_pay_refund_config"; + + /** + * 设备数ip限制数 + */ + public static final String REPEATED_DEVICE_IP_REGISTER_LIMIT_CONFIG = "repeated_device_ip_register_limit_config"; } public static class WithDrawStatus { diff --git a/accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/IpMaxRegisterLimitConfig.java b/accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/DayIpMaxRegisterLimitConfig.java similarity index 88% rename from accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/IpMaxRegisterLimitConfig.java rename to accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/DayIpMaxRegisterLimitConfig.java index e6fd16189..be8b4fa49 100644 --- a/accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/IpMaxRegisterLimitConfig.java +++ b/accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/DayIpMaxRegisterLimitConfig.java @@ -1,6 +1,6 @@ package com.accompany.oauth2.dto; -public class IpMaxRegisterLimitConfig { +public class DayIpMaxRegisterLimitConfig { private boolean open; private long max; diff --git a/accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/RepeatedDeviceIpRegisterLimitConfig.java b/accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/RepeatedDeviceIpRegisterLimitConfig.java new file mode 100644 index 000000000..e5bc159cd --- /dev/null +++ b/accompany-oauth2/accompany-oauth2-sdk/src/main/java/com/accompany/oauth2/dto/RepeatedDeviceIpRegisterLimitConfig.java @@ -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; +} diff --git a/accompany-oauth2/accompany-oauth2-service/src/main/java/com/accompany/oauth2/service/account/AccountManageService.java b/accompany-oauth2/accompany-oauth2-service/src/main/java/com/accompany/oauth2/service/account/AccountManageService.java index e8c618e29..00a8cea51 100644 --- a/accompany-oauth2/accompany-oauth2-service/src/main/java/com/accompany/oauth2/service/account/AccountManageService.java +++ b/accompany-oauth2/accompany-oauth2-service/src/main/java/com/accompany/oauth2/service/account/AccountManageService.java @@ -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); } }