修改依赖库

This commit is contained in:
haotian 2025-09-09 11:36:04 +08:00
parent 08ae1f44c0
commit 1029e71324

View File

@ -3,7 +3,8 @@ from typing import Optional
from fastapi import Depends, FastAPI, HTTPException, status from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jose import JWTError, jwt # from jose import JWTError, jwt
import jwt
from passlib.context import CryptContext from passlib.context import CryptContext
from pydantic import BaseModel from pydantic import BaseModel
@ -73,9 +74,9 @@ def authenticate_user(fake_db, username: str, password: str):
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None): def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
to_encode = data.copy() to_encode = data.copy()
if expires_delta: if expires_delta:
expire = datetime.utcnow() + expires_delta expire = datetime.now(datetime.timezone.utc) + expires_delta
else: else:
expire = datetime.utcnow() + timedelta(minutes=15) expire = datetime.now(datetime.timezone.utc) + timedelta(minutes=15)
to_encode.update({"exp": expire}) to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt return encoded_jwt
@ -93,7 +94,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
if username is None: if username is None:
raise credentials_exception raise credentials_exception
token_data = TokenData(username=username) token_data = TokenData(username=username)
except JWTError: except jwt.exceptions.PyJWTError:
raise credentials_exception raise credentials_exception
user = get_user(fake_users_db, username=token_data.username) user = get_user(fake_users_db, username=token_data.username)
if user is None: if user is None: