1. 程式人生 > >接口訪問加密和限頻方案

接口訪問加密和限頻方案

temp byte 用戶 charset null 訪問 () ets stand

需求:安全性要求較高的接口暴露到公網中,需要進行加密和限頻.

案例:根據用戶手機號查詢用戶ID,需要防止根據phone庫非法掃接口,
1.返回uid加密:
采用RSA加密,私鑰加密,公鑰解密,為了混淆結果,無論有無uid都返回加密結果。當無結果會根據phone md5截取7位
作為偽號,使用另一私鑰加密。如果公鑰解密失敗則說明無對應uid。
2.限頻,根據key=access_token 50次每天。limit50; timeunit:day
public long incr(String key) {
long count = redisTemplate.opsForValue().increment(key, 1);

if (count == 1) {
// 暫時不考慮設置超時時間失敗的情況
redisTemplate.expire(key, 1, timeUnit);
}
if (count > limit) {
// 超過臨界值之後, 每次錯誤都延長記錄有效期
redisTemplate.expire(key, 1, timeUnit);
}
return count;
}
public long getCountByKey(String key){
long count = redisTemplate.execute((RedisCallback<Long>) (conn) -> {
byte[] countStr = conn.get(key.getBytes(StandardCharsets.UTF_8));
if (countStr == null) {
return 0L;
}
return Long.parseLong(new String(countStr));
});
return count;
}
public void check(String key) {
if (getCountByKey(key) > limit) {
throw new TooManyFailsException();
}
}

接口訪問加密和限頻方案