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

修复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:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\views.py", line 27, in post
    serializer.is_valid(raise_exception=True)
  File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\serializers.py", line 422, in run_validation
    value = self.validate(value)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\serializers.py", line 78, in validate
    data['refresh'] = str(refresh)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\tokens.py", line 82, in __str__
    return token_backend.encode(self.payload)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\backends.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:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 497, in dispatch
    self.initial(request, *args, **kwargs)
  File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 414, in initial
    self.perform_authentication(request)
  File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\views.py", line 324, in perform_authentication
    request.user
  File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\request.py", line 227, in user
    self._authenticate()
  File "D:\Anaconda3\envs\ad_world\lib\site-packages\rest_framework\request.py", line 380, in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\authentication.py", line 36, in authenticate
    validated_token = self.get_validated_token(raw_token)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\authentication.py", line 90, in get_validated_token
    return AuthToken(raw_token)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\tokens.py", line 42, in __init__
    self.payload = token_backend.decode(token, verify=verify)
  File "D:\cyberpeace\xctf\codebase\ad_world\libs\rest_framework_simplejwt\backends.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'))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值