Django,TastyPie,身份验证和自定义中间件令人头疼 [英] Django, TastyPie, Authentication, and custom middleware headache

查看:151
本文介绍了Django,TastyPie,身份验证和自定义中间件令人头疼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django Web应用程序,它需要整个站点的身份验证。我已经通过自定义中间件完成了该工作,该中间件基本上可以测试 request.user.is_anonymous 是否存在,并将它们重定向到登录页面。看起来像这样:

 来自django.contrib.auth.views导入登录
来自django.contrib.auth导入从django.http身份验证$ b $b。http导入HttpResponseRedirect,从django.utils导入HttpResponse
从django.core导入simplejson
导入序列化程序

类SiteLogin:
中间件需要为每个视图登录。
def process_request(self,request):
if request.path!='/ accounts / login /'and request.user.is_anonymous():
如果request.POST:
返回登录名(请求)
否则:
返回HttpResponseRedirect('/ accounts / login /?next =%s'%request.path)

现在,我正在制作一个iOS应用,目前,它仅会在Django服务器上执行GET请求。我正在尝试使用DeliciousPie来执行此操作,但无法进行身份验证。我正在使用 ApiKeyAuthentication ,并且我相信它已经正确设置了。但是,它只是将我重定向到登录页面。我想知道是否需要编辑此中间件来处理DeliciousPie的请求,但我认为DeliciousPie可以为我进行身份验证...



我觉得我的处境非常类似于这个问题,但是我想知道我的自定义中间件是否正在阻碍。



这是我的 api.py



<$ p $从django.contrib.auth.models导入用户p> 从django.db导入用户
从dastepie.resources导入模型
从cpm.systems.models导入ModelResource
来自cpm.products.models的
进口来自asteapie.models的产品
来自tastypie.authentication导入create_api_key
来自tastypie.authentication导入ApiKeyAuthentication
来自tastypie.authorization导入DjangoAuthorization,Authorization

models.signals.post_save.connect(create_api_key,sender = User)



类SystemResource(ModelResource):
类Meta:
queryset = System.objects.all()
resource_name ='system'
身份验证= ApiKeyAuthentication()
授权= DjangoAuthorization()


类ProductResource( ModelResource):
类元:
queryset = Product.objects.all()
resource_name ='product'
身份验证= ApiKeyAuthen tication()
授权= DjangoAuthorization()

以及我<$ c $的一部分c> urls.py :

  from cpm.ios.api import SystemResource,ProductResource 
从tastypie.api导入Api

v1_api = Api(api_name ='v1')
v1_api.register(SystemResource())
v1_api.register(ProductResource())

admin.autodiscover()

urlpatterns = patterns('',

#iOS DeliciousPie相关:
(r'^ ios / ',include(v1_api.urls)),
#....更多网址....

我尝试导航到的URL是:

  http://192.168.1.130:8080/ios/v1 / system / C0156 /?username = garfonzo& api_key = 12345?format = json 

但是我刚刚重定向到我的登录页面。我已按照本教程进行了开球,在管理面板上创建了一个api密钥,并将 WSGIPassAuthorization On 添加到了我的apache配置中。



有什么想法吗?



编辑我刚刚完全删除了中间件,现在所有我收到401个验证错误...



EDIT 2
我应该指出,如果我删除了?format = json ,然后得到以下响应:对不起,尚未实现。请在您的网址后附加?format = json。。这样就像进行身份验证一样,但是由于我没有指定格式而失败了。



所以我的URL看起来像这样: http://192.168.1.130:8080/ios/v1/system/C0156/?username=garfonzo& ; api_key = 12345 ,但是一旦我添加?format = JSON ,它就会出现401错误。

解决方案

TastyPie请求与任何典型的Django请求通过相同的中间件堆栈。因此,这绝对是您的中间件。您必须重新考虑它,或者只是简单地使用基础知识并使用@login_required。



在您禁用中间件后api键不起作用,因为您的URL格式错误。您不能使用?使用一次后在查询字符串中输入。尝试以下网址:

  http://192.168.1.130:8080/ios/v1/system/C0156/?username=garfonzo& ; api_key = 12345& format = JSON 


I have a Django web application which requires authentication across the whole site. I've accomplished that with custom middleware which basically test if request.user.is_anonymous and, if they are, redirects them to the login page. It looks like this:

from django.contrib.auth.views import login
from django.contrib.auth import authenticate
from django.http import HttpResponseRedirect, HttpResponse
from django.utils import simplejson
from django.core import serializers

class SiteLogin:
    "This middleware requires a login for every view"
    def process_request(self, request):
        if request.path != '/accounts/login/' and request.user.is_anonymous():
            if request.POST:
                return login(request)
            else:
                return HttpResponseRedirect('/accounts/login/?next=%s' % request.path)

Now I'm making an iOS app which, for now, will just do GET requests off the Django server. I am trying to use TastyPie to do this but I can't get the the authentication working. I am using ApiKeyAuthentication and, I believe, have set it up properly. However, it just redirects me to the login page. I'm wondering if I need to edit this middleware to handle TastyPie requests, but I thought TastyPie could to auth for me...

I feel like my situation is very similar to this question, but I wonder if my custom middleware is getting in the way.

Here's my api.py:

from django.contrib.auth.models import User
from django.db import models
from tastypie.resources import ModelResource
from cpm.systems.models import System
from cpm.products.models import Product
from tastypie.models import create_api_key
from tastypie.authentication import ApiKeyAuthentication
from tastypie.authorization import DjangoAuthorization, Authorization

models.signals.post_save.connect(create_api_key, sender=User)



class SystemResource(ModelResource):
  class Meta:   
    queryset = System.objects.all()
    resource_name = 'system'
    authentication = ApiKeyAuthentication()
    authorization = DjangoAuthorization()


class ProductResource(ModelResource):
  class Meta:       
    queryset = Product.objects.all()
    resource_name = 'product'
    authentication = ApiKeyAuthentication()
    authorization = DjangoAuthorization()

And a portion of my urls.py:

from cpm.ios.api import SystemResource, ProductResource
from tastypie.api import Api

v1_api = Api(api_name='v1')
v1_api.register(SystemResource())
v1_api.register(ProductResource())

admin.autodiscover()

urlpatterns = patterns('',

    # iOS TastyPie related:
    (r'^ios/', include(v1_api.urls)),
    # .... more urls....

The URL I try to navigate to is:

http://192.168.1.130:8080/ios/v1/system/C0156/?username=garfonzo&api_key=12345?format=json

But I'm just redirected to my login page. I've followed the tutorial to a tee, created an api key on my Admin panel, and added WSGIPassAuthorization On to my apache config.

Any ideas?

EDIT I just removed that middleware altogether and now all I receive are 401 AUTHENTICATION errors...

EDIT 2 I should point out that if I remove the ?format=json then I get a response of: Sorry, not implemented yet. Please append "?format=json" to your URL.. So it's like it does authenticate, but then fails because I'm not specifying the format.

So my URL looks like this: http://192.168.1.130:8080/ios/v1/system/C0156/?username=garfonzo&api_key=12345 but as soon as I add the ?format=JSON then it goes to a 401 error.

解决方案

TastyPie requests go through the same stack of middlewares as any typical django request. So, it's definitely your middleware. You have to rethink it, or just drop to the basics and use @login_required.

The api key doesn't work after you disabled the middleware because your URL is malformed. You can't use a ? in a querystring after you used it once. Try this url:

http://192.168.1.130:8080/ios/v1/system/C0156/?username=garfonzo&api_key=12345&format=JSON

这篇关于Django,TastyPie,身份验证和自定义中间件令人头疼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