Explorar el Código

风控体系代码提交

zjy hace 1 mes
padre
commit
07860f8b9d

+ 2 - 0
alien-entity/src/main/java/shop/alien/mapper/second/SecondRiskControlRecordMapper.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
+import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 import shop.alien.entity.second.SecondGoodsRecord;
@@ -15,6 +16,7 @@ import java.util.List;
 /**
  * 二手商品风控记录映射器
  */
+@Mapper
 public interface SecondRiskControlRecordMapper extends BaseMapper<SecondRiskControlRecord> {
 
 

+ 6 - 0
alien-gateway/pom.xml

@@ -189,6 +189,12 @@
             <artifactId>jaxb-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>shop.alien</groupId>
+            <artifactId>alien-config</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 2 - 2
alien-gateway/src/main/java/shop/alien/gateway/AlienGatewayApplication.java

@@ -13,8 +13,8 @@ import org.springframework.context.annotation.ComponentScan;
  * @version 1.0
  * @date 2025/1/21 16:58
  */
-@ComponentScan({"shop.alien.gateway.*"})
-@MapperScan({"shop.alien.gateway.mapper"})
+@ComponentScan({"shop.alien.gateway.*", "shop.alien.config.properties"})
+@MapperScan({"shop.alien.gateway.mapper", "shop.alien.mapper"})
 @EnableFeignClients(basePackages = "shop.alien.gateway.feign")
 @SpringBootApplication
 public class AlienGatewayApplication {

+ 45 - 21
alien-gateway/src/main/java/shop/alien/gateway/service/LifeUserService.java

@@ -5,23 +5,24 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import shop.alien.config.properties.RiskControlProperties;
 import shop.alien.entity.second.LifeUserLog;
+import shop.alien.entity.second.SecondRiskControlRecord;
 import shop.alien.entity.store.LifeUser;
 import shop.alien.entity.store.vo.LifeUserVo;
 import shop.alien.gateway.config.BaseRedisService;
 import shop.alien.gateway.feign.SecondServiceFeign;
 import shop.alien.gateway.mapper.LifeUserLogMapper;
 import shop.alien.gateway.mapper.LifeUserMapper;
+import shop.alien.mapper.second.SecondRiskControlRecordMapper;
 import shop.alien.util.common.JwtUtil;
 
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -37,10 +38,14 @@ public class LifeUserService extends ServiceImpl<LifeUserMapper, LifeUser> {
 
     private final BaseRedisService baseRedisService;
 
-    private final SecondServiceFeign secondServiceFeign;
+    private final SecondServiceFeign alienSecondFeign;
+
+    @Autowired
+    private RiskControlProperties riskControlProperties;
+
+    private final SecondRiskControlRecordMapper secondRiskControlRecordMapper;
+
 
-    @Value("${risk-control.account-abnormal.reg-count24h:4}")
-    private Integer regCount24h;
 
     @Value("${jwt.expiration-time}")
     private String effectiveTime;
@@ -88,10 +93,10 @@ public class LifeUserService extends ServiceImpl<LifeUserMapper, LifeUser> {
                 userVo.setToken(JwtUtil.createJWT("user_" + phoneNum, lifeUser.getUserName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong));
 //                userVo.setToken(JWTUtils.createToken(tokenMap));
                 baseRedisService.setString("user_" + phoneNum, userVo.getToken());
+
                 // 二手平台登录log,同一个macip登录多账号记录
                 addLifeUserLogInfo(user2, macIp);
-                // 第一次登录,添加用户基础积分
-                secondServiceFeign.createPointsRecord(user2.getId(), 300, 1);
+
                 return userVo;
             } else {
                 return null;
@@ -108,8 +113,10 @@ public class LifeUserService extends ServiceImpl<LifeUserMapper, LifeUser> {
             String token = JwtUtil.createJWT("user_" + phoneNum, user.getUserName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong);
             userVo.setToken(token);
             baseRedisService.setString("user_" + phoneNum, token);
+
             // 二手平台登录log,同一个macip登录多账号记录
             addLifeUserLogInfo(user, macIp);
+
             return userVo;
         }
     }
@@ -131,21 +138,38 @@ public class LifeUserService extends ServiceImpl<LifeUserMapper, LifeUser> {
             lifeUserLog.setUserName(user.getUserName());
             lifeUserLog.setMacIp(macIp);
             lifeUserLog.setCreatedTime(new Date());
-            lifeUserLogMapper.insert(lifeUserLog);
-
-            String startDate = LocalDateTime.now().minusHours(24L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-            String endDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-            List<LifeUserLog> lsit = lifeUserLogMapper.getLifeUserLogByDate(startDate, endDate, macIp);
-
-            if (lsit.size() > regCount24h) {
-                String detailInfo = lsit.stream()
-                        .map(row -> row.getUserId().toString())
-                        .collect(Collectors.joining(","));
-                secondServiceFeign.recordRiskControlData(null, 2, "账号异常", macIp, detailInfo);
+            int count = lifeUserLogMapper.insert(lifeUserLog);
+            if (count > 0) {
+                String startDate = LocalDateTime.now().minusHours(24L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+                String endDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+                List<LifeUserLog> lsit = lifeUserLogMapper.getLifeUserLogByDate(startDate, endDate, macIp);
+
+                if (lsit.size() > riskControlProperties.getAccountAbnormal().getRegCount24h() && !isViolation(startDate, endDate, macIp, user.getId())) {
+                    String detailInfo = lsit.stream()
+                            .map(row -> row.getUserId().toString())
+                            .collect(Collectors.joining(","));
+                    alienSecondFeign.recordRiskControlData(user.getId(), 2, "账号异常", macIp, detailInfo);
+                }
             }
+            // 第一次登录,添加用户基础积分
+            alienSecondFeign.createPointsRecord(user.getId(), 300, 1);
         } catch (Exception e) {
-            log.error("用户登录log存放异常:{}");
+            log.error("用户登录log存放异常:{}", e);
         }
 
     }
+
+    public boolean isViolation(String startDate, String endDate, String macIp, Integer userId) {
+        List<SecondRiskControlRecord> list = secondRiskControlRecordMapper.selectByBusinessId(startDate, endDate, macIp);
+        for (SecondRiskControlRecord record : list) {
+            // 将数组转换为包含整数的列表
+            List<Integer> userIdList = Arrays.stream(record.getDetailInfo().split(","))
+                    .map(Integer::parseInt)
+                    .collect(Collectors.toList());
+            if (userIdList.contains(userId)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }