Git Product home page Git Product logo

amisadmin / fastapi-user-auth Goto Github PK

View Code? Open in Web Editor NEW
320.0 7.0 42.0 462 KB

FastAPI-User-Auth is a simple and powerful FastAPI user RBAC authentication and authorization library. Based on FastAPI-Amis-Admin and provides a freely extensible visual management interface.

Python 100.00%
fastapi-amis-admin fastapi-user-auth fastapi-user fastapi-auth fastapi-jwt-auth fastapi-admin fastapi-rbac fastapi

fastapi-user-auth's People

Contributors

625412505 avatar amisadmin avatar dongfengweixiao avatar jason810496 avatar mkadir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastapi-user-auth's Issues

文档中的简单例子跑不起来

这个例子跑不起来, 我用 pip 安装的

from fastapi_amis_admin.admin.settings import Settings
from fastapi_user_auth.site import AuthAdminSite
from starlette.requests import Request
from sqlmodel import SQLModel

# 创建FastAPI应用
app = FastAPI()

# 创建AdminSite实例
site = AuthAdminSite(settings=Settings(database_url='sqlite:///amisadmin.db?check_same_thread=False'))
auth = site.auth
# 挂载后台管理系统
site.mount_app(app)


# 创建初始化数据库表
@app.on_event("startup")
async def startup():
    await site.db.async_run_sync(SQLModel.metadata.create_all, is_session=False)
        # 创建默认管理员,用户名: admin,密码: admin, 请及时修改密码!!!
    await auth.create_role_user("admin")
    # 创建默认超级管理员,用户名: root,密码: root, 请及时修改密码!!!
    await auth.create_role_user("root")
    # 运行site的startup方法,加载casbin策略等
    await site.router.startup()
    # 添加一条默认的casbin规则
    if not auth.enforcer.enforce("u:admin", site.unique_id, "page", "page"):
        await auth.enforcer.add_policy("u:admin", site.unique_id, "page", "page", "allow")


# 要求: 用户必须登录
@app.get("/auth/get_user")
@auth.requires()
def get_user(request: Request):
    return request.user


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app)```



C:\Users\es\projects\esdata-backend\test.py:18: DeprecationWarning:
on_event is deprecated, use lifespan event handlers instead.

    Read more about it in the
    [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).

@app.on_event("startup")
INFO: Started server process [14908]
INFO: Waiting for application startup.
ERROR: Traceback (most recent call last):
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\starlette\routing.py", line 705, in lifespan
async with self.lifespan_context(app) as maybe_state:
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\starlette\routing.py", line 584, in aenter
await self._router.startup()
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\starlette\routing.py", line 682, in startup
await handler()
File "C:\Users\es\projects\esdata-backend\test.py", line 26, in startup
await site.router.startup()
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\starlette\routing.py", line 682, in startup
await handler()
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\fastapi_user_auth\admin\admin.py", line 388, in _load_policy
await self.load_policy()
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\fastapi_user_auth\admin\admin.py", line 391, in load_policy
await self.site.auth.enforcer.load_policy()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\es.pyenv\pyenv-win\versions\3.11.3\Lib\functools.py", line 1001, in get
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\fastapi_user_auth\auth\auth.py", line 92, in enforcer
enforcer = AsyncEnforcer(
^^^^^^^^^^^^^^
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\casbin\core_enforcer.py", line 63, in init
self.init_with_adapter(model, adapter)
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\casbin\core_enforcer.py", line 84, in init_with_adapter
self.init_with_model_and_adapter(m, adapter)
File "C:\Users\es\projects\esdata-backend\venv\Lib\site-packages\casbin\async_internal_enforcer.py", line 35, in init_with_model_and_adapter
raise RuntimeError("Invalid parameters for enforcer.")
RuntimeError: Invalid parameters for enforcer.

ERROR: Application startup failed. Exiting.

注册用户时报错 AttributeError: __aenter__

Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 372, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in call
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/debug.py", line 96, in call
raise exc from None
File "/usr/local/lib/python3.9/site-packages/uvicorn/middleware/debug.py", line 93, in call
await self.app(scope, receive, inner_send)
File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 261, in call
await super().call(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, in call
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in call
raise exc
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in call
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 92, in call
await self.simple_response(scope, receive, send, request_headers=headers)
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 147, in simple_response
await self.app(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in call
raise exc
File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in call
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call
raise e
File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call
await self.app(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 656, in call
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 408, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 261, in call
await super().call(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/applications.py", line 112, in call
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in call
raise exc
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in call
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in call
raise exc
File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in call
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call
raise e
File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call
await self.app(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 656, in call
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 61, in app
response = await func(request)
File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 217, in app
solved_result = await solve_dependencies(
File "/usr/local/lib/python3.9/site-packages/fastapi/dependencies/utils.py", line 527, in solve_dependencies
solved = await call(**sub_values)
File "/usr/local/lib/python3.9/site-packages/fastapi_amis_admin/admin/admin.py", line 832, in route
return await self.handle(request, data) # type:ignore
File "/usr/local/lib/python3.9/site-packages/fastapi_user_auth/admin.py", line 128, in handle
async with request.auth.db.session_maker() as session:
AttributeError: aenter

关于连接第三方登录

目前在用Azure来做的第三方登录。把Access Token存放在Cookie 然后重写了get_current_user() 但是发现老是重定向到登录界面无限循环。可是案例里面在跳转几次后就加载Home页面了,请教大概是遗漏了什么地方?

使用add_grouping_policy添加子角色继承, 但是require在判定角色时不能识别继承的子角色

await auth.create_role_user("admin")
await auth.create_role_user("root")
await auth.enforcer.add_grouping_policy("r:root", "r:admin")
# 在dependencies中使用root验证auth.requires("admin")失败

在auth.has_role_for_user函数中, 能否使用self.enforcer.get_implicit_roles_for_user再自己判定呢?

(也看到casbin没有提供has_implicit_roles_for_user了, 可能这其中有效率之类的考虑。如果是我的使用方式有问题, 请告诉我, 非常感谢!)

postgreSQL数据库使用update_casbin_site_grouping方法批量插入数据时,出现表auth_casbin_rule的id不能为空错误。

postgreSQL数据库使用update_casbin_site_grouping方法时,错误如下:
sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "id" of relation "auth_casbin_rule" violates not-null constraint。
错误原因:fastapi_user_auth\utils\sqlachemy_adapter.py内第134行
values.append(self.parse_rule(ptype, rule).dict())
#导出dict时,包含id字段,id字段为NONE,postgreSQL数据库不允许id为null的数据插入。
未测试建议:能否在导出dict时排除id字段。修改原同行代码为:
values.append(self.parse_rule(ptype, rule).dict(exclude={"id"}))

和fastapi-scheduler搭配使用

已经用fastapi-scheduler创建了定时任务,不想随便让人通过注册账户登入后台来控制定时任务。但还想用fastapi-user-auth和fastapi_amis_admin的功能,有什么办法吗?

.

.

casbin验证资源权限时,角色的根节点权限在叶节点无继承。

g2_,_配置,左侧继承对象,右侧被继承对象。
查询数据结果  g2 , AuthAdminSite ,Auth>UserAuthApp。测试无法继承权限。
通过casbin网站编辑器测试使用源代码配置模型,使用Policy如下:
测试结果:
1695886884586
写入数据库的g2资源继承关系时,是否左右关系反置。

页面问题

实际的 Hero
image

页面显示异常
image

创建英雄时, 如果先填写英雄名称, 则不能选择团队; 如果先选择团队, 再填写名称是可以的
image

alembic upgrade head 升级数据库时报错

新增任意表后,执行 alembic revision --autogenerate -m "something" 生成脚本,执行 alembic upgrade head 时报错。

发生异常: ProgrammingError
(sqlalchemy.dialects.postgresql.asyncpg.ProgrammingError) <class 'asyncpg.exceptions.InvalidTableDefinitionError'>: column "group_id" is in a primary key
[SQL: ALTER TABLE auth_group_roles ALTER COLUMN group_id DROP NOT NULL]
(Background on this error at: https://sqlalche.me/e/14/f405)

使用数据库为 pgsql,使用的驱动为 asyncpg。如需更多信息,请告知。

我发现在数据库后台新增加的数据 需要我重新启动服务刷新才会出现请问这个和数据库连接有关系么

我使用的是create_async_engine 创建的异步数据库引擎
`
engine = create_async_engine(url='mysql+aiomysql://root:123456@localhost:3306/fastapi')

使用JwtTokenStore创建auth对象

auth = Auth(
db=AsyncDatabase(engine),
token_store=JwtTokenStore(secret_key='1558858285966')
)

