Nginx开源是否支持OpenID和JWT

Does Nginx open source support OpenID and JWT

我有一个基本的 Nginx docker 映像,用作反向代理,目前使用位于我的应用程序服务器前面的基本身份验证。我正在寻找一种方法将它与我们使用 JWT 的开发中的 SSO 解决方案集成,但所有文档都说它需要 Nginx+。那么,是否可以在开源 Nginx 内部进行 JWT 验证,或者我是否需要付费版本?

当然,有开源代码,您可以根据自己的情况使用和自定义这些代码 (example)。

恕我直言,有更好的实现,您可以在应用程序前将其用作 "auth proxy"。我最喜欢的是 keycloak-gatekeeper(你可以将它与任何 OpenID IdP 一起使用,而不仅仅是与 Keycloak 一起使用),它可以提供身份验证、授权、令牌加密、刷新令牌实现、占用空间小,...

还有lua-resty-openidchttps://github.com/zmartzone/lua-resty-openidc

lua-resty-openidc is a library for NGINX implementing the OpenID Connect Relying Party (RP) and/or the OAuth 2.0 Resource Server (RS) functionality.

When used as an OpenID Connect Relying Party it authenticates users against an OpenID Connect Provider using OpenID Connect Discovery and the Basic Client Profile (i.e. the Authorization Code flow). When used as an OAuth 2.0 Resource Server it can validate OAuth 2.0 Bearer Access Tokens against an Authorization Server or, in case a JSON Web Token is used for an Access Token, verification can happen against a pre-configured secret/key .

鉴于您的配置设置没有身份验证,我发现了这个并让它工作:https://hub.docker.com/r/tomsmithokta/nginx-oss-okta 完全基于上面提到的 lua-resty-openidc。不过,它已经建成的事实对我很有帮助。

首先在 Okta Web GUI 中配置您的 Okta 应用程序,然后填写 NGINX 示例配置文件中未注释掉的适当字段。唯一需要注意的是取消注释 redirect_uri 并填写它,而是注释掉或删除 redirect_uri_path 这是一个已弃用的字段。配置中的所有其他内容都是您可以使用或按原样接受的参数。

默认情况下,它会将您转到 headers 页面,但如果您调整 proxy_pass 字段,您应该能够将它传递到您的应用程序。

基于这个要点https://gist.github.com/abbaspour/af8dff3b297b0fcc6ba7c625c2d7c0a3

这是我在 dockerfile 中的做法(基于 buster-slim )

FROM python:3.9-slim as base

FROM base as builder

ENV LANG en_GB.UTF-8 \
    LANGUAGE en_GB.UTF-8 \
    PYTHONUNBUFFERED=True \
    PYTHONIOENCODING=UTF-8

RUN apt-get update \
    && apt-get install --no-install-recommends --no-install-suggests -y \
    build-essential  \
    patch \
    git \
    wget \
    libssl-dev \
    libjwt-dev \
    libjansson-dev \
    libpcre3-dev \
    zlib1g-dev \
    && wget https://nginx.org/download/nginx-1.18.0.tar.gz \
    && tar -zxvf nginx-1.18.0.tar.gz \
    && git clone https://github.com/TeslaGov/ngx-http-auth-jwt-module \
    && cd nginx-1.18.0  \
    && ./configure --add-module=../ngx-http-auth-jwt-module \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-ld-opt="-L/usr/local/opt/openssl/lib" \
    --with-cc-opt="-I/usr/local/opt/openssl/include" \
    && make


FROM base

COPY --from=builder /nginx-1.18.0/objs/nginx /usr/sbin/nginx
COPY --from=builder /nginx-1.18.0/conf /usr/local/nginx/conf

ENV LANG en_GB.UTF-8 \
    LANGUAGE en_GB.UTF-8 \
    PYTHONUNBUFFERED=True \
    PYTHONIOENCODING=UTF-8

RUN apt-get update && \
    apt-get install --no-install-recommends --no-install-suggests -y \
    libssl-dev \
    libjwt-dev \
    libjansson-dev \
    libpcre3-dev \
    zlib1g-dev