FastAPI-Azure-Auth 使用教程

FastAPI-Azure-Auth 使用教程

fastapi-azure-authEasy and secure implementation of Azure Entra ID (previously AD) for your FastAPI APIs 🔒 B2C, single- and multi-tenant support.项目地址:https://gitcode.com/gh_mirrors/fa/fastapi-azure-auth

1、项目介绍

FastAPI-Azure-Auth 是一个用于 FastAPI 应用的 Azure AD 认证库,旨在简化 FastAPI 应用的认证和授权流程。该库支持单租户、多租户以及 B2C 应用,使得开发者可以专注于业务逻辑而不必担心认证细节。

2、项目快速启动

安装

首先,通过 pip 安装 FastAPI-Azure-Auth 库:

pip install fastapi-azure-auth

配置 FastAPI 应用

在 FastAPI 应用中进行以下配置:

from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html

app = FastAPI(
    swagger_ui_oauth2_redirect_url='/oauth2-redirect',
    swagger_ui_init_oauth={
        'usePkceWithAuthorizationCodeGrant': True,
        'clientId': 'your-client-id'
    }
)

@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title=app.title + " - Swagger UI",
        oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
        init_oauth=app.swagger_ui_init_oauth
    )

配置 CORS

确保本地环境启用了 CORS:

from fastapi.middleware.cors import CORSMiddleware

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:8000"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

配置 FastAPI-Azure-Auth

在应用中配置 FastAPI-Azure-Auth:

from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer

azure_auth = SingleTenantAzureAuthorizationCodeBearer(
    app_client_id='your-client-id',
    tenant_id='your-tenant-id',
    scopes={
        'api://your-client-id/user_impersonation': 'user_impersonation',
    }
)

app.include_router(azure_auth.router)

3、应用案例和最佳实践

单租户应用

在单租户应用中,配置 Azure AD 和 FastAPI 如下:

from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer

azure_auth = SingleTenantAzureAuthorizationCodeBearer(
    app_client_id='your-client-id',
    tenant_id='your-tenant-id',
    scopes={
        'api://your-client-id/user_impersonation': 'user_impersonation',
    }
)

app.include_router(azure_auth.router)

多租户应用

在多租户应用中,配置 Azure AD 和 FastAPI 如下:

from fastapi_azure_auth import MultiTenantAzureAuthorizationCodeBearer

azure_auth = MultiTenantAzureAuthorizationCodeBearer(
    app_client_id='your-client-id',
    tenant_id='common',
    scopes={
        'api://your-client-id/user_impersonation': 'user_impersonation',
    }
)

app.include_router(azure_auth.router)

4、典型生态项目

FastAPI-Azure-Auth 可以与以下项目结合使用,以构建完整的认证和授权解决方案:

  • FastAPI: 用于构建高性能的 API。
  • Azure AD: 提供安全的认证和授权服务。
  • Swagger UI: 用于文档和测试 API。

通过这些项目的结合,可以构建出安全、高效的 API 服务。

fastapi-azure-authEasy and secure implementation of Azure Entra ID (previously AD) for your FastAPI APIs 🔒 B2C, single- and multi-tenant support.项目地址:https://gitcode.com/gh_mirrors/fa/fastapi-azure-auth

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方玉蜜United

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值