【问题标题】:How to make Django rest_framework Session Authentication case insensitive?如何使 Django rest_framework 会话身份验证不区分大小写?
【发布时间】:2016-08-24 17:10:02
【问题描述】:

我正在使用带有以下代码的 Django REST 框架基本身份验证:

class MyBasicAuthentication(BasicAuthentication):

    def authenticate_header(self, request):
        return 'xBasic realm="%s"' % self.www_authenticate_realm


class AuthView(APIView):
    authentication_classes = (MyBasicAuthentication,)
    serializer_class = UserSerializer

    def post(self, request, *args, **kwargs):
        user = authenticate(username=request.user.username, password=request.user.password)
        login(request, user)

        response = get_user_basic_info(request.user)

        return Response(response)

它工作正常,但我需要使此身份验证对用户名不区分大小写。有什么建议吗?

【问题讨论】:

    标签: python django authentication django-rest-framework case-insensitive


    【解决方案1】:

    如何为存储在数据库中的用户名选择全部大写或全部小写字符。 IE 全部大写:

    user = authenticate(username=str(request.user.username).upper(), password=request.user.password) 
    

    这样输入可以大小写混合,但输出可以标准化。

    【讨论】:

    • 一种更好的方法来做你建议做的事情是做身份验证(username_iexact=request.user.username),password=request.user.password) 但这里的问题是 django rest_framework 身份验证(BasicAuthentication .www_authenticate_realm) 发生在此行之前,该行使用 Django 的常规 Auth 对用户进行身份验证
    【解决方案2】:

    执行此操作的最简单方法是标准化存储用户名的方式(@Upsampled 回答时为大写或小写),这意味着首先发生的身份验证不会有问题。如果您想将所有现有用户更改为特定情况,我会推荐这个答案https://stackoverflow.com/a/33456271

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2020-08-09
      • 2012-10-22
      • 2018-07-05
      • 1970-01-01
      • 2020-06-12
      相关资源
      最近更新 更多