FastAPI - 安全(Security)4

server/2024/9/23 10:19:10/

删除routers/member.py文件

修改main.py文件

# -*- coding:utf-8 –*-
from fastapi import Depends,FastAPIfrom fastapi.security import OAuth2PasswordBearer
from typing import Annotatedoauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")app = FastAPI(title="文档标题",description="关于API文档的补充说明",version="1.0.0",docs_url="/docs"
)@app.get("/items/")
async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):return {"token": token}

https://fastapi.tiangolo.com/zh/tutorial/security/first-steps

创建用户模型

修改main.py文件

# -*- coding:utf-8 –*-
from fastapi import Depends, FastAPIfrom fastapi.security import OAuth2PasswordBearer
from typing import Annotated
from pydantic import BaseModeloauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")class User(BaseModel):username: stremail: str | None = Nonefull_name: str | None = Nonedisabled: bool | None = Nonedef fake_decode_token(token):return User(username=token + "fakedecoded", email="vvv@example.com", full_name="vvv lao")async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):user = fake_decode_token(token)return userapp = FastAPI(title="文档标题",description="关于API文档的补充说明",version="1.0.0",docs_url="/docs"
)@app.get("/users/me")
async def read_users_me(current_user: Annotated[User, Depends(get_current_user)]):return current_user

其中get_current_user使用oauth2_scheme作为依赖项

https://fastapi.tiangolo.com/zh/tutorial/security/simple-oauth2


http://www.ppmy.cn/server/45917.html

相关文章

bert_baseline

BERT (Bidirectional Encoder Representations from Transformers) 是一种基于 Transformer 架构的预训练语言模型,由 Google AI Language 团队在 2018 年提出。BERT 的主要贡献在于其双向的上下文编码能力和通过预训练-微调(pre-training-finetuning&am…

2024国内常见的教学质量管理系统有哪些

教学质量管理系统是一种针对学校的管理工具,旨在提高教学质量和效果。教学质量管理是为保证培养规格,促使教学效果达到课程计划、教学大纲和教科书所规定的要求,对教学过程和效果进行指导、控制的活动。是教学管理的核心。它能帮助学校建立一…

笔记本电脑网络卡顿 Windows电脑网络提速通用方法

问题环境 在使用笔记本电脑连接Wi-Fi进行在线腾讯视频播放时加载页面缓慢等等困顿情况,对比相同情况下的手机则网络没有任何问题,浏览速度都是极快。 判断是否网络连接速度缓慢 点击同类型的在线视频播放网页,例如腾讯视频,进入观…

JavaScript的内存管理机制

No.内容链接1Openlayers 【入门教程】 - 【源代码示例300】 2Leaflet 【入门教程】 - 【源代码图文示例 150】 3Cesium 【入门教程】 - 【源代码图文示例200】 4MapboxGL【入门教程】 - 【源代码图文示例150】 5前端就业宝典 【面试题详细答案 1000】 文章目录 一、内存…

信号稳定,性能卓越!德思特礁鲨系列MiMo天线正式发布!

作者介绍 礁鲨系列天线,以其独特的外观设计和强大的性能,成为德思特Panorama智能天线家族的最新成员。这款天线不仅稳定提供5G、WIFI和GNSS信号,更能在各类复杂环境中展现出卓越的性能。它的设计灵感来源于海洋中的礁鲨,象征着力量…

一些常用的 ADB(Android Debug Bridge)命令

以下是一些常用的 ADB&#xff08;Android Debug Bridge&#xff09;命令&#xff1a; 设备相关命令 查看连接的设备&#xff1a; adb devices连接设备&#xff1a; adb connect <device_ip_address>断开设备&#xff1a; adb disconnect <device_ip_address>应用…

phpstudy配置网站伪静态

apache的伪静态写法&#xff1a; RewriteEngine On RewriteCond % {REQUEST_FILENAME} !-f RewriteCond % (REQUEST_FILENAME) !-d RewriteRule ^(.*)$ indexp?/$1 [QSA, PT,L] nginx写法&#xff1a; location / { index index.html index.php; #autoindex on; if(!…

Linux CFS调度器简介

文章目录 前言一、概要二、实现2.1 简介2.2 算法实现2.3 内核源码 三、特点四、调度策略五、调度类参考资料 前言 早期的Linux调度器采用了简化的设计&#xff0c;显然并不针对具有许多处理器甚至超线程的大规模架构。Linux 1.2调度器使用循环队列对可运行任务进行管理&#x…