mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
1.13.0
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package com.gitee.sop.gateway.manager;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.gateway.mapper.IPBlacklistMapper;
|
||||
import com.gitee.sop.gatewaycommon.bean.ChannelMsg;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultIPBlacklistManager;
|
||||
import com.gitee.sop.gatewaycommon.manager.ZookeeperContext;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 限流配置管理
|
||||
* @author tanghc
|
||||
*/
|
||||
@Slf4j
|
||||
public class DbIPBlacklistManager extends DefaultIPBlacklistManager {
|
||||
|
||||
@Autowired
|
||||
IPBlacklistMapper ipBlacklistMapper;
|
||||
|
||||
@Autowired
|
||||
Environment environment;
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
List<String> ipList = ipBlacklistMapper.listAllIP();
|
||||
log.info("加载IP黑名单, size:{}", ipList.size());
|
||||
ipList.stream().forEach(this::add);
|
||||
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
protected void after() throws Exception {
|
||||
ZookeeperContext.setEnvironment(environment);
|
||||
String path = ZookeeperContext.getIpBlacklistChannelPath();
|
||||
ZookeeperContext.listenPath(path, nodeCache -> {
|
||||
String nodeData = new String(nodeCache.getCurrentData().getData());
|
||||
ChannelMsg channelMsg = JSON.parseObject(nodeData, ChannelMsg.class);
|
||||
final IPDto ipDto = JSON.parseObject(channelMsg.getData(), IPDto.class);
|
||||
String ip = ipDto.getIp();
|
||||
switch (channelMsg.getOperation()) {
|
||||
case "add":
|
||||
log.info("添加IP黑名单,ip:{}", ip);
|
||||
add(ip);
|
||||
break;
|
||||
case "delete":
|
||||
log.info("移除IP黑名单,ip:{}", ip);
|
||||
remove(ip);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class IPDto {
|
||||
private String ip;
|
||||
}
|
||||
|
||||
}
|
@@ -13,5 +13,6 @@ public class ManagerInitializer {
|
||||
apiConfig.setIsvRoutePermissionManager(new DbIsvRoutePermissionManager());
|
||||
apiConfig.setRouteConfigManager(new DbRouteConfigManager());
|
||||
apiConfig.setLimitConfigManager(new DbLimitConfigManager());
|
||||
apiConfig.setIpBlacklistManager(new DbIPBlacklistManager());
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,22 @@
|
||||
package com.gitee.sop.gateway.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* IP黑名单
|
||||
* @author tanghc
|
||||
*/
|
||||
@Mapper
|
||||
public interface IPBlacklistMapper {
|
||||
|
||||
/**
|
||||
* 获取所有IP
|
||||
* @return
|
||||
*/
|
||||
@Select("SELECT ip FROM config_ip_blacklist")
|
||||
List<String> listAllIP();
|
||||
|
||||
}
|
Reference in New Issue
Block a user