Browse Source

接口访问次数限制

yurk 2 years ago
parent
commit
0772120f81

+ 36 - 0
fhKeeper/formulahousekeeper/management-platform/pom.xml

@@ -30,6 +30,12 @@
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
         </dependency>
+        <!--easyExcel-->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>2.2.4</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
@@ -38,6 +44,12 @@
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
         </dependency>
+        <!--Map依赖 -->
+        <dependency>
+            <groupId>net.jodah</groupId>
+            <artifactId>expiringmap</artifactId>
+            <version>0.5.10</version>
+        </dependency>
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
@@ -126,6 +138,24 @@
             <artifactId>aspectjweaver</artifactId>
         </dependency>
 
+        <!-- logback -->
+        <!--<dependency>
+            <groupId>net.logstash.logback</groupId>
+            <artifactId>logstash-logback-encoder</artifactId>
+            <version>6.3</version>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>1.2.3</version>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-core</artifactId>
+            <version>1.2.3</version>
+        </dependency>-->
+
+
         <!--     tess4j相关依赖   -->
         <!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
         <dependency>
@@ -167,6 +197,12 @@
             <artifactId>UserAgentUtils</artifactId>
             <version>1.21</version>
         </dependency>
+        <!--热部署-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-devtools</artifactId>
+            <version>2.0.1.RELEASE</version>
+        </dependency>
 
     </dependencies>
 

+ 60 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/aop/LimitRequestAspect.java

@@ -0,0 +1,60 @@
+package com.management.platform.aop;
+
+import com.management.platform.config.LimitRequest;
+import com.management.platform.util.HttpRespMsg;
+import net.jodah.expiringmap.ExpirationPolicy;
+import net.jodah.expiringmap.ExpiringMap;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+@Aspect
+@Component
+public class LimitRequestAspect {
+    private static ConcurrentHashMap<String, ExpiringMap<String, Integer>> book = new ConcurrentHashMap<>();
+    // 定义切点
+    // 让所有有@LimitRequest注解的方法都执行切面方法
+    @Pointcut("@annotation(limitRequest)")
+    public void excudeService(LimitRequest limitRequest) {
+    }
+
+    @Around("excudeService(limitRequest)")
+    public HttpRespMsg doAround(ProceedingJoinPoint pjp, LimitRequest limitRequest) throws Throwable {
+        HttpRespMsg httpRespMsg=new HttpRespMsg();
+        // 获得request对象
+        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
+        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
+        HttpServletRequest request = sra.getRequest();
+
+        // 获取Map对象, 如果没有则返回默认值
+        // 第一个参数是key, 第二个参数是默认值
+        ExpiringMap<String, Integer> map = book.getOrDefault(request.getRequestURI(), ExpiringMap.builder().variableExpiration().build());
+        Integer uCount = map.getOrDefault(request.getRemoteAddr(), 0);
+
+
+        if (uCount >= limitRequest.count()) { // 超过次数,不执行目标方法
+            //这里的返回对象类型根据controller方法的返回方式一致
+            httpRespMsg.setError("接口访问次数超过限制,请1分钟后重试");
+            return httpRespMsg;
+        } else if (uCount == 0){ // 第一次请求时,设置开始有效时间
+            map.put(request.getRemoteAddr(), uCount + 1, ExpirationPolicy.CREATED, limitRequest.time(), TimeUnit.MILLISECONDS);
+        } else { // 未超过次数, 记录数据加一
+            map.put(request.getRemoteAddr(), uCount + 1);
+        }
+        book.put(request.getRequestURI(), map);
+
+        // result的值就是被拦截方法的返回值
+        httpRespMsg.data= pjp.proceed();
+        return httpRespMsg;
+    }
+
+}

+ 12 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/config/LimitRequest.java

@@ -0,0 +1,12 @@
+package com.management.platform.config;
+
+import java.lang.annotation.*;
+
+@Documented
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface LimitRequest {
+    //毫秒,分钟,小时 之间的转换用算数
+    long time() default 60000; // 限制时间 单位:毫秒
+    int count() default Integer.MAX_VALUE; // 允许请求的次数
+}

+ 2 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/ReportController.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.github.pagehelper.util.StringUtil;
+import com.management.platform.config.LimitRequest;
 import com.management.platform.entity.*;
 import com.management.platform.entity.vo.SysRichFunction;
 import com.management.platform.entity.vo.WorktimeItem;
@@ -1229,6 +1230,7 @@ public class ReportController {
     }
 
     /*提供第三方接口获取日报数据*/
+    @LimitRequest(count = 10)
     @PostMapping("/getReportListByToken")
     public HttpRespMsg getReportListByToken(@RequestParam String token, @RequestParam String startDate, @RequestParam String endDate){
         return reportService.getReportListByToken(token,startDate,endDate);

+ 7 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/ReportServiceImpl.java

@@ -40,6 +40,7 @@ import java.text.SimpleDateFormat;
 import java.time.Duration;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeParseException;
 import java.time.temporal.ChronoUnit;
@@ -3966,6 +3967,12 @@ public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> impleme
         HttpRespMsg msg=new HttpRespMsg();
         DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd");
         DateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        LocalDate openDate = LocalDate.parse(startDate, df);
+        LocalDate closeDate = LocalDate.parse(endDate, df);
+        if(Duration.between(openDate.atTime(LocalTime.now()),closeDate.atTime(LocalTime.now())).toDays()>31){
+            msg.setError("获取数据时间段不得超过一个月");
+            return msg;
+        }
         List<ThirdPartyInterface> thirdPartyInterfaceList = thirdPartyInterfaceMapper.selectList(new QueryWrapper<ThirdPartyInterface>().eq("token", token));
         if(thirdPartyInterfaceList.size()==0){
             msg.setError("token错误");

+ 2 - 2
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/task/TimingTask.java

@@ -253,7 +253,7 @@ public class TimingTask {
                     report.setProjectAuditState(1);
                     report.setReportAutoApprove(1);
                     reportMapper.updateById(report);
-                    Optional<User> first = userList.stream().filter(u -> u.getId().equals(report.getCreatorId())).findFirst();
+                    /*Optional<User> first = userList.stream().filter(u -> u.getId().equals(report.getCreatorId())).findFirst();
                     if(first.isPresent()){
                         Optional<CompanyDingding> optional = companyDingdingList.stream().filter(cm -> cm.getCompanyId().equals(company.getId())).findFirst();
                         if(optional.isPresent()){
@@ -284,7 +284,7 @@ public class TimingTask {
                             json.put("content_item",dataJson);
                             wxCorpInfoService.sendWXCorpTemplateMsg(corpInfo.get(),first.get().getCorpwxUserid(),json);
                         }
-                    }
+                    }*/
                 }
             }
         }