添加客户API调用量使用90%发送kafka消息
This commit is contained in:
+107
-61
@@ -14,6 +14,7 @@ import com.fangjia.open.model.RestfulModelAndView;
|
||||
import com.fangjia.service.open.ApiUserAuthorityService;
|
||||
import com.fangjia.service.open.ApiVisitCountService;
|
||||
import com.fangjia.service.sql.ApiUserService;
|
||||
import com.fangjia.service.util.kafka.KafkaProducerUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -30,12 +31,13 @@ import java.util.*;
|
||||
* API拦截器
|
||||
*
|
||||
* @author
|
||||
*
|
||||
*/
|
||||
public class AuthenticationInterceptor implements Interceptor {
|
||||
|
||||
|
||||
private ApiUserService apiUserService;
|
||||
|
||||
private KafkaProducerUtils kafkaProducerUtils;
|
||||
private ApiUserAuthorityService apiUserAuthorityService;
|
||||
private ApiVisitCountService apiVisitCountService;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationInterceptor.class);
|
||||
@@ -43,9 +45,13 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
private static final List<String> HAS_SUB_SERVICE_URIS = new ArrayList<String>();
|
||||
private static final String SUB_SERVICE_SEPARATOR = "#";
|
||||
private static final String SERVICE_CODE_PARAM_NAME = "serviceCode";
|
||||
private static final String SMS_DATA_TOPIC = "sms-data-topic";
|
||||
private static final String SMS_DATA_TOPIC_PROFIX = "sendSms90-";
|
||||
|
||||
static {
|
||||
HAS_SUB_SERVICE_URIS.add("/property/transaction");
|
||||
}
|
||||
|
||||
/**
|
||||
* token过期时间 单位:s
|
||||
*/
|
||||
@@ -62,13 +68,13 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
|
||||
try {
|
||||
HttpCode httpCode = null;
|
||||
if(!OpenConfig.getConfig().isOpenAllAuthority()) {
|
||||
if (!OpenConfig.getConfig().isOpenAllAuthority()) {
|
||||
String token = request.getParameter("token");
|
||||
if(isNeedToken(authority)){
|
||||
if (isNeedToken(authority)) {
|
||||
httpCode = authorized(token, request);
|
||||
}else if(StringUtils.isNotBlank(token)){
|
||||
} else if (StringUtils.isNotBlank(token)) {
|
||||
ApiUser apiUser = parseApiUserByToken(FangjiaApiUtil.parse2Token(token));
|
||||
if(apiUser != null){
|
||||
if (apiUser != null) {
|
||||
User user = apiUserService.findUserById(apiUser.getId());
|
||||
AuthenticationBean.get(request, apiUser, user, null);
|
||||
}
|
||||
@@ -77,18 +83,18 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
//功能方法没有设置权限或者有该方法权限
|
||||
if (authority == null || authority.role() == Role.NONE || null == httpCode) {
|
||||
Object obj;
|
||||
try{
|
||||
try {
|
||||
obj = invocation.invoke();
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}
|
||||
return obj;
|
||||
}else {
|
||||
if(httpCode.getErrorCode() == 4003){
|
||||
} else {
|
||||
if (httpCode.getErrorCode() == 4003) {
|
||||
BasicModel basicModel = BasicModel.buildBadRequestModel(httpCode.getErrorMessage().toString());
|
||||
ret.setModel(basicModel);
|
||||
return ret;
|
||||
}else{
|
||||
} else {
|
||||
String msg = httpCode.getMessage() + "(" + httpCode.getErrorMessage() + ")";
|
||||
BasicModel basicModel = BasicModel.buildUnAuthorizedModel().setMsg(msg);
|
||||
ret.setModel(basicModel);
|
||||
@@ -105,7 +111,7 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
|
||||
}
|
||||
|
||||
private boolean isNeedToken(Authority authority){
|
||||
private boolean isNeedToken(Authority authority) {
|
||||
return authority != null && authority.role() != null && authority.role() != Role.NONE;
|
||||
}
|
||||
|
||||
@@ -119,15 +125,15 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
Long apiVisitCount;
|
||||
if (upperLimit != null && upperLimit > 0) {
|
||||
apiVisitCount = apiVisitCountService.findApiVisitCount(auth.getUserId(), uri, null, visitDate);
|
||||
if(apiVisitCount >= upperLimit){
|
||||
if (apiVisitCount >= upperLimit) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if(hasSubService(uri) && StringUtils.isNotBlank(serviceCode)){
|
||||
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||
upperLimit = auth.getLimitCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||
if (upperLimit != null && upperLimit > 0) {
|
||||
apiVisitCount = apiVisitCountService.findApiVisitCount(auth.getUserId(), uri, serviceCode, visitDate);
|
||||
if(apiVisitCount >= upperLimit){
|
||||
if (apiVisitCount >= upperLimit) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -135,18 +141,18 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isReachDeadline(ApiUserAuthority auth, String uri, String serviceCode){
|
||||
private boolean isReachDeadline(ApiUserAuthority auth, String uri, String serviceCode) {
|
||||
|
||||
if(auth.getDeadlineMap() == null){
|
||||
if (auth.getDeadlineMap() == null) {
|
||||
return false;
|
||||
}
|
||||
Date deadline = auth.getDeadlineMap().get(uri);
|
||||
if(null != deadline && deadline.before(new Date())){
|
||||
if (null != deadline && deadline.before(new Date())) {
|
||||
return true;
|
||||
}
|
||||
if(hasSubService(uri) && StringUtils.isNotBlank(serviceCode)){
|
||||
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||
deadline = auth.getDeadlineMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||
if(null != deadline && deadline.before(new Date())){
|
||||
if (null != deadline && deadline.before(new Date())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -160,21 +166,50 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(auth.getTotalCountMap() != null && auth.getTotalCountMap().get(uri) != null){
|
||||
if (auth.getTotalCountMap() != null && auth.getTotalCountMap().get(uri) != null) {
|
||||
Integer totalLimit = auth.getTotalCountMap().get(uri);
|
||||
Long totalApiVisitCount;
|
||||
if(totalLimit != null && totalLimit > 0){
|
||||
if (totalLimit != null && totalLimit > 0) {
|
||||
totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||
if(totalApiVisitCount >= totalLimit){
|
||||
if (totalApiVisitCount >= totalLimit) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(hasSubService(uri) && StringUtils.isNotBlank(serviceCode)){
|
||||
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||
Integer totalLimit = auth.getTotalCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||
if(totalLimit != null && totalLimit > 0){
|
||||
if (totalLimit != null && totalLimit > 0) {
|
||||
Long totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||
if(totalApiVisitCount >= totalLimit){
|
||||
if (totalApiVisitCount >= totalLimit) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isReach90SumLimit(ApiUserAuthority auth, String uri, String serviceCode) {
|
||||
|
||||
if (auth == null || auth.getTotalCountMap() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (auth.getTotalCountMap() != null && auth.getTotalCountMap().get(uri) != null) {
|
||||
Integer totalLimit = auth.getTotalCountMap().get(uri);
|
||||
Long totalApiVisitCount;
|
||||
if (totalLimit != null && totalLimit > 0) {
|
||||
totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||
if (totalApiVisitCount.intValue() == (int) (totalLimit * 0.9)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||
Integer totalLimit = auth.getTotalCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||
if (totalLimit != null && totalLimit > 0) {
|
||||
Long totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||
if (totalApiVisitCount.intValue() == (int) (totalLimit * 0.9)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -213,42 +248,42 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
if (null == apiUser) {
|
||||
return ret;
|
||||
}
|
||||
if(apiUser.getDeadTime() != null && DateUtil.truncateDay(new Date()).after(apiUser.getDeadTime())){
|
||||
if (apiUser.getDeadTime() != null && DateUtil.truncateDay(new Date()).after(apiUser.getDeadTime())) {
|
||||
return new HttpCode(OpenHttpCodeEnum.USER_EXPIRED);
|
||||
}
|
||||
if(!isOldUser(apiUser) && isExpired(token)){
|
||||
if (!isOldUser(apiUser) && isExpired(token)) {
|
||||
return new HttpCode(OpenHttpCodeEnum.TOKEN_EXPIRED);
|
||||
}
|
||||
if (1 == apiUser.getStatus()) {
|
||||
return new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "你的账户已失效");
|
||||
}
|
||||
if(StringUtils.isNotBlank(apiUser.getIp())){
|
||||
if (StringUtils.isNotBlank(apiUser.getIp())) {
|
||||
String ip = AnalyticsUtil.getIpAddress(request);
|
||||
if(!isAuthIp(ip,apiUser)){
|
||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD,"ip[" + ip + "]无权限访问API");
|
||||
if (!isAuthIp(ip, apiUser)) {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "ip[" + ip + "]无权限访问API");
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
String uri = request.getRequestURI();
|
||||
ApiUserAuthority auth = this.apiUserAuthorityService.findByUserId(apiUser.getId());
|
||||
if(auth != null){
|
||||
User user = apiUserService.findUserById(apiUser.getId());
|
||||
if (auth != null) {
|
||||
AuthenticationBean.get(request, apiUser, user, auth);
|
||||
}
|
||||
uri = OpenConfig.getConfig().isDevelopmentEnvironment() ? uri.replace("/fangjia-open", "") : uri;
|
||||
// 检查是否有访问该Action的权限
|
||||
boolean authAction = null != auth && auth.getOpenResourseUris().contains(uri);
|
||||
String cityName = request.getParameter("city");
|
||||
if(StringUtils.isBlank(cityName)){
|
||||
if (StringUtils.isBlank(cityName)) {
|
||||
cityName = request.getParameter("cityName");
|
||||
}
|
||||
if (StringUtils.isNotBlank(cityName)) {
|
||||
boolean authCity = StringUtils.isNotBlank(apiUser.getCity()) && !StringUtils.equalsIgnoreCase(apiUser.getCity(), "all");
|
||||
if(authCity){
|
||||
if (authCity) {
|
||||
cityName = request.getParameter("city");
|
||||
cityName = RegionData.formatCity(cityName);
|
||||
if(!RegionData.isCity(cityName)){
|
||||
if (!RegionData.isCity(cityName)) {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.ARGUMENT_INVALID);
|
||||
ret.setErrorMessage("城市非法");
|
||||
return ret;
|
||||
@@ -260,19 +295,19 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "您未开通城市" + (StringUtils.isNotBlank(cityName) ? cityName : StringUtils.EMPTY) + "的权限");
|
||||
}
|
||||
if (authAction && authCity) {
|
||||
if(hasSubService(uri)){
|
||||
if (hasSubService(uri)) {
|
||||
List<String> serviceCodes = getServiceCode(request);
|
||||
for(String serviceCode : serviceCodes){
|
||||
if(!auth.getOpenResourseUris().contains(uri + SUB_SERVICE_SEPARATOR + serviceCode)){
|
||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD ,"无权限访问服务:" + serviceCode);
|
||||
for (String serviceCode : serviceCodes) {
|
||||
if (!auth.getOpenResourseUris().contains(uri + SUB_SERVICE_SEPARATOR + serviceCode)) {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "无权限访问服务:" + serviceCode);
|
||||
return ret;
|
||||
}
|
||||
if(isReachDeadline(auth, uri, serviceCode)){
|
||||
if (isReachDeadline(auth, uri, serviceCode)) {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.SERVICE_EXPIRED, serviceCode + "服务已过期");
|
||||
return ret;
|
||||
}
|
||||
if(isReachUpperLimit(auth, uri, serviceCode)){
|
||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT ,serviceCode + "今日已达到配额上限");
|
||||
if (isReachUpperLimit(auth, uri, serviceCode)) {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, serviceCode + "今日已达到配额上限");
|
||||
return ret;
|
||||
}
|
||||
// 检测是否达到总数访问上限
|
||||
@@ -280,10 +315,14 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, serviceCode + "已达到配额上限");
|
||||
return ret;
|
||||
}
|
||||
// 检测是否达到总数的90%
|
||||
if (isReach90SumLimit(auth, uri, serviceCode)) {
|
||||
kafkaProducerUtils.send(SMS_DATA_TOPIC, SMS_DATA_TOPIC_PROFIX + apiUser.getPhone() + "-" + user.getUserName() + "-" + uri);
|
||||
}
|
||||
}
|
||||
|
||||
}else{
|
||||
if(isReachDeadline(auth, uri, null)){
|
||||
} else {
|
||||
if (isReachDeadline(auth, uri, null)) {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "服务已过期");
|
||||
return ret;
|
||||
}
|
||||
@@ -297,6 +336,10 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "已达到配额上限");
|
||||
return ret;
|
||||
}
|
||||
// 检测是否达到总数的90%
|
||||
if (isReach90SumLimit(auth, uri, null)) {
|
||||
kafkaProducerUtils.send(SMS_DATA_TOPIC, SMS_DATA_TOPIC_PROFIX + apiUser.getPhone() + "-" + user.getUserName() + "-" + uri);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -305,16 +348,14 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private List<String> getServiceCode(HttpServletRequest request){
|
||||
private List<String> getServiceCode(HttpServletRequest request) {
|
||||
|
||||
String serviceCode = request.getParameter(SERVICE_CODE_PARAM_NAME);
|
||||
if(StringUtils.isBlank(serviceCode)){
|
||||
if (StringUtils.isBlank(serviceCode)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return Arrays.asList(serviceCode.split(",|,"));
|
||||
@@ -322,44 +363,46 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
|
||||
/**
|
||||
* 判断该API是否拥有子权限
|
||||
*
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
private boolean hasSubService(String uri){
|
||||
private boolean hasSubService(String uri) {
|
||||
return HAS_SUB_SERVICE_URIS.contains(uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* 2016年06月06日以前的用户是老用户,token不过期
|
||||
*
|
||||
* @param apiUser
|
||||
* @return
|
||||
*/
|
||||
private boolean isOldUser(ApiUser apiUser){
|
||||
private boolean isOldUser(ApiUser apiUser) {
|
||||
return OLD_USER_SPLITTED_DATE.after(apiUser.getCreationDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断Token是否过期
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
private boolean isExpired(Token token){
|
||||
private boolean isExpired(Token token) {
|
||||
return System.currentTimeMillis() / 1000 - token.getTimestamp() > TOKEN_EXPIRED_TIME;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param ip 验证客户IP是否在注册的白名单中
|
||||
* @param apiUser
|
||||
* @return
|
||||
*/
|
||||
private boolean isAuthIp(String ip,ApiUser apiUser){
|
||||
if(StringUtils.isBlank(ip) || StringUtils.isBlank(apiUser.getIp())){
|
||||
private boolean isAuthIp(String ip, ApiUser apiUser) {
|
||||
if (StringUtils.isBlank(ip) || StringUtils.isBlank(apiUser.getIp())) {
|
||||
return false;
|
||||
}
|
||||
String[] ips = apiUser.getIp().split("\n");
|
||||
for(String authIp:ips){
|
||||
if(StringUtils.isNotBlank(authIp) && ip.startsWith(authIp.replaceAll("\\s", ""))){
|
||||
for (String authIp : ips) {
|
||||
if (StringUtils.isNotBlank(authIp) && ip.startsWith(authIp.replaceAll("\\s", ""))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -378,9 +421,9 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
if (StringUtils.isBlank(cityName)) {
|
||||
return true;
|
||||
}
|
||||
if(!RegionData.isCity(cityName)){
|
||||
if (!RegionData.isCity(cityName)) {
|
||||
String city = RegionData.formatCity(cityName);
|
||||
if(null != city){
|
||||
if (null != city) {
|
||||
cityName = city;
|
||||
}
|
||||
}
|
||||
@@ -395,16 +438,16 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
return true;
|
||||
}
|
||||
|
||||
private ApiUser parseApiUserByToken(Token token){
|
||||
private ApiUser parseApiUserByToken(Token token) {
|
||||
|
||||
if(token == null){
|
||||
if (token == null) {
|
||||
return null;
|
||||
}
|
||||
ApiUser apiUser = apiUserService.findApiUserById(token.getUserId());
|
||||
if(apiUser == null){
|
||||
if (apiUser == null) {
|
||||
return null;
|
||||
}
|
||||
if(!apiUser.getAppKey().equals(token.getAppKey())){
|
||||
if (!apiUser.getAppKey().equals(token.getAppKey())) {
|
||||
return null;
|
||||
}
|
||||
return apiUser;
|
||||
@@ -448,4 +491,7 @@ public class AuthenticationInterceptor implements Interceptor {
|
||||
this.apiVisitCountService = apiVisitCountService;
|
||||
}
|
||||
|
||||
public void setKafkaProducerUtils(KafkaProducerUtils kafkaProducerUtils) {
|
||||
this.kafkaProducerUtils = kafkaProducerUtils;
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -2,6 +2,7 @@ package com.fangjia.service.util.kafka;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
@@ -39,6 +40,12 @@ public class KafkaProducerUtils implements DisposableBean {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void send(String topic,String msg){
|
||||
if (StringUtils.isNotBlank(topic) && StringUtils.isNotBlank(msg)){
|
||||
ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, msg);
|
||||
this.producer.send(record);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user