jeecg限制用户访问次数
**防止客户恶意点击或者爬虫不断访问接口对接口进行次数限制**
*版本1 超过一定时间内次数不到指定次数返回不可访问,会产生一定时间内连续点击,只要不超过100次*
public class RedisCountUtils {
private static final Logger logger = LoggerFactory . getLogger ( RedisCountUtils . class ) ;
public static final long MORE_TIME = 30 * 60 * 1000 ;
public static final long EXPIRE_TIME = 60 * 60 * 1000 ;
public static final int VISITS_TIME = 100 ;
public static final long SAVE_TIME = 5 * 60 * 1000 ;
public static boolean CheckVisitsCount ( RedisUtil redisUtil, String message) {
if ( oConvertUtils. isEmpty ( message) ) {
return false ;
}
int limitCount = 0 ;
Object obj = redisUtil. hget ( CommonConstant . CSOM_CHECK + message, message) ;
String limitCountKey = "LIMIT_COUNT" + message;
String limitCountItem = message+ "_limitCount" ;
if ( redisUtil. hget ( limitCountKey, limitCountItem) == null ) {
limitCount = 1 ;
} else {
limitCount = ( int ) redisUtil. hget ( limitCountKey, limitCountItem) ;
}
if ( obj instanceof Long ) {
long time = ( Long ) obj;
if ( System . currentTimeMillis ( ) - time < EXPIRE_TIME* 2 ) {
limitCount ++ ;
redisUtil. hset ( limitCountKey, limitCountItem, limitCount) ;
if ( limitCount > VISITS_TIME) {
return true ;
} else {
return false ;
}
} else {
limitCount = 1 ;
redisUtil. hset ( CommonConstant . CSOM_CHECK + message, message, System . currentTimeMillis ( ) ) ;
redisUtil. hset ( limitCountKey, limitCountItem, limitCount) ;
return false ;
}
} else {
redisUtil. hset ( CommonConstant . CSOM_CHECK + message, message, System . currentTimeMillis ( ) ) ;
redisUtil. hset ( limitCountKey, limitCountItem, limitCount) ;
return false ;
}
}
public static boolean CheckStackCount ( RedisUtil redisUtil, String message, String visits_time,
String more_time, String expire_time) {
if ( oConvertUtils. isEmpty ( message) ) {
return false ;
}
if ( oConvertUtils. isEmpty ( visits_time) ) {
visits_time = String . valueOf ( VISITS_TIME) ;
}
if ( oConvertUtils. isEmpty ( more_time) ) {
more_time = String . valueOf ( MORE_TIME) ;
}
if ( oConvertUtils. isEmpty ( expire_time) ) {
expire_time = String . valueOf ( EXPIRE_TIME* 24 ) ;
}
String csomKey = CommonConstant . CSOM_CHECK + message;
if ( ! redisUtil. hasKey ( csomKey) ) {
ArrayList < Object > list = new ArrayList < > ( ) ;
list. add ( System . currentTimeMillis ( ) ) ;
redisUtil. lSet ( csomKey, list, Long . valueOf ( expire_time) ) ;
return false ;
}
long lsize = redisUtil. lGetListSize ( csomKey) ;
if ( lsize < Long . valueOf ( visits_time) ) {
redisUtil. lSet ( csomKey, System . currentTimeMillis ( ) , Long . valueOf ( expire_time) ) ;
return false ;
} else {
Object obj = redisUtil. lGetIndex ( csomKey, 0 ) ;
if ( obj instanceof Long ) {
long time = ( Long ) obj;
System . out. println ( time) ;
if ( System . currentTimeMillis ( ) - time < Long . valueOf ( more_time) ) {
return true ;
} else {
redisUtil. lSet ( csomKey, System . currentTimeMillis ( ) , Long . valueOf ( expire_time) ) ;
redisUtil. ltrim ( csomKey, 1 , Long . valueOf ( visits_time) ) ;
System . out. println ( redisUtil. lGet ( csomKey, 0 , - 1 ) . toString ( ) ) ;
}
}
return false ;
}
}
public static boolean saveRedisData ( RedisUtil redisUtil, String prefix, String item, Object message, String saveTime) {
if ( oConvertUtils. isEmpty ( message) ) {
return false ;
}
if ( oConvertUtils. isEmpty ( saveTime) ) {
saveTime = String . valueOf ( SAVE_TIME) ;
}
long time = Long . valueOf ( saveTime) ;
String key = prefix + item;
if ( redisUtil. hHasKey ( key, item) ) {
if ( redisUtil. hget ( key, item) == null ) {
return redisUtil. hset ( key, item, message, time) ;
}
return redisUtil. hset ( key, item, message, time) ;
} else {
return redisUtil. hset ( key, item, message, time) ;
}
}
public static void delRedis ( RedisUtil redisUtil, String prefix, String item) {
String key = prefix + item;
if ( redisUtil. hasKey ( key) ) {
redisUtil. del ( key) ;
}
}