带有令牌身份验证的Django-RestFramework返回Apache的401



我正在使用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")

如果我删除

@permission_classes((IsAuthenticated,))
@staff_member_required

它将在Apache上工作,但后来是不安全的。有什么想法是什么问题以及如何解决?

apache mod_wsgi特定配置

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

如果您部署到Apache,并且使用任何基于非课程的身份验证,则需要明确配置MOD_WSGI以将所需的标头传递到应用程序。这可以通过在适当上下文中指定WSGIPASSAUTHORIZIAD指令并将其设置为" ON"。

来完成。
# this can go in either server config, virtual.
host, directory or .htaccess
WSGIPassAuthorization On

最新更新