【问题标题】:Nginx authentication using JWT and an external authentication server in a multi-tenant system多租户系统中使用 JWT 和外部认证服务器的 Nginx 认证
【发布时间】:2016-09-20 17:11:25
【问题描述】:

我正在构建一个以 Nginx 为前端的多租户系统。

我希望所有访问 Nginx 的请求首先被“过滤”它们是否具有有效的 JWT。如果没有,则应该向外部身份验证服务器发出“呼出”,该服务器将执行 SAML/SSO 并返回 JWT 或“假”。如果为 false,则返回 401。

如果存在有效的 JWT,则需要对其进行解释并提取租户名称。然后,根据请求路径,需要修改 url/POST 正文以包含正确的租户(我们正在访问 Elasticsearch,需要确保只允许租户查询自己的索引)

身份验证服务器将内置在 php 中,因此我们需要的只是“过滤器”部分和调用身份验证服务器的有效方式。有没有现成的 nginx 模块可以解决这个要求?或者 Lua 是我最好的选择吗?我是一个比较新手的 Nginx-er。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    nginx 有更好、更简单的基于 JWT 的身份验证模块。 高度可配置。 https://github.com/tarachandverma/nginx-openidc

    您可以配置多个信赖方。 https://github.com/tarachandverma/nginx-openidc/blob/master/test-conf/oidc-config.xml#L12

    <!-- relying parties configuration -->
     <relyingParties default="282412598309-545pvmsh9r23f4k1o7267744s59sod6v.apps.googleusercontent.com">
        <relyingParty clientID="282412598309-545pvmsh9r23f4k1o7267744s59sod6v.apps.googleusercontent.com" clientSecret="xxxxx" domain=".com" validateNonce="true">
            <description>nginx oidc demo</description>
            <redirectUri>http://ngx-oidc-demo.com/oauth2/callback</redirectUri>
        </relyingParty>
     </relyingParties>
    

    【讨论】:

    • 请不要只发布链接答案,因为链接可能会断开。从链接中获取相关内容并在此处发布。
    【解决方案2】:

    使用https://github.com/auth0/nginx-jwt,对我来说安装 Openresty 更容易,因为我没有太多时间在 nginx 上手动安装 lua 模块及其所有依赖项。

    在第 114 行的 https://github.com/auth0/nginx-jwt/blob/master/nginx-jwt.lua 处,库将 sub 添加到应该是 Id 的标题中,如果您需要其他任何内容,可以更改它。

    ngx.header["X-Auth-UserId"] = jwt_obj.payload.sub

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 2019-05-29
      • 2016-08-25
      • 1970-01-01
      • 2020-11-29
      • 2018-05-05
      • 2019-01-03
      • 2019-01-07
      • 1970-01-01
      相关资源
      最近更新 更多