gpt4 book ai didi

django - 您将如何在Nginx代理后面的Django应用上管理X509身份验证?

转载 作者:行者123 更新时间:2023-12-04 03:45:11 25 4
gpt4 key购买 nike

我想使用X509客户端身份验证进行安全的API访问。

X509证书是由我们的CA(memberca)生成的。

目的是:

  • 客户端使用SSL来连接API
  • 拥有有效成员(member)资格的X509证书的人可以访问
  • 我们检查是否未使用CRL吊销证书。

  • 我认为,我可以直接在Nginx配置中进行管理。
    我该怎么办?

    我可以使用Django吗?那我该怎么办呢?
    我可以使用两种解决方案为用户提供证书吗?

    例如,Nginx可以使用WSGI header 映射一些证书数据,以便我可以匹配我的用户吗?

    最佳答案

    我已经配置了类似的设置。

    首先,我使用Apache2对用户进行身份验证。

    您需要启用 mod_ssl ,并且必须(全局)定义:

  • SSLCertificateFile:指向您的PEM编码的服务器证书;
  • SSLCertificateKeyFile:指向您的PEM编码的服务器 key ;
  • SSLCertificateChainFile:指向您的CA证书的PEM列表;
  • SSLCACertificatePath:指向包含所有PEM CA证书的文件夹;
  • SSLCACertificateFile:指向您的CA证书(应具有与SSLCertificateChainFile相同的值);
  • SSLCARevocationPath:指向包含所有CRL的文件夹;
  • SSLCARevocationFile:指向已撤销证书的列表(您的ca-bundle.crl)
  • SSLCARevocationCheck链。

  • 现在,您的服务器已准备就绪,可以验证客户端X.509证书。

    如果您不想将apache2用作前端Web服务器,则可以通过启用 mod_proxy 将其配置为反向代理。

    您只需要像这样定义一个虚拟主机:
    <VirtualHost *:443>
    ServerName test.example.com:443
    ServerAdmin webmaster@example.com

    RequestHeader set Front-End-Https "On"

    # Here I define two headers, Auth-User and Remote-User
    # They will contain the key SSL_CLIENT_S_DN_CN which is the name of the
    # client certificate's owner.
    <If "-n %{SSL_CLIENT_S_DN_CN}">
    # If the key doesn't exist, it means that the certificate wasn't sent or
    # it was revoked.

    RequestHeader set Auth-User "%{SSL_CLIENT_S_DN_CN}s"
    RequestHeader set Remote-User "%{SSL_CLIENT_S_DN_CN}s"
    </If>

    # Now enable SSL, and SSL via the proxy
    SSLEngine on
    SSLProxyEngine on

    ## Require a client certificate
    # SSLVerifyClient require
    ## NB: I prefer set it to optional, in order to allow the user
    ## to connect to my application with a degraded mode (login+password)
    ## It's easy to detect if the user was authenticated by apache by looking
    ## at HTTP_AUTH_USER or HTTP_REMOTE_USER

    SSLVerifyClient optional

    # Maximum depth of CA Certificates in Client Certificate verification
    SSLVerifyDepth 4

    # Now, I pass all of this to my application, which is runned in nginx for example :
    <Location />
    ProxyPass http://<applciation host>
    ProxyPassReverse http://<applciation host>
    ProxyPreserveHost on
    # Send all informations about the client/server certificates to the application
    SSLOptions +StdEnvVars +ExportCertData
    </Location>
    </VirtualHost>

    现在,使用django,您只需要按照 here所述启用远程身份验证后端。

    从客户端证书中提取的所有信息都将发送到应用程序,因此可以使用请求对象(和/或中间件)使用它们。

    希望对您有所帮助。

    关于django - 您将如何在Nginx代理后面的Django应用上管理X509身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12318269/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com