FastAPI-Users中AuthenticationBackend报AttributeError: transport错误求助
FastAPI-Users配置JWT认证触发AttributeError: transport的解决方法
问题重现
使用FastAPI-Users实现授权认证功能时,将JWT作为AuthenticationBackend的transport属性配置,但持续触发AttributeError: transport错误。已尝试重写代码、更新并重新安装库,且确认库源码中AuthenticationBackend类确实存在transport属性,代码参考官方文档实现。
报错栈
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 314, in _bootstrap self.run() File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\process.py", line 108, in run self._target(*self._args, **self._kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started target(sockets=sockets) File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 61, in run return asyncio.run(self.serve(sockets=sockets)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 68, in serve config.load() File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\config.py", line 467, in load self.loaded_app = import_from_string(self.app) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\importer.py", line 21, in import_from_string module = importlib.import_module(module_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1206, in _gcd_import File "<frozen importlib._bootstrap>", line 1178, in _find_and_load File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 690, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "D:\Pyton\MyProject\fastapi_shop\main.py", line 24, in <module> fastapi_users.get_auth_router(UserRead, UserCreate), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi_users\fastapi_users.py", line 84, in get_auth_router return get_auth_router( ^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\fastapi_users\router\auth.py", line 43, in get_auth_router **backend.transport.get_openapi_login_responses_success(), ^^^^^^^^^^^^^^^^^ File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydantic\_internal\_model_construction.py", line 210, in __getattr__ raise AttributeError(item) AttributeError: transport
相关代码
AuthenticationBackend配置
auth_backend = AuthenticationBackend( name="jwt", transport=bearer_transport, get_strategy=get_jwt_strategy, )
导入语句
from fastapi_users.authentication import ( AuthenticationBackend, BearerTransport, JWTStrategy, )
解决方案
- 版本兼容性检查:确认FastAPI-Users版本与参考的10.1版本文档匹配。执行
pip show fastapi-users查看版本,若版本不符,安装指定版本:pip install fastapi-users==10.1.0 - 验证
bearer_transport实例化:确保bearer_transport是BearerTransport的有效实例,正确写法示例:bearer_transport = BearerTransport(tokenUrl="auth/jwt/login") - 检查
FastAPIUsers实例初始化:确认创建FastAPIUsers对象时正确传入auth_backend参数,示例:fastapi_users = FastAPIUsers[UserDB, uuid.UUID]( get_user_manager, [auth_backend], ) - 清理缓存重装库:彻底卸载后清理缓存再重装,避免旧版本残留:
pip uninstall -y fastapi-users pip cache purge pip install fastapi-users==10.1.0
内容的提问来源于stack exchange,提问作者Castro_mx
相关产品推荐
相关产品推荐

