【问题标题】:Camel rest - Allow specific ip to access send requestCamel rest - 允许特定 ip 访问发送请求
【发布时间】:2019-04-24 05:40:11
【问题描述】:
我有以下骆驼休息路线。目前,所有主机都可以使用公开的 URL 访问此路由。
我是否可以根据配置的 IP 限制远程主机访问。
我想允许某些 IP 地址访问此 URL。骆驼中是否有任何配置可以处理这个问题?
rest("/api/")
.id("reset-api-route")
.get("/reset")
.to("direct:resetRoute");
【问题讨论】:
标签:
apache-camel
camel-http
【解决方案1】:
使用 camel-netty4-http 组件,您可以在标头中包含远程 IP 地址。
但是,在您的应用程序之前在防火墙上进行网络级别隔离可能更有意义。
使用 camel-netty4-http,您可以像这样使用远程 IP 检查和执行逻辑:
@Override
public void configure() throws Exception {
restConfiguration()
.component("netty4-http")
.host("localhost")
.port(8000)
.bindingMode(RestBindingMode.auto);
rest("/api/")
.id("reset-api-route")
.get("/reset")
.to("direct:resetRoute");
from("direct:resetRoute")
.log("${in.headers.CamelNettyRemoteAddress}")
.choice()
.when(header("CamelNettyRemoteAddress").startsWith("/127.0.0.1:")) // localhost
.transform().constant("allowed").endChoice()
.otherwise()
.transform().constant("denied");
}
如果您的 Camel 应用程序在 Spring-Boot 中运行,那么您可以使用 Spring Security IP 过滤。另外请记住,如果您的应用程序位于负载均衡器之后,那么根据负载均衡器的不同,您可能总是会看到负载均衡器的地址,而不是原始调用方。