当前位置:网站首页>实现各个微服务间限制IP访问 的三种方式
实现各个微服务间限制IP访问 的三种方式
2022-07-22 09:21:00 【单手入天象】
方式一:防火墙拦截
例如项目部署在linux下,修改防火墙,先拦截所有ip的访问,然后再开放我们服务调用使用的ip和端口的白名单,就可以实现拦截除了我们的服务外的所有ip的请求。
#阻止所有IP访问
iptables -A INPUT -s 0.0.0.0/0 -p tcp --dport 80 -j DROP
#然后再添加白名单
iptables -A INPUT -s 1.2.3.4 -p tcp --dport 80 -j ACCEPT
###############或者###########
iptables -A INPUT -s 2.3.4.5 -p tcp -j ACCEPT
方式二:nginx配置实现拦截
配置某个,或者某类接口,只能允许哪些ip访问
##对应的location添加指定规则
location / {
allow 132.23.22.185;
deny all;
}
方式三:代码实现拦截
写个拦截器(实现 HandlerInterceptor 接口),重写 preHandle 方法,注册拦截器拦截所有请求,在拦截器里面获取访问者的实际ip,然后根据你的需要是否拦截该请求
下面的方法是通过 request 获取访问者实际 ip
private String getRemoteIp(HttpServletRequest request) {
//只有通过了负载均衡或者HTTP代理才会添加该项
String ip = request.getHeader("x-forwarded-for");
//apache服务器才有这个请求头
if(StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
//weblogic有这个请求头
if(StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
//nginx代理有这个请求头
if(StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if(StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
边栏推荐
- LeetCode 116. 填充每个节点的下一个右侧节点指针
- 模块TensorFlow中没有Session
- Leetcode 720. the longest word in the dictionary
- PAT乙级1010一元多项式求导(题意理解)
- bjyx
- How to resolve errors in executing the yum makecache command
- IP地址、CIRD格式网址、主机名正则表达式
- The routing interface of local access to local TP5 of wechat applet is normal. Why can't you scan the code on the mobile phone to preview and get data?
- LeetCode: 197. 上升的温度
- 六度空间
猜你喜欢
1.通过类似window路径的方式访问json
MySQL的语句执行顺序
sql 语法中 join 的所有用法总结(简单例子)
连接mysql8.0出现caching-sha2-password问题
数据存储分区--范围分区,哈希分区,列表分区,性能调优必不可缺少的部分
Six dimensional space
App mobile terminal test [6] application program (APK) package management and activity
PTA 6-11 求自定类型元素序列的中位数 (25 分)
win10sp1升到最新版本;QT5.9.6静态编译(network有效)
MySQL statement execution order
随机推荐
sql 语法中 join 的所有用法总结(简单例子)
Log4J日志配置详解
Summary 20220119
1.Qt之打包发布程序 (NSIS);
PTA 习题8-8 判断回文字符串
Add the GD Library under centos7.5, and then the MySQL expansion library. There is no problem without the configuration of MySQL expansion. There is no MySQL expansion in phpinfo
Summary 20220210
There is no session in the tensorflow module
Recursively find the partial sum of simple alternating power series (15 points)
Six dimensional space
(c语言)数组是一种特殊的指针?
03. simple responsibility principle
OSI model, tcp/ip model
【QT源代码复用】QDateTimeEdit的下拉按钮事件响应
IP地址、CIRD格式网址、主机名正则表达式
04.接口隔离原则(Interface Segregation Principle)
02.依赖导致原则 - Dependence Inversion Principle
Leetcode 653. sum of two IV - input BST
LeetCode 720. 词典中最长的单词
1.QTableWidget插入按钮,灵活删除本行,一列显示行号