2016-06-18 69 views
0

我使用帶令牌認證的django restframework。 它運行正常,當我在django開發服務器上運行它,但是當我在apache上運行它時,它失敗並出現401驗證錯誤。帶令牌認證的django-restframework在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的,但那麼它的不安全。 任何想法是什麼問題以及如何解決它?

回答

2

阿帕奇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 
+0

完美。我把它放在httpd.conf中,它正在工作。謝謝哈米德。 – max