我有一个与 Web 应用程序通信的 FastAPI 服务器。我的网络应用程序也有两种类型的用户:用户(非管理员)和管理员。我向 FastAPI 添加了全局依赖项来验证用户。我希望验证依赖项默认只允许管理员访问端点,并有一些装饰器(或类似的东西)来允许非管理员访问某些路由。这样,就不会有人意外地创建一条只供管理员使用的公共路由。
def verify_token(request: Request):
# make sure the user's auth token is valid
# retrieve the user's details from the database
# make sure user is Admin, otherwise throw HTTP exception
return True
app = FastAPI(
title="My App",
dependencies=[Depends(verify_token)]
)
@app.get(/admins_only)
def admins_only():
# this works well!
return {'result': 2}
@app.get(/non_admin_route)
def non_admin_route():
# this doesn't work because verify_token
# only allows admins by default, but it should
# be accessible to non admins …
Run Code Online (Sandbox Code Playgroud) 在 C++ 中,您可以创建具有可变数量参数的元组。如果不显式硬编码泛型的数量,我将如何在 Java 中实现类似的东西。我希望能够做类似的事情:
元组<整数、整数、字符串> t
但不强迫自己在元组中使用三个项目。