Prechádzať zdrojové kódy

全链路:::::ip+黑白名单+访问限制-v0.05

(cherry picked from commit 1619476e9e023c44c9525d7c4795c3a24b37c5e4)
MOKASZ\lw12420 1 rok pred
rodič
commit
2875b5808a

+ 4 - 3
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/planNew/PlanNewCommonOutController.java

@@ -97,8 +97,9 @@ public class PlanNewCommonOutController {
      * @return {@link Mono}<{@link RStatus}<{@link PlanNewCommon}>>
      * @return {@link Mono}<{@link RStatus}<{@link PlanNewCommon}>>
      */
      */
     @GetMapping("getPlanNewCommonH5Copy")
     @GetMapping("getPlanNewCommonH5Copy")
-    public Mono<RStatus<PlanNewCommonRes>> getPlanNewCommonH5Copy(@RequestParam("code") String code, ServerWebExchange exchange) {
-
+    public Mono<RStatus<PlanNewCommonRes>> getPlanNewCommonH5Copy(@RequestParam("code") String code,
+                                                                  @RequestParam("unique") String unique,
+                                                                  ServerWebExchange exchange) {
         Mono<PlanNewCommon> planNewCommonMono = template.selectOne(Query.query(Criteria.where("code").is(code)).columns(COLUMNARRAYCOMMON), PlanNewCommon.class)
         Mono<PlanNewCommon> planNewCommonMono = template.selectOne(Query.query(Criteria.where("code").is(code)).columns(COLUMNARRAYCOMMON), PlanNewCommon.class)
                 .switchIfEmpty(Mono.error(new BaseException("计划code异")))
                 .switchIfEmpty(Mono.error(new BaseException("计划code异")))
                 .onErrorResume(throwable -> Mono.error(new BaseException("计划code异")));
                 .onErrorResume(throwable -> Mono.error(new BaseException("计划code异")));
@@ -185,7 +186,7 @@ public class PlanNewCommonOutController {
         });
         });
         // GZIP压缩
         // GZIP压缩
        // Mono<byte[]> res = resMono.flatMap(f -> Mono.just(Objects.requireNonNull(this.compressData(JSONUtil.toJsonStr(f)))));
        // Mono<byte[]> res = resMono.flatMap(f -> Mono.just(Objects.requireNonNull(this.compressData(JSONUtil.toJsonStr(f)))));
-        Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange);
+        Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange,unique);
         return rateReturn.flatMap(bl->{
         return rateReturn.flatMap(bl->{
             if(bl){
             if(bl){
                 return  RStatus.success(resMono) ;
                 return  RStatus.success(resMono) ;

+ 6 - 3
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/planNew/PlanNewOutController.java

@@ -114,7 +114,10 @@ public class PlanNewOutController {
      * @return {@link Mono}<{@link RStatus}<{@link DomainSelectRes}>>
      * @return {@link Mono}<{@link RStatus}<{@link DomainSelectRes}>>
      */
      */
     @GetMapping("domain/select")
     @GetMapping("domain/select")
-    public Mono<RStatus<DomainSelectRes>> domainSelect(@RequestParam("code") String code,@RequestParam("type") String type ,@RequestParam("typePlan") Integer typePlan , ServerWebExchange exchange ){
+    public Mono<RStatus<DomainSelectRes>> domainSelect(@RequestParam("code") String code,
+                                                       @RequestParam("type") String type,
+                                                       @RequestParam("typePlan") Integer typePlan,
+                                                       ServerWebExchange exchange ){
         if (1 == typePlan) {
         if (1 == typePlan) {
             Mono<PlanNew> code1 = template
             Mono<PlanNew> code1 = template
                     .selectOne(Query.query(Criteria.where("code").is(code)), PlanNew.class)
                     .selectOne(Query.query(Criteria.where("code").is(code)), PlanNew.class)
@@ -191,7 +194,7 @@ public class PlanNewOutController {
                     return Mono.just(domainSelectRes);
                     return Mono.just(domainSelectRes);
                 });
                 });
             });
             });
-            Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange);
+            Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange,null);
             return rateReturn.flatMap(bl->{
             return rateReturn.flatMap(bl->{
                 if(bl){
                 if(bl){
                     return  RStatus.successList(domainSelectResMono1) ;
                     return  RStatus.successList(domainSelectResMono1) ;
@@ -204,7 +207,7 @@ public class PlanNewOutController {
           //  return RStatus.success(domainSelectResMono1);
           //  return RStatus.success(domainSelectResMono1);
         }else  if(2==typePlan){
         }else  if(2==typePlan){
 
 
-            Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange);
+            Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange,null);
             return rateReturn.flatMap(bl->{
             return rateReturn.flatMap(bl->{
                 if(bl){
                 if(bl){
                     Long start = System.currentTimeMillis();
                     Long start = System.currentTimeMillis();

+ 8 - 2
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/testController.java

@@ -67,10 +67,16 @@ public class testController {
     private BlacklistService blacklistService;
     private BlacklistService blacklistService;
     @Resource
     @Resource
     TweetService tweetService;
     TweetService tweetService;
-
+    @GetMapping("get55")
+    public Mono<Long> get55(@RequestParam("key") String key) {
+        return reactiveRedisTemplate.getExpire(key).flatMap(f->{
+            long seconds = f.getSeconds();
+            return Mono.just(seconds);
+        });
+    }
     @GetMapping("get51")
     @GetMapping("get51")
     public Mono<Boolean>  get51(ServerWebExchange exchange)   {
     public Mono<Boolean>  get51(ServerWebExchange exchange)   {
-        Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange);
+        Mono<Boolean> rateReturn = blacklistService.getRateReturn(exchange,null);
         return rateReturn;
         return rateReturn;
     }
     }
     @GetMapping("get5")
     @GetMapping("get5")

+ 54 - 39
launch-admin/src/main/java/com/webflux/launchadmin/mysql/service/ip/BlacklistService.java

@@ -40,8 +40,8 @@ public class BlacklistService {
     private R2dbcEntityTemplate template;
     private R2dbcEntityTemplate template;
 
 
     //private static final List<String> firstUrl = List.of("out/planNew/hfoInfo","out/planNew/hfoInfo","out/listening/landingPageList");
     //private static final List<String> firstUrl = List.of("out/planNew/hfoInfo","out/planNew/hfoInfo","out/listening/landingPageList");
-    private static  final CopyOnWriteArrayList<String> ip_balck_list = new CopyOnWriteArrayList<>();
-    private static  final CopyOnWriteArrayList<String> ip_white_list = new CopyOnWriteArrayList<>();
+//    private static  final CopyOnWriteArrayList<String> ip_balck_list = new CopyOnWriteArrayList<>();
+//    private static  final CopyOnWriteArrayList<String> ip_white_list = new CopyOnWriteArrayList<>();
     private static  final  AtomicInteger  ipRateLimitRange= new AtomicInteger();//访问次数
     private static  final  AtomicInteger  ipRateLimitRange= new AtomicInteger();//访问次数
     private static  final String key="ip_rate_limit:";//为用户ip访问记录次数ip_rate_limit:1.180.116.135
     private static  final String key="ip_rate_limit:";//为用户ip访问记录次数ip_rate_limit:1.180.116.135
     public static final AtomicReference<String> wxUrl=new AtomicReference<>();
     public static final AtomicReference<String> wxUrl=new AtomicReference<>();
@@ -51,7 +51,7 @@ public class BlacklistService {
      * @param exchange
      * @param exchange
      * @return
      * @return
      */
      */
-    public Mono<Boolean> getRateReturn(ServerWebExchange exchange){
+    public Mono<Boolean> getRateReturn(ServerWebExchange exchange,String unique){
         ServerHttpRequest httpRequest = exchange.getRequest();
         ServerHttpRequest httpRequest = exchange.getRequest();
         InetSocketAddress remoteAddress = httpRequest.getRemoteAddress();
         InetSocketAddress remoteAddress = httpRequest.getRemoteAddress();
         String ipAddress;
         String ipAddress;
@@ -62,7 +62,13 @@ public class BlacklistService {
         }
         }
         if (Objects.nonNull(ipAddress)) {
         if (Objects.nonNull(ipAddress)) {
             try {
             try {
-                return getIpRateNum(ipAddress);
+                return getFrist( unique).flatMap(f->{
+                    if (f) {
+                      return getIpRateNum(ipAddress);
+                    }else {
+                      return Mono.just(true);
+                    }
+                }) ;
             }catch (Exception e){
             }catch (Exception e){
                 log.error("ip黑名单验证:" +ipAddress+"::Error:::"+e.getMessage());
                 log.error("ip黑名单验证:" +ipAddress+"::Error:::"+e.getMessage());
                 return Mono.just(false);
                 return Mono.just(false);
@@ -70,16 +76,25 @@ public class BlacklistService {
         }
         }
         return Mono.just(false);
         return Mono.just(false);
     }
     }
+    private Mono<Boolean> getFrist(String unique){
+        if(Objects.nonNull(unique)){
+           return reactiveRedisTemplate.opsForValue().get(unique)
+                    .switchIfEmpty(reactiveRedisTemplate.opsForValue().set(unique, "1", Duration.ofHours(5L)).flatMap(f-> Mono.just("9999")))
+                    .flatMap(f->{
+                        if ("9999".equals(f)) {
+                            return Mono.just(true);
+                        }else {
+                            return Mono.just(false);
+                        }
+                    });
+        }else {
+            return Mono.just(true);
+        }
+    }
     /**
     /**
      * 获取ip访问次数
      * 获取ip访问次数
      */
      */
     public Mono<Boolean> getIpRateNum(String ip){
     public Mono<Boolean> getIpRateNum(String ip){
-        if (ip_white_list.contains(ip)) {
-            return Mono.just(true);
-        }
-        if (ip_balck_list.contains(ip)) {
-            return Mono.just(false);
-        }
         String keys=key+ip;
         String keys=key+ip;
         long secondsUntilNextMidnight = getSecondsUntilNextMidnight();
         long secondsUntilNextMidnight = getSecondsUntilNextMidnight();
         return reactiveRedisTemplate.opsForValue().get(keys)
         return reactiveRedisTemplate.opsForValue().get(keys)
@@ -141,35 +156,35 @@ public class BlacklistService {
     /**
     /**
      * 数据源同步本地 黑白名单
      * 数据源同步本地 黑白名单
      */
      */
-    @Scheduled(fixedDelay = 160000)
-    public void getIpBalckList(){
-        template.select(Query.query(Criteria.where("status").is(1)), IpWhite.class).collectList().flatMap(ipInfo -> {
-            Map<Integer, List<IpWhite>> collect = ipInfo.stream().filter(f -> Objects.nonNull(f.type())).collect(Collectors.groupingBy(IpWhite::type));
-            List<String> ips = getCollect(collect,1);
-            if(!ips.isEmpty()){
-                List<String> addIps = getAddIps(ips, ip_white_list);
-                if (!addIps.isEmpty()) {
-                    ip_white_list.addAll(addIps);
-                }
-                List<String> removers = getAddIps(ip_white_list, ips);
-                if(!removers.isEmpty()){
-                    ip_white_list.removeAll(removers);
-                }
-            }
-            List<String> ips2 = getCollect(collect,2);
-            if(!ips.isEmpty()){
-                List<String> addIps2 = getAddIps(ips2, ip_balck_list);
-                if (!addIps2.isEmpty()) {
-                    ip_balck_list.addAll(addIps2);
-                }
-                List<String> removers2 = getAddIps(ip_balck_list, ips2);
-                if(!removers2.isEmpty()){
-                    ip_balck_list.removeAll(removers2);
-                }
-            }
-            return Mono.just(ipInfo);
-        }).subscribe();
-    }
+//    @Scheduled(fixedDelay = 160000)
+//    public void getIpBalckList(){
+//        template.select(Query.query(Criteria.where("status").is(1)), IpWhite.class).collectList().flatMap(ipInfo -> {
+//            Map<Integer, List<IpWhite>> collect = ipInfo.stream().filter(f -> Objects.nonNull(f.type())).collect(Collectors.groupingBy(IpWhite::type));
+//            List<String> ips = getCollect(collect,1);
+//            if(!ips.isEmpty()){
+//                List<String> addIps = getAddIps(ips, ip_white_list);
+//                if (!addIps.isEmpty()) {
+//                    ip_white_list.addAll(addIps);
+//                }
+//                List<String> removers = getAddIps(ip_white_list, ips);
+//                if(!removers.isEmpty()){
+//                    ip_white_list.removeAll(removers);
+//                }
+//            }
+//            List<String> ips2 = getCollect(collect,2);
+//            if(!ips.isEmpty()){
+//                List<String> addIps2 = getAddIps(ips2, ip_balck_list);
+//                if (!addIps2.isEmpty()) {
+//                    ip_balck_list.addAll(addIps2);
+//                }
+//                List<String> removers2 = getAddIps(ip_balck_list, ips2);
+//                if(!removers2.isEmpty()){
+//                    ip_balck_list.removeAll(removers2);
+//                }
+//            }
+//            return Mono.just(ipInfo);
+//        }).subscribe();
+//    }
 
 
 
 
     @NotNull
     @NotNull