|
|
|
@ -1,9 +1,15 @@
|
|
|
|
|
package com.mashibing.api.filter.impl;
|
|
|
|
|
|
|
|
|
|
import com.mashibing.api.client.BeaconCacheClient;
|
|
|
|
|
import com.mashibing.api.filter.CheckFilter;
|
|
|
|
|
import com.mashibing.common.constant.CacheConstant;
|
|
|
|
|
import com.mashibing.common.enums.ExceptionEnums;
|
|
|
|
|
import com.mashibing.common.exception.ApiException;
|
|
|
|
|
import com.mashibing.common.model.StandardSubmit;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author kezhen
|
|
|
|
@ -12,8 +18,25 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
@Service(value = "ip")
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class IPCheckFilter implements CheckFilter {
|
|
|
|
|
|
|
|
|
|
private final String IP_ADDRESS = "ipAddress";
|
|
|
|
|
@Autowired
|
|
|
|
|
private BeaconCacheClient cacheClient;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void check(StandardSubmit submit) {
|
|
|
|
|
log.info("【接口模块-校验IP】 校验ing…………");
|
|
|
|
|
//根据 cacheClient 根据客户的apikey以及ipaddress去查询客户的IP白名单
|
|
|
|
|
String ip = cacheClient.hGetString(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), IP_ADDRESS);
|
|
|
|
|
submit.setIp(ip);
|
|
|
|
|
|
|
|
|
|
// 如果ip白名单为null,或者ip与请求的ip一致,则放行
|
|
|
|
|
if (StringUtils.isEmpty(ip) || ip.contains(submit.getRealIP())) {
|
|
|
|
|
log.info("【接口模块-校验IP】 客户端请求IP合法!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.info("【接口模块-校验IP】 请求的ip不在白名单内");
|
|
|
|
|
throw new ApiException(ExceptionEnums.IP_NOT_WHITE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|