【问题标题】:Restrict service to internal network BUT load balancer?将服务限制为内部网络但负载均衡器?
【发布时间】:2014-06-27 14:11:45
【问题描述】:
在我们基于 ServiceStack (v3) 的 API 中,我们有一些仅供内部使用的服务,因此我们在所有内部请求 DTO 上放置了 [Restrict(InternalOnly = true)] 属性。
问题在于我们使用负载平衡,并且受限服务对所有人公开访问,因为调用 API 的 IP 始终是负载平衡器的 IP,因此是内部 IP。
有什么办法可以规避这种情况,使内部服务仅限于内部 IP,负载均衡器的 IP 除外?
【问题讨论】:
标签:
c#
asp.net
servicestack
servicestack-bsd
【解决方案1】:
我还没有看到基于特定 IP 进行限制的内置方式 (See [Restrict] tests)。但是,您可以使用自定义属性自己过滤请求:
public class AllowLocalExcludingLBAttribute : RequestFilterAttribute
{
public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto)
{
// If the request is not local or it belongs to the load balancer then throw an exception
if(!req.IsLocal || req.RemoteIp == "10.0.0.1")
throw new HttpError(System.Net.HttpStatusCode.Forbidden, "403", "Service can only be accessed internally");
}
}
然后您只需在服务或操作方法上添加[AllowLocalExcludingLB],否则您将使用[Restrict] 属性或将其与其他限制结合使用。 将10.0.0.1 替换为您的负载均衡器 IP。