带有令牌身份验证的 django-restframework 在 apache 上返回 401

max*_*max 0 django-rest-framework

我正在使用带有令牌身份验证的 django restframework。当我在 django 开发服务器上运行它时它工作正常,但是当我在 apache 上运行它时,它失败并出现 401 身份验证错误。

# settings.py
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    )
}

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'django.contrib.admin',
    'rest_framework',
    'rest_framework.authtoken',
)

@api_view(['GET', 'POST'])
@authentication_classes((TokenAuthentication, SessionAuthentication, BasicAuthentication))
@permission_classes((IsAuthenticated,))
@staff_member_required
def api(request):
    params = request.data
    res = rest_api.get_data(params)
    return HttpResponse(json.dumps(res), content_type="application/json")
Run Code Online (Sandbox Code Playgroud)

如果我删除

@permission_classes((IsAuthenticated,))
@staff_member_required
Run Code Online (Sandbox Code Playgroud)

它可以在 apache 上工作,但它是不安全的。任何想法是什么问题以及如何解决它?

Ham*_*ami 8

Apache mod_wsgi 具体配置

请注意,如果使用 mod_wsgi 部署到 Apache,则默认情况下不会将授权标头传递给 WSGI 应用程序,因为假定身份验证将由 Apache 处理,而不是在应用程序级别处理。

如果您要部署到 Apache,并使用任何基于非会话的身份验证,则需要显式配置 mod_wsgi 以将所需的标头传递给应用程序。这可以通过在适当的上下文中指定 WSGIPassAuthorization 指令并将其设置为“On”来完成。

# this can go in either server config, virtual.
host, directory or .htaccess

WSGIPassAuthorization On
Run Code Online (Sandbox Code Playgroud)