栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

修复python3.6.13+django2.2+djangorestframework 3.12.4 使用djangorestframework

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

修复python3.6.13+django2.2+djangorestframework 3.12.4 使用djangorestframework

修复python3.6.13+django2.2+djangorestframework 3.12.4
使用djangorestframework_simplejwt-4.4.0-py3时,产生的两个bug

bug01:

Internal Server Error: /login/
Traceback (most recent call last):
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkviews.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwtviews.py", line 27, in post
    serializer.is_valid(raise_exception=True)
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkserializers.py", line 220, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkserializers.py", line 422, in run_validation
    value = self.validate(value)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwtserializers.py", line 78, in validate
    data['refresh'] = str(refresh)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwttokens.py", line 82, in __str__
    return token_backend.encode(self.payload)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwtbackends.py", line 43, in encode
    return token.decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'

解决方式:
rest_framework_simplejwt文件夹下backends.py文件里

class TokenBackend:

    def encode(self, payload):
        """
        Returns an encoded token for the given payload dictionary.
        """
        jwt_payload = payload.copy()
        if self.audience is not None:
            jwt_payload['aud'] = self.audience
        if self.issuer is not None:
            jwt_payload['iss'] = self.issuer

        token = jwt.encode(jwt_payload, self.signing_key, algorithm=self.algorithm)
        return token  # .decode('utf-8') 修改的地方

bug02:

Internal Server Error: /user_center/list/
Traceback (most recent call last):
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkviews.py", line 497, in dispatch
    self.initial(request, *args, **kwargs)
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkviews.py", line 414, in initial
    self.perform_authentication(request)
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkviews.py", line 324, in perform_authentication
    request.user
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkrequest.py", line 227, in user
    self._authenticate()
  File "D:Anaconda3envsad_worldlibsite-packagesrest_frameworkrequest.py", line 380, in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwtauthentication.py", line 36, in authenticate
    validated_token = self.get_validated_token(raw_token)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwtauthentication.py", line 90, in get_validated_token
    return AuthToken(raw_token)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwttokens.py", line 42, in __init__
    self.payload = token_backend.decode(token, verify=verify)
  File "D:cyberpeacexctfcodebasead_worldlibsrest_framework_simplejwtbackends.py", line 56, in decode
    options={'verify_aud': self.audience is not None})
TypeError: decode() got an unexpected keyword argument 'verify'

解决方式:
rest_framework_simplejwt文件夹下backends.py文件里

class TokenBackend:
    def decode(self, token, verify=True):
        """
        Performs a validation of the given token and returns its payload
        dictionary.

        Raises a `TokenBackendError` if the token is malformed, if its
        signature check fails, or if its 'exp' claim indicates it has expired.
        """
        try:
            return jwt.decode(token.decode('utf-8'), self.verifying_key, algorithms=[self.algorithm],  # verify=verify, 修改的地方
                              audience=self.audience, issuer=self.issuer,
                              options={'verify_aud': self.audience is not None})
        except InvalidTokenError:
            raise TokenBackendError(_('Token is invalid or expired'))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/313595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号