如何在Django Rest框架中的simplejwt令牌认证中跳过或删除密码字段? [英] How to skip or remove password field in simplejwt token authentication in django rest framework?

查看:796
本文介绍了如何在Django Rest框架中的simplejwt令牌认证中跳过或删除密码字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是,我不想在simplejwt令牌认证中输入密码.我通过继承 TokenObtainPairSerializer init()方法在身份验证中添加了一个额外的字段 根据我的要求.

My requirement is, I don't wanted to enter password in simplejwt token authentication. I have added one extra field in the authentication by inheriting the init() method of TokenObtainPairSerializer as per my requrements.

当前,我在密码字段中传递了None,但仍向用户显示(djnago管理门户).我想要的是,我不想在使用simplejwt进行身份验证时向用户显示密码字段.

Currently, I am passing None as in password field but still its showing to user (djnago admin portal). What I want is, I don't wanted to show the password field to user while authentication using simplejwt.

下面是我的代码

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

class CustomSerializer(TokenObtainPairSerializer):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields[self.username_field] = serializers.CharField()
        self.fields['password'] = PasswordField(default=None)
        self.fields['extra'] = serializers.CharField()

    def validate(self, attrs):
        pass

有什么方法可以将PasswordField设置为不可用,从而不会向用户显示?

Is there any ways to set PasswordField as unusable so it wont show to user?

推荐答案

我已经按照下面提到的过程解决了这个问题,

I have followed the below mentioned process to solve the problem,

  1. 覆盖如下所示的TokenObtainPairSerializer__init__方法,

使用del self.fields['password'],因此它不会询问您密码并添加您想要的任何字段.

Use del self.fields['password'], so It wont ask you the password and add whatever fields you want.

class CustomSerializer(TokenObtainPairSerializer): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() del self.fields['password']

class CustomSerializer(TokenObtainPairSerializer): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[self.username_field] = serializers.CharField() del self.fields['password']

这真的很好.我回答了一个几乎相同的问题,您可以在此处了解更多信息.

This works really well. There is a almost same question I have answered, You can check it here for more knowledge.

让我知道是否有人可以更好地解决这个问题.

Let me know if anyone have better solution of this problem.

这篇关于如何在Django Rest框架中的simplejwt令牌认证中跳过或删除密码字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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