Zuul - Api 网关认证

Zuul - Api Gateway Authentication

我想通过 Spring 云来介绍 Zuul 作为一些服务前面的 API 网关。

我对身份验证有一些设计疑问。 身份验证将由 Spring Security 处理,它在 servlet 过滤器链中位于 Zuul 之前。

我的顾虑:

在管理 API 网关方面:

谢谢, 阿德里安

我们正在使用 Spring 会话在位于 Zuul 边缘服务器后面的所有服务之间复制会话。 Zuul 将对填充用户凭证的用户进行身份验证,并将经过身份验证的用户插入到会话中。然后将其复制到所有服务中,每个服务负责自己的安全规则和设置。所以实际上,Zuul 所做的只是在 spring 安全性中查找用户,并且后端服务会根据他们的需要执行安全规则。这样,您可以独立更改每个服务,使网关只是一个哑代理。

Dave Syers 教程中关于 Spring Security and an Angular JS app. I also posted 的一个很好的例子与此相关,其中包含我们如何执行此操作的示例,这可能会有所帮助。

  • the Gateway would sit in front of many services

这里有什么问题?

  • some services may expose endpoints which do not require authentication

Spring 安全有一个 permitAll() 访问规则

  • some services may expose endpoints which need a Session Id and some with a token", an arbitrary opaque value (for example downloading a file if you know a "hard to guess" url) In the API Gateway/Spring Security you can configure all the endpoints with their specific authentication requirements.

你的 Zuul 代理可以有会话。如果您使用的是 Spring Security OAuth 2.0,您可以使用 ResourceServerSecurityConfigurer#stateless(false) 并使用 HttpSecurity#sessionManagement().sessionCreationPolicy(...) 激活会话,以便在每次收到有效的访问令牌时创建会话。 JSESSIONID Cookie 将放置在 HTTP 响应中。

  • how do you enforce the actual Service Teams to provide the required settings per downstream service?

我不确定我是否理解这里的问题,你不想在 API 网关(zuul 代理)级别强制实施安全约束吗?还是您要在代理和目标应用程序上同时使用 "security double checks"?

  • how do you allow frequent Authentication settings changes in the Gateway (as per service needs) without having to stop the entire Gateway?

Zuul 允许您在运行时动态添加 ZuulRoutes,如果您将它用作独立库的话。包装在 Spring 安全性中,其上下文在启动时初始化一次...我怀疑您是否可以在运行时轻松更改安全配置。

按照 OP 在评论中的精确度进行编辑:如果您的团队应该对他们的安全规则负责,那么拥有一个集中式网关是设计上的矛盾。

我对微服务理念的解释是,每个应用程序都是独立的,负责其全部功能范围,安全/访问控制是其中的一部分。您可以在应用程序级别轻松验证令牌(通过调用授权服务器或使用 JWT),每个应用程序定义每个资源所需的范围。 Spring Cloud 已经有一个 OAuth 2.0 starter,或者如果您使用 "plain" Spring Boot.

,您可以轻松创建一个

这样您就可以在任何地方(public 云或本地服务器)部署单独的应用程序,而无需依赖上游组件来确保安全性或与其他团队同步您的网关配置部署。

API 网关的东西很容易受到诱惑,但不要忽视风险和限制:

  • 您将无法保护内部呼叫
  • 您将不得不依赖上游网络组件,并将应用程序的输入视为理所当然
  • 高级访问控制规则可能会让人头疼:如何获取用户的个人权限等
  • 您必须与其他团队同步配置更改