Commit bc6a5347 authored by 李欣峰's avatar 李欣峰

<dev>

1.修改调用api时,限制调用总次数,当调用一次以后,apiAuth表内的limit_num字段-1,当该字段为0时,返回调用次数权限不足提示
parent 8705a820
...@@ -50,7 +50,6 @@ public class RefreshApiLimitSchedule { ...@@ -50,7 +50,6 @@ public class RefreshApiLimitSchedule {
JSONObject o = (JSONObject) data.get(i); JSONObject o = (JSONObject) data.get(i);
String name = o.get("name").toString(); String name = o.get("name").toString();
Integer singleLimit = Integer.parseInt(o.get("single_limit").toString()); Integer singleLimit = Integer.parseInt(o.get("single_limit").toString());
// log.info("接口{}限流次数{}",name, singleLimit);
//桶的最大容量,即能装载 Token 的最大数量 //桶的最大容量,即能装载 Token 的最大数量
int capacity = singleLimit; int capacity = singleLimit;
//每次 Token 补充量 //每次 Token 补充量
......
...@@ -28,7 +28,6 @@ import com.zorkdata.dddlib.core.sdk.InvokeResult; ...@@ -28,7 +28,6 @@ import com.zorkdata.dddlib.core.sdk.InvokeResult;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.checkerframework.common.reflection.qual.Invoke;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -440,7 +439,7 @@ public class ApiServiceApplicationImpl implements ApiApplication { ...@@ -440,7 +439,7 @@ public class ApiServiceApplicationImpl implements ApiApplication {
predicates.add(keywordPredicate1); predicates.add(keywordPredicate1);
} }
if (StringUtils.isNotBlank(datasource)) { if (StringUtils.isNotBlank(datasource)) {
String datasourceWord = StrUtil.builder("%", "\"name\""+":"+"\""+datasource, "%").toString(); String datasourceWord = StrUtil.builder("%", "\"name\"" + ":" + "\"" + datasource, "%").toString();
Predicate keywordPredicate2 = criteriaBuilder.and(criteriaBuilder.like(root.get("datasource"), datasourceWord)); Predicate keywordPredicate2 = criteriaBuilder.and(criteriaBuilder.like(root.get("datasource"), datasourceWord));
predicates.add(keywordPredicate2); predicates.add(keywordPredicate2);
} }
...@@ -793,6 +792,7 @@ public class ApiServiceApplicationImpl implements ApiApplication { ...@@ -793,6 +792,7 @@ public class ApiServiceApplicationImpl implements ApiApplication {
//获取api的id,根据apiId去查询该id的Api给哪些用户授权了,对其进行限制 //获取api的id,根据apiId去查询该id的Api给哪些用户授权了,对其进行限制
Integer apiId = api.getId(); Integer apiId = api.getId();
String createdBy = api.getCreatedBy(); String createdBy = api.getCreatedBy();
//1.判断是不是创建者,如果是创建者,直接跳过,可以直接调用,不受权限控制 //1.判断是不是创建者,如果是创建者,直接跳过,可以直接调用,不受权限控制
if (!username.equals(createdBy)) { if (!username.equals(createdBy)) {
Map<Date, Date> startTimeAndendTime = apiAuthRepository.findApiAuthByApiId(apiId, userid); Map<Date, Date> startTimeAndendTime = apiAuthRepository.findApiAuthByApiId(apiId, userid);
...@@ -808,11 +808,20 @@ public class ApiServiceApplicationImpl implements ApiApplication { ...@@ -808,11 +808,20 @@ public class ApiServiceApplicationImpl implements ApiApplication {
//4.比较时间,符合 start > nowdate > end,不符合的话直接返回,权限不足 //4.比较时间,符合 start > nowdate > end,不符合的话直接返回,权限不足
if (i1 != 1 || i2 != 1) { if (i1 != 1 || i2 != 1) {
invokeResult = InvokeResult.fail(); invokeResult = InvokeResult.fail();
invokeResult.setMessage("权限不足!"); invokeResult.setMessage("权限不足,不在权限设置时间内!");
return invokeResult; return invokeResult;
} }
} }
} }
//查看当前用户在当前api还剩下多少次调用次数,limit>0可调用,否则不可调用
Integer limit = apiAuthRepository.findApiAuthLimitByApiId(apiId, userid);
if (limit <= 0) {
invokeResult = InvokeResult.fail();
invokeResult.setMessage("权限不足,调用次数不足!");
return invokeResult;
}
} }
//0未发布 , 1发布 //0未发布 , 1发布
Integer status = apiRepository.findStatusByName(apiName); Integer status = apiRepository.findStatusByName(apiName);
...@@ -862,7 +871,8 @@ public class ApiServiceApplicationImpl implements ApiApplication { ...@@ -862,7 +871,8 @@ public class ApiServiceApplicationImpl implements ApiApplication {
apiCall.setSuccess(false); apiCall.setSuccess(false);
} }
apiCallRepository.save(apiCall); apiCallRepository.save(apiCall);
//每次调用完成以后,ApiAuth表内limit_num字段次数减少1
apiAuthRepository.updateApiAuthLimitNum(apiId, userid);
return invokeResult; return invokeResult;
} }
......
...@@ -5,7 +5,9 @@ import com.zorkdata.apiservice.domain.dto.ApiAuthDTO; ...@@ -5,7 +5,9 @@ import com.zorkdata.apiservice.domain.dto.ApiAuthDTO;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -74,4 +76,26 @@ public interface ApiAuthRepository extends JpaRepository<ApiAuth, Integer>, JpaS ...@@ -74,4 +76,26 @@ public interface ApiAuthRepository extends JpaRepository<ApiAuth, Integer>, JpaS
*/ */
@Query(value = "select count(*) from api_auth where api_id= ?1", nativeQuery = true) @Query(value = "select count(*) from api_auth where api_id= ?1", nativeQuery = true)
Integer findAuthApiIdById(Integer id); Integer findAuthApiIdById(Integer id);
/**
* 根据apiid查询ApiAuth里limit次数
*
* @param apiId
* @param userid
* @return
*/
@Query(value = "select limit_num from api_auth where api_id= ?1 and user_id = ?2", nativeQuery = true)
Integer findApiAuthLimitByApiId(Integer apiId, Integer userid);
/**
* 更新ApiAuth表内Lmit_num次数
*
* @param apiId
* @param userid
*/
@Modifying
@Transactional(rollbackFor = Exception.class)
@Query(value = "update api_auth set limit_num = limit_num - 1 where api_id= ?1 and user_id = ?2", nativeQuery = true)
void updateApiAuthLimitNum(Integer apiId, Integer userid);
} }
package com.zorkdata.apiservice.domain.repository; package com.zorkdata.apiservice.domain.repository;
import com.zorkdata.apiservice.domain.domain.Api; import com.zorkdata.apiservice.domain.domain.Api;
import com.zorkdata.apiservice.domain.dto.ApiDTO;
import com.zorkdata.dddlib.domain.EntityRepository; import com.zorkdata.dddlib.domain.EntityRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.*; import java.util.Date;
import java.util.List;
import java.util.Map;
/** /**
* @title: ApiServiceRepositoryB * @title: ApiServiceRepositoryB
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment