1

问题定义:

我已阅读有关使用此官方链接graphene-jwt中的包为我的项目添加 JWT 身份验证的文章。根据教程中所说,应该对项目文件进行一些更改:settings.py

  1. 首先应该安装包:
pip3 install django-graphql-jwt
  1. 将 AuthenticationMiddleware 添加到MIDDLEWARE变量。
MIDDLEWARE = [
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    ...
]
  1. 添加 JSONWebTokenBackend 后端AUTHENTICATION_BACKENDS变量:
AUTHENTICATION_BACKENDS = [
    'graphql_jwt.backends.JSONWebTokenBackend',
    'django.contrib.auth.backends.ModelBackend',
]
  1. 将 JSONWebTokenMiddleware 添加到GRAPEHENE变量中。
GRAPHENE = {
    'SCHEMA': 'apps.schema.schema.schema', # Where your Graphene schema lives
    'MIDDLEWARE': [
        'graphql_jwt.middleware.JSONWebTokenMiddleware',
    ],
}

完成第 4 步后,每个请求localhost:8000/graphql都有以下堆栈跟踪:

errorInternal Server Error: /graphql
Traceback (most recent call last):
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/django/views/generic/base.py", line 62, in view
    self = cls(**initkwargs)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/views.py", line 78, in __init__
    schema = graphene_settings.SCHEMA
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/settings.py", line 116, in __getattr__
    val = perform_import(val, attr)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/settings.py", line 55, in perform_import
    return import_from_string(val, setting_name)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphene_django/settings.py", line 69, in import_from_string
    module = importlib.import_module(module_path)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/mostafa/Desktop/Work/OOD_Project/back/transportation/apps/schema/schema.py", line 2, in <module>
    import graphql_jwt
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/__init__.py", line 1, in <module>
    from . import relay
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/relay.py", line 3, in <module>
    from . import mixins
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/mixins.py", line 10, in <module>
    from . import exceptions, settings
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/graphql_jwt/settings.py", line 26, in <module>
    JWT_SECRET_KEY = env('JWT_SECRET_KEY', default=settings.SECRET_KEY)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 123, in __call__
    return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 284, in get_value
    value = self.get_value(value, cast=cast, default=default)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 284, in get_value
    value = self.get_value(value, cast=cast, default=default)
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 284, in get_value
    value = self.get_value(value, cast=cast, default=default)
  [Previous line repeated 866 more times]
  File "/home/mostafa/Desktop/Work/OOD_Project/back/venv/lib/python3.6/site-packages/environ/environ.py", line 273, in get_value
    value = self.ENVIRON[var]
  File "/usr/lib/python3.6/os.py", line 666, in __getitem__
    value = self._data[self.encodekey(key)]
RecursionError: maximum recursion depth exceeded:

项目结构:

├── apps
│   └── orders
│       ├── ... other-files
│       ├── schema.py
│   └── schema
│       └── schema.py
│   └── users
│       ├── ... other-files
│       └── schema.py

截屏: grpahql 超出最大递归深度 screenshot

不知道是bug还是我的错。(API 在添加上述配置之前运行良好,并且已经检查了很多次)我在互联网上搜索过,但没有找到我的问题的正确答案。如果有人可以帮助我,我将不胜感激。

PS1:与我有关的最著名的问题是this one,主要讨论python中的最大递归深度误差。但是我在这里无法理解此错误的原因。

PS2:我还在 settings.py 中添加了以下几行,但它仍然不起作用!

import sys
sys.setrecursionlimit(10000)
4

1 回答 1

1

看看这个问题: https ://github.com/joke2k/django-environ/issues/60

我认为您的密钥以 $ 开头,这就是问题所在。

于 2020-06-12T12:36:06.837 回答