目前我手动生成令牌,但我想使用jwt
令牌,我遵循官方文档和其他参考资料,但我仍然无法找出问题。
serializers.py ,其中在手动生成身份验证令牌之后。
class UserLoginSerializer(serializers.ModelSerializer):
token = serializers.CharField(allow_blank=True, read_only=True)
class Meta:
model = User
fields = [
'username',
'password',
'token',
]
extra_kwargs = {"password":
{"write_only": True}
}
def validate(self, data):
username = data.get('username', None)
password = data.get('password', None)
try:
usern = Account.objects.get(username=username)
except ObjectDoesNotExist:
raise serializers.ValidationError("User does not exists")
if usern.check_password(password):
data["token"] = "asdasdasdasd"
else:
raise serializers.ValidationError("password invalid")
return data
urls.py
from django.conf.urls import url
from .views import AuthRegister, AuthLogin
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns = [
url(r'^register/$', AuthRegister.as_view()),
url(r'^login/$', AuthLogin.as_view()),
url(r'^api-token-auth/', obtain_jwt_token),
]
在我已添加'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
我使用了url(r'^api-token-auth/', obtain_jwt_token)
,但我无法弄清楚如何生成jwt
令牌。
请有人帮帮我!
答案 0 :(得分:0)
您需要对端点/api-token-auth/
上的API进行HTTP POST调用。在此调用中,正文将包含两个键 - username
和password
。
使用cURL的示例:
curl -X POST -d username=abcd -d password=1234 http://ip:port/api-token-auth/