Python[fastapi]auth2scopes

2024-06-16 14:11:29 发布

您现在位置:Python中文网/ 问答频道 /正文

以下指南: https://fastapi.tiangolo.com/advanced/security/oauth2-scopes/

我正在尝试在其他路径上实现auth2作用域 来自fastapi中的不同路由器

main.py如下所示:

from fastapi import FastAPI
from security.routes import security_router
from mig_ws.routes import ws_router


app = FastAPI()

#Note security attached to root meaning http://localhost:8999 
app.include_router(
    security_router,
    prefix="",
    tags=["SECURITY"],
    responses={404: {"description": "Page not exist"}},
)


#http://localhost:8999/ws
app.include_router(
    ws_router,
    prefix="/ws",
    tags=["MIG_WS"],
    responses={404: {"description": "Page not exist"}},
)


所以在文档中它看起来像http://localhost:8999/docs

enter image description here

文件夹树:

Webserver
├── main.py
├── mig_ws
│   ├── __init__.py
│   ├── modles.py
│   └── routes.py
├── security
│   ├── models.py
│   └── routes.py

auth2作用域与security/router.py相关 如何在mig_ws文件夹上实现auth2作用域规则? 意味着需要从中运行某些内容的用户 http:localhost:899/ws需要授权吗

security/routes.py上的实现示例

@security_router.get("/users/me/", response_model=User)
async def read_users_me(current_user: User = Depends(get_current_active_user)):
    return current_user

ws/routes.py上的代码示例

# this actually hits on http:localhost:899/ws/generate_ctgan_mode/

@ws_router.get("/generate_ctgan_model/{filename}")

async def Generate_ctgan_model(filename: str):
     return generate_ctgan_model(filename)


Tags: frompyimportlocalhosthttpmodelwsauth2