IP黑白名单是网络安全管理中常见的策略工具,用于控制网络访问权限,根据业务场景的不同,其应用范围广泛
添加一个简单的白名单,然后只有白名单上面的 IP 才能访问网站,否则不能访问这是只是一个很简单的实现方法
首先:在 application.yml 配置 IP 白名单
1 2 3 4 5 6 |
my: ipList: - 192.168.3.3 - 192.168.2.12 - 192.168.5.24 - 152.63.54.26 |
想要引用到配置文件里面的 String 数组,如果使用普通的 @Value 是不行的,必须使用其他方法
步骤一:创建一个实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Component @ConfigurationProperties(prefix = "my") // 这里是对应的配置文件里面自定义的 my public class My {
private String[] ipList;
public My(String[] ipList) { this.ipList = ipList; }
public String[] getIpList() { return ipList; }
public void setIpList(String[] ipList) { this.ipList = ipList; } } |
这样配置文件的信息就保存在了这个实体类里面
在 Controller 里面比对 IP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@Controller public class LoginController {
@Autowired private My myIpList;
@PostMapping("/login") @ResponseBody public String login(@RequestBody LoginForm loginForm, HttpServletRequest request) { String [] ipList = myIpList.getIpList();
String IPAddress = loginForm.getIpAddress(); // 如果前端传递过来的 IP 在白名单里面,就返回 OK,如果不在,就返回 error for (String s : ipList) { if (s.equals(IPAddress)) { return "OK"; } } return "error"; }
} |
这个是利用了 aop 的切面编程,如果同一个 IP 访问一个地址一分钟之内超过10次,就会把这个 IP 拉入黑名单,在自定义的时间内是不能再次访问这个网站的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
spring: redis: # 超时时间 timeout: 10000ms # 服务器地址 host: 192.168.1.1 # 服务器端口 port: 6379 # 数据库 database: 0 # 密码 password: xxxxxxx lettuce: pool: # 最大连接数,默认为 8 max-active: 1024 # 最大连接阻塞等待时间,默认 -1 max-wait: 10000ms # 最大空闲连接 max-idle: 200 # 最小空闲连接 min-idle: 5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented @Order(Ordered.HIGHEST_PRECEDENCE) public @interface RequestLimit {
/** * 允许访问的最大次数 */ int count() default Integer.MAX_VALUE;
/** * 时间段,单位为毫秒,默认值一分钟 */ long time() default 60000; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@Configuration @Slf4j public class RedisConfiguration {
@Bean @ConditionalOnMissingBean public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { System.err.println("调用了 Redis 配置类"); RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); // String 类型 key 序列器 redisTemplate.setKeySerializer(new StringRedisSerializer()); // String 类型 value 序列器 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash 类型 value 序列器 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash 类型 value 序列器 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
@Aspect @Component @Slf4j public class RequestLimitContract {
private static final Logger logger = LoggerFactory.getLogger("requestLimitLogger");
@Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private ObjectMapper objectMapper;
@Before("within(@org.springframework.stereotype.Controller *) && @annotation(limit)") public void requestLimit(final JoinPoint joinPoint , RequestLimit limit) throws RequestLimitException { try { LoginForm loginForm = new LoginForm(); Object[] args = joinPoint.getArgs(); HttpServletRequest request = null; for (int i = 0; i < args.length; i++) { if (args[i] instanceof HttpServletRequest) { request = (HttpServletRequest) args[i]; break; } else if (args[i] instanceof LoginForm) { loginForm = (LoginForm) args[i]; }
} if (request == null) { throw new RequestLimitException("方法中缺失HttpServletRequest参数"); }
//获取请求中的ip与url链接参数 用于拼接key存放redis中 String ip = loginForm.getIpAddress(); String url = request.getRequestURL().toString(); Long interview_time = new Date().getTime(); String key = "req_limit_".concat(url).concat("---").concat(ip); System.err.println("准备保存在redis中的数据为-->"); Map<String, Long> form = new HashMap<>(); form.put("size", 1L); form.put("saveRedisTime", interview_time);
if (redisTemplate.opsForValue().get(key) == null) { System.err.println(form); redisTemplate.opsForValue().set(key, form); } else { //用于进行ip访问的计数 Map<String, Long> result = (Map<String, Long>) redisTemplate.opsForValue().get(key); System.err.println("从redis取到的数据(内部)"); System.out.println(result);
assert result != null; result.put("size", result.get("size") + 1); redisTemplate.opsForValue().set(key, result);
if (result.get("size") > 10) { logger.info("用户IP[" + ip + "]访问地址[" + url + "]超过了限定的次数[" + limit.count() + "]"); throw new RequestLimitException(); }
// 如果访问次数小于 10 次,那么一分钟过后就直接删除这个节点 if (result.get("size") <= limit.count()) { //创建一个定时器 Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { @Override public void run() { redisTemplate.delete(key); } }; //这个定时器设定在time规定的时间之后会执行上面的remove方法,也就是说在这个时间后它可以重新访问 timer.schedule(timerTask, limit.time()); } } }catch (RequestLimitException e){ throw e; }catch (Exception e){ logger.error("发生异常",e); } } } |
记住要在启动类上面开启启动定时器注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
@Component public class ScheduledTask {
@Autowired private RedisTemplate<String, Object> redisTemplate;
// @Value("${properties.continueTime}") private final long continueTime = 120; // 这是是黑名单的保存时间,在这个时间内,同一个IP将不能继续访问 // @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨触发一次 @Scheduled(fixedRate = 1000000) // 每 30s 执行一次 public void deleteDataScheduled() { Set<String> keys = redisTemplate.keys("*"); if (keys == null || keys.size() == 0) { return; } System.out.println("redis里面所有的key为:" + keys.toString());
long now_time = new Date().getTime();
for (String key : keys) { Map<String, Long> result = (Map<String, Long>) redisTemplate.opsForValue().get(key); assert result != null; System.err.println(result); // 计算出时间差 long createTime = result.get("saveRedisTime"); System.err.println("创建时间为:" + createTime); long l = (now_time - createTime) / 1000L; System.err.println("时间差为:" + l); if (l > continueTime) { System.out.println("禁止时间结束,解除时间限制!!!"); redisTemplate.delete(key); return; } int s = Math.toIntExact(result.get("size")); System.out.println("s===>" + s + " l===>" + l); if (s <= 10) { if (l > 60) { System.out.println("一分钟结束,删除节点,重新计时"); redisTemplate.delete(key); } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@PostMapping("/login") @RequestLimit(count=10,time=60000) // 这个注解就是开启IP限制的注解,这个注解的业务都是自己写的 @ResponseBody public String login(@RequestBody LoginForm loginForm, HttpServletRequest request) {
System.out.println("执行登录代码");
String name = loginForm.getUsername(); String IPAddress = loginForm.getIpAddress(); String timestamp = String.valueOf(new Date().getTime()); String info = Arrays.toString(ipList);
request.setAttribute("userName", name); request.setAttribute("ipAddress", IPAddress);
return "OK1"; } |
总结:在这里针对 IP 的限制就结束了,总而言之还是比较简单的,没有什么特别难的点