将auth对象传入AdminSite

site = AuthAdminSite(
settings=Settings(database_url_async='mysql+aiomysql://root:123456@localhost:3306/fastapi'),
auth=auth
)`

postgreSQL数据库查看角色管理报错

用postgreSQL数据库,查看角色管理报错。
原因是 fastapi_user_auth\auth\models.py内第99行 ,CasbinSubjectRolesQuery里用了GROUP_CONCAT方法,postgreSQL不支持该方法,需要替换为string_agg

启动时遇到“sqlite3.OperationalError: database is locked”错误

更新到了最新版,发现启动后@app.on_event("startup")执行到“ await site.router.startup() ”时出错,错误信息如下

ERROR:    Traceback (most recent call last):
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1900, in _execute_context
    self.dialect.do_execute(
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\default.py", line 736, in do_execute
    cursor.execute(statement, parameters)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\dialects\sqlite\aiosqlite.py", line 100, in execute
    self._adapt_connection._handle_exception(error)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\dialects\sqlite\aiosqlite.py", line 228, in _handle_exception
    raise error
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\dialects\sqlite\aiosqlite.py", line 82, in execute
    self.await_(_cursor.execute(operation, parameters))
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 68, in await_only
    return current.driver.switch(awaitable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 121, in greenlet_spawn
    value = await result
            ^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\cursor.py", line 48, in execute
    await self._execute(self._cursor.execute, sql, parameters)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\cursor.py", line 40, in _execute
    return await self._conn._execute(fn, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\core.py", line 133, in _execute
    return await future
           ^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\core.py", line 106, in run
    result = function()
             ^^^^^^^^^^
sqlite3.OperationalError: database is locked

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\starlette\routing.py", line 677, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\starlette\routing.py", line 566, in __aenter__
    await self._router.startup()
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\starlette\routing.py", line 654, in startup
    await handler()
  File "C:\WebProjects\my_experiments\backend\main.py", line 50, in startup
    await site.router.startup()
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\starlette\routing.py", line 654, in startup
    await handler()
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\fastapi_user_auth\admin\admin.py", line 387, in _load_policy
    await self.load_policy()
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\fastapi_user_auth\admin\admin.py", line 392, in load_policy
    await update_casbin_site_grouping(self.site.auth.enforcer, self.site)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\fastapi_user_auth\admin\utils.py", line 102, in update_casbin_site_grouping
    await enforcer.add_named_grouping_policies("g2", add_roles)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\casbin\async_management_enforcer.py", line 249, in add_named_grouping_policies
    rules_added = await self._add_policies("g", ptype, rules)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\casbin\async_internal_enforcer.py", line 143, in _add_policies
    result = await self.adapter.add_policies(sec, ptype, rules)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\fastapi_user_auth\utils\sqlachemy_adapter.py", line 153, in add_policies
    await self.db.async_execute(insert(self._db_class).values(values))
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\ext\asyncio\session.py", line 215, in execute
    result = await greenlet_spawn(
             ^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 126, in greenlet_spawn
    result = context.throw(*sys.exc_info())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlmodel\orm\session.py", line 101, in execute
    return super().execute(  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\orm\session.py", line 1712, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1705, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\sql\elements.py", line 333, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1572, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1943, in _execute_context
    self._handle_dbapi_exception(
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 2124, in _handle_dbapi_exception
    util.raise_(
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\util\compat.py", line 208, in raise_
    raise exception
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1900, in _execute_context
    self.dialect.do_execute(
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\engine\default.py", line 736, in do_execute
    cursor.execute(statement, parameters)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\dialects\sqlite\aiosqlite.py", line 100, in execute
    self._adapt_connection._handle_exception(error)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\dialects\sqlite\aiosqlite.py", line 228, in _handle_exception
    raise error
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\dialects\sqlite\aiosqlite.py", line 82, in execute
    self.await_(_cursor.execute(operation, parameters))
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 68, in await_only
    return current.driver.switch(awaitable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 121, in greenlet_spawn
    value = await result
            ^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\cursor.py", line 48, in execute
    await self._execute(self._cursor.execute, sql, parameters)
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\cursor.py", line 40, in _execute
    return await self._conn._execute(fn, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\core.py", line 133, in _execute
    return await future
           ^^^^^^^^^^^^
  File "c:\WebProjects\my_experiments\.venv\Lib\site-packages\aiosqlite\core.py", line 106, in run
    result = function()
             ^^^^^^^^^^
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked
[SQL: INSERT INTO auth_casbin_rule (id, ptype, v0, v1, v2, v3, v4, v5) VALUES (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: (None, 'g2', 'Auth>UserAuthApp', 'Auth>CasbinRuleAdmin', None, None, None, None, None, 'g2', 'Auth>UserAuthApp', 'Auth>LoginHistoryAdmin', None, None, None, None, None, 'g2', 'AuthAdminSite', '283002fe81d9bbae', None, None, None, None, None, 'g2', 'Auth>UserAuthApp', 'Auth>RoleAdmin', None, None, None, None, None, 'g2', 'Auth>UserAuthApp', 'Auth>UserLoginFormAdmin', None, None, None, None, None, 'g2', 'Auth>UserAuthApp', 'Auth>UserAdmin', None, None, None, None, None, 'g2', 'AuthAdminSite', 'Auth>UserAuthApp', None, None, None, None, None, 'g2', 'f736aeb720a47010', 'b19cc208ea4e2622', None, None, None, None, None, 'g2', 'Auth>UserAuthApp', 'Auth>UserRegFormAdmin', None, None, None, None, None, 'g2', 'AuthAdminSite', 'f736aeb720a47010', None, None, None, None, None, 'g2', 'Auth>UserAuthApp', 'Auth>UserInfoFormAdmin', None, None, None, None, None, 'g2', 'AuthAdminSite', 'a42c53cb3721761c', None, None, None, None, None, 'g2', 'AuthAdminSite', '0765cdf25a1f9548', None, None, None, None, None, 'g2', 'f736aeb720a47010', 'a5d1401d49a19d97', None, None, None, None)]
(Background on this error at: https://sqlalche.me/e/14/e3q8)

2023-09-21 09:38:08,163 INFO sqlalchemy.engine.Engine ROLLBACK
ERROR:    Application startup failed. Exiting.

这个要怎么解决?

[postgresql+asyncpg] 登录提示'接口报错:"Internal Server Error"'

fastapi_amis_admin.amis_admin 改为 fastapi_user_auth.site 中的 AuthAdminSite 后,能够显示登录页面,但是输入账号密码后,提示 "Internal Server Error"

使用的是 fastapi_user_auth 内部自带的 models,模型部分无任何修改。

检查日志,有如下输出:

sys:1: SAWarning: Class SelectOfScalar will not make use of SQL compilation caching as it does not set the 'inherit_cache' attribute to ``True``.  This can have significant performance implications including some performance degradations in comparison to prior SQLAlchemy versions.  Set this attribute to True if this object can make use of the cache key generated by the superclass.  Alternatively, this attribute may be set to False which will disable this warning. (Background on this error at: https://sqlalche.me/e/14/cprf)
INFO:     127.0.0.1:53295 - "POST /auth/form/login/api HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 366, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\applications.py", line 261, in __call__
    await super().__call__(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
    raise e
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\routing.py", line 408, in handle
    await self.app(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\applications.py", line 261, in __call__
    await super().__call__(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
    raise e
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\starlette\routing.py", line 61, in app
    response = await func(request)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\routing.py", line 217, in app
    solved_result = await solve_dependencies(
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi\dependencies\utils.py", line 527, in solve_dependencies
    solved = await call(**sub_values)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi_amis_admin\amis_admin\admin.py", line 739, in route
    return await self.handle(request, data)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi_user_auth\admin.py", line 39, in handle
    user = await request.auth.authenticate_user(username=data.username, password=data.password)  # type:ignore
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\fastapi_user_auth\auth\auth.py", line 92, in authenticate_user
    if user and self.pwd_context.verify(pwd, pwd2):  # 用户存在 且 密码验证通过
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\passlib\context.py", line 2343, in verify
    record = self._get_or_identify_record(hash, scheme, category)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\passlib\context.py", line 2031, in _get_or_identify_record
    return self._identify_record(hash, category)
  File "$SOMEPATH\FastAPIDemo\venv\lib\site-packages\passlib\context.py", line 1132, in identify_record
    raise exc.UnknownHashError("hash could not be identified")
passlib.exc.UnknownHashError: hash could not be identified

demo项目源码

大佬方便提供一下demo项目的源码吗,因为根据文档,还是有一些地方不清楚如何实现,如果有一个现成的项目来参考那就很棒了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.