在我们基于 ServiceStack (v3) 的 API 中,我们有一些仅供内部使用的服务,因此我们[Restrict(InternalOnly = true)]
在所有内部请求 DTO 上放置了一个属性。
问题是我们使用负载均衡,并且受限服务对每个人都可以公开访问,因为调用 API 的 IP 始终是负载均衡器的 IP,因此是内部 IP。
有什么办法可以规避这种情况,使内部服务仅限于内部 IP,负载均衡器的 IP 除外?
在我们基于 ServiceStack (v3) 的 API 中,我们有一些仅供内部使用的服务,因此我们[Restrict(InternalOnly = true)]
在所有内部请求 DTO 上放置了一个属性。
问题是我们使用负载均衡,并且受限服务对每个人都可以公开访问,因为调用 API 的 IP 始终是负载均衡器的 IP,因此是内部 IP。
有什么办法可以规避这种情况,使内部服务仅限于内部 IP,负载均衡器的 IP 除外?
我还没有看到基于特定 IP 进行限制的内置方式(参见[Restrict]
测试) 。但是,您可以使用自定义属性自己过滤请求:
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。