简体   繁体   English

如何在 django rest 框架中的 simplejwt 令牌身份验证中跳过或删除密码字段?

[英]How to skip or remove password field in simplejwt token authentication in django rest framework?

My requirement is, I don't wanted to enter password in simplejwt token authentication.我的要求是,我不想在 simplejwt 令牌身份验证中输入密码。 I have added one extra field in the authentication by inheriting the init() method of TokenObtainPairSerializer as per my requrements.根据我的要求,我通过继承TokenObtainPairSerializerinit()方法在身份验证中添加了一个额外的字段。

Currently, I am passing None as in password field but still its showing to user (djnago admin portal).目前,我在密码字段中传递None ,但仍向用户显示(djnago 管理门户)。 What I want is, I don't wanted to show the password field to user while authentication using simplejwt.我想要的是,我不想在使用 simplejwt 进行身份验证时向用户显示密码字段。

below is my code,下面是我的代码,

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

Is there any ways to set PasswordField as unusable so it wont show to user?有什么方法可以将 PasswordField 设置为不可用,这样它就不会显示给用户?

I have followed the below mentioned process to solve the problem,我已经按照下面提到的过程来解决问题,

  1. Override the TokenObtainPairSerializer class __init__ method like below,覆盖TokenObtainPairSerializer class __init__方法,如下所示,

  2. Use del self.fields['password'] , so It wont ask you the password and add whatever fields you want.使用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.让我知道是否有人对此问题有更好的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在“django-rest-framework-simplejwt”中使用“电话号码”和“otp”而不是“用户名”和“密码”来生成令牌? - How can I use `phone number` and `otp` in "django-rest-framework-simplejwt" instead of `username` and `password` to generate token? 如何在不使用密码的情况下在 Django Rest Framework 中实现令牌认证? - How to implement Token Authentication in Django Rest Framework without using a password? 在django-rest-framework-simplejwt注册后返回令牌 - return token after registration with django-rest-framework-simplejwt Django rest_framework_simplejwt令牌过期太快 - Django rest_framework_simplejwt token expiring too fast django-rest-framework-simplejwt 验证令牌内的参数 - django-rest-framework-simplejwt validate parameter inside the token 在 Django Rest 框架中使用 simplejwt 进行登录身份验证,带有模板和 ajax - Login Authentication with simplejwt in Django Rest Framework with templates and ajax 使用 Django Rest 框架时,如何在 SimpleJWT 自定义令牌中返回 401 响应? - How do I return a 401 response in a SimpleJWT Custom Token when using the Django Rest Framework? JWT 身份验证使用 SimpleJWT 在 Django Rest Framework 中返回 AnonymousUser - JWT authentication returns AnonymousUser in Django Rest Framework with SimpleJWT 如何在“django-rest-framework-simplejwt”中使用“email”而不是“username”来生成令牌? - How can I use `email` in "django-rest-framework-simplejwt" instead of `username` to generate token? Django Rest 框架令牌认证 - Django Rest Framework Token Authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM