python - 带有自定义中间件的 django 自定义身份验证后端(用户名、密码和 token Absed 身份验证)

标签 python django python-2.7 django-authentication django-middleware

我面临的情况是,我需要进行自定义身份验证和自定义中间件来对用户进行身份验证和授权。我必须检查 POST 请求中的用户名密码参数,或者是否设置了 cookie 以进行基于 token 的身份验证。现在,据我所知,Python 中不允许函数重载,我该如何实现它。我将下面的代码用于自定义身份验证和自定义中间件。

自定义中间件:

from django.contrib.auth import authenticate

class AuthMiddleWare(object):
    def process_request(self, request):

        if request.path != '/favicon.ico':
            print "inside process_request " + request.path              

            if request.method == 'POST' and request.POST.has_key('username' ) and request.POST.has_key('password'):                     
                authenticate(username = request.POST.get('username'),password = request.POST.get('password'))

            if 'SPRING_SECURITY_REMEMBER_ME_COOKIE' in request.COOKIES:                     
                authenticate(token = request.COOKIES.get('SPRING_SECURITY_REMEMBER_ME_COOKIE'))

        return None

以及自定义身份验证后端:

from core.api import NcpAPI       

class CustomNCPAuthBackend(object):     
    """
    This is custom authentication backend.
    Authenticate against the webservices call.

    The method below would override authenticate() of django.contrib.auth    
    """
    def authenticate(self, username = None, password = None):           
        print "inside authenticate of username and password with username being : "+username            
        return None

    def authenticate(self,token=None):
        print "inside authenticate of token with token being : "+token
        return None

问题是,即使我在发布请求中检查用户名和密码,它也会调用 token ,因为 token 在那里,但我怎样才能以某种方式强制它呢?

我尝试删除 cookie 并重试,但仍然没有启动以用户名和密码作为参数的身份验证功能。

请问有什么办法可以解决这个问题吗?

最佳答案

你是对的,Python 不支持函数重载,因为它根本不需要它。在您的情况下,发生的情况是 authenticate 的第二个声明覆盖了第一个声明,因此您只剩下一个版本的 authenticate,即采用 token 作为参数的版本.

你应该做的是(只是一个例子,有很多可能的解决方案):

class CustomNCPAuthBackend(object):
    """
    This is custom authentication backend.
    Authenticate against the webservices call.

    The method below would override authenticate() of django.contrib.auth    
    """
    def authenticate_password(self, username=None, password=None):
        print "inside authenticate of username and password with username being : "+username
        return None

    def authenticate_token(self,token=None):
        print "inside authenticate of token with token being : "+token
        return None

    def authenticate(self, token=None, username=None, password=None):
        if token is not None:
             return self.authenticate_token(token)
        else:
             return self.authenticate_password(username, password)

这样它将与您编写的 AuthMiddleWare 一起使用。

关于python - 带有自定义中间件的 django 自定义身份验证后端(用户名、密码和 token Absed 身份验证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16483082/

相关文章:

django - 在模型集工厂中使用自定义表单?

Python 桌面应用程序

python - 如何让 Python 知道用户连接的 Wifi?

python-2.7 - 具有自定义颜色的离散值的 matplotlib 热图

python - 有没有办法让 Graphite 烯与 django GenericRelation 字段一起工作?

python - 如何在运行时从其他文件访问字典

python - 将 C++ 类包装到 python 时出现问题

python - Geopy异常处理

python - 如何在 ArrayField 中存储字符串? (Django 和 PostgreSQL)

python - 尝试使用具有多行及其属性的 Bokeh Widget Checkbox 组时出现值错误 "visible"