IP地址校验

master
kezhen 2 years ago
parent b407484298
commit 9b8b191bd5

@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author kezhen
@ -46,4 +47,20 @@ public class CacheController {
log.info("【缓存模块】hGetAll 方法获取key = {} 的数据 value = {}", key, value);
return value;
}
@GetMapping("/cache/hget/{key}/{field}")
public Object hGet(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field) {
log.info("【缓存模块】 hget 方法获取key = {}field = {} 的数据", key, field);
Object value = redisClient.getMapItem(key, field);
log.info("【缓存模块】 hget 方法获取key = {}field = {} 的数据 value = {}", key, field, value);
return value;
}
@GetMapping("/cache/semember/{key}")
public Set semember(@PathVariable(value = "key") String key) {
log.info("【缓存模块 semember 方法】获取key = {}", key);
Set<Object> value = redisClient.sGet(key);
log.info("【缓存模块 semember 方法】获取key = {} 的数据 value = {}", key, value);
return value;
}
}

@ -19,5 +19,4 @@ public class ApiExceptionHandler{
return R.error(ex);
}
}

@ -16,4 +16,10 @@ public interface BeaconCacheClient {
@GetMapping("/cache/hgetall/{key}")
Map hGetAll(@PathVariable(value = "key") String key);
@GetMapping("/cache/hget/{key}/{field}")
Object hGet(@PathVariable(value = "key") String key,@PathVariable(value = "field") String field);
@GetMapping("/cache/hget/{key}/{field}")
String hGetString(@PathVariable(value = "key") String key,@PathVariable(value = "field") String field);
}

@ -14,7 +14,7 @@ import java.util.Map;
/**
* @author zjw
* @description apikey
* @description apikey
*/
@Service(value = "apikey")
@Slf4j
@ -31,13 +31,15 @@ public class ApiKeyCheckFilter implements CheckFilter {
Map clientBusiness = cacheClient.hGetAll(CacheConstant.CLIENT_BUSINESS + submit.getApikey());
//2. 如果为null直接扔异常
if(clientBusiness == null || clientBusiness.size() == 0){
log.info("【接口模块-校验apikey】 非法的apikey = {}",submit.getApikey());
if (clientBusiness == null || clientBusiness.size() == 0) {
log.info("【接口模块-校验apikey】 非法的apikey = {}", submit.getApikey());
throw new ApiException(ExceptionEnums.ERROR_APIKEY);
}
//3. 正常封装数据
// submit.setClientId(Long.parseLong(clientBusiness.get("id") + ""));
log.info("【接口模块-校验apikey】 查询到客户信息 clientBusiness = {}",clientBusiness);
System.out.println("clientBusiness.get(\"id\") = " + clientBusiness.get("id"));
submit.setCliendId(Long.parseLong(clientBusiness.get("id") + ""));
log.info("【接口模块-校验apikey】 查询到客户信息 clientBusiness = {}", clientBusiness);
}
}

@ -1,8 +1,10 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.client.BeaconCacheClient;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
@ -12,8 +14,13 @@ import org.springframework.stereotype.Service;
@Service(value = "fee")
@Slf4j
public class FeeCheckFilter implements CheckFilter {
@Autowired
private BeaconCacheClient cacheClient;
@Override
public void check(StandardSubmit submit) {
log.info("【接口模块-校验客户余额】 校验ing…………");
}
}

@ -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);
}
}

@ -6,8 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
/**
* @author kezhen
* @description

Loading…
Cancel
Save