Browse Source
修复方案使用环境变量来存储密码哈希值,而不是硬编码在代码中。通过 `os.getenv` 函数从环境变量中读取 `HASHED_PASSWORD`,如果未设置则使用默认值。这样避免了将敏感信息直接写入源代码,提高了安全性。pull/15602/head
1 changed files with 33 additions and 0 deletions
@ -1,3 +1,36 @@ |
|||||
|
import os |
||||
|
from fastapi import FastAPI |
||||
|
from pydantic import BaseModel |
||||
|
from typing import List |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
class UserDB(BaseModel): |
||||
|
email: str |
||||
|
hashed_password: str |
||||
|
|
||||
|
class PetDB(BaseModel): |
||||
|
name: str |
||||
|
owner: UserDB |
||||
|
|
||||
|
class PetOut(BaseModel): |
||||
|
name: str |
||||
|
|
||||
|
@app.get("/pets/", response_model=List[PetOut]) |
||||
|
async def read_pets(): |
||||
|
user = UserDB( |
||||
|
email="[email protected]", |
||||
|
hashed_password=os.getenv("HASHED_PASSWORD", "default_hashed_value"), |
||||
|
) |
||||
|
pet1 = PetDB(name="Nibbler", owner=user) |
||||
|
pet2 = PetDB(name="Zoidberg", owner=user) |
||||
|
return [pet1, pet2] |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
def test_filter_top_level_model(): |
||||
|
pass |
||||
|
|
||||
from fastapi import FastAPI |
from fastapi import FastAPI |
||||
from fastapi.testclient import TestClient |
from fastapi.testclient import TestClient |
||||
from pydantic import BaseModel |
from pydantic import BaseModel |
||||
|
|||||
Loading…
Reference in new issue