FastAPI Security: API 보안 구현

2025. 2. 24. 11:41·파이썬/Fast API
반응형
# 기본 설치 (FastAPI에 포함되어 있음)
pip install fastapi[all]
pip install python-jose[cryptography]  # JWT 토큰용
pip install passlib[bcrypt]  # 비밀번호 해싱용

OAuth2 비밀번호 인증

from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jose import JWTError, jwt
from passlib.context import CryptContext

# 기본 설정
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

app = FastAPI()

@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    user = authenticate_user(form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password"
        )
    access_token = create_access_token(data={"sub": user.username})
    return {"access_token": access_token, "token_type": "bearer"}

API Key 인증

from fastapi.security import APIKeyHeader, APIKeyQuery

# 헤더 기반 API 키
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=True)

# 쿼리 파라미터 기반 API 키
api_key_query = APIKeyQuery(name="api_key", auto_error=True)

@app.get("/secure/header")
async def secure_endpoint(api_key: str = Depends(api_key_header)):
    if not is_valid_api_key(api_key):
        raise HTTPException(status_code=403, detail="Invalid API Key")
    return {"message": "Secured by header"}

HTTP Basic 인증

from fastapi.security import HTTPBasic, HTTPBasicCredentials
import secrets

security = HTTPBasic()

@app.get("/users/me")
async def get_current_user(credentials: HTTPBasicCredentials = Depends(security)):
    correct_username = secrets.compare_digest(
        credentials.username, "admin"
    )
    correct_password = secrets.compare_digest(
        credentials.password, "secret"
    )
    if not (correct_username and correct_password):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid credentials",
            headers={"WWW-Authenticate": "Basic"},
        )
    return {"username": credentials.username}

JWT 토큰 구현

from datetime import datetime, timedelta
from typing import Optional

SECRET_KEY = "your-secret-key"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30

def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
    to_encode = data.copy()
    expire = datetime.utcnow() + (
        expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    )
    to_encode.update({"exp": expire})
    encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
    return encoded_jwt

async def get_current_user(token: str = Depends(oauth2_scheme)):
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"},
    )
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        username: str = payload.get("sub")
        if username is None:
            raise credentials_exception
    except JWTError:
        raise credentials_exception
    return username

복합 보안 구현

from fastapi.security import OAuth2PasswordBearer, APIKeyHeader
from typing import Union

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)

async def get_authorization(
    token: Union[str, None] = Depends(oauth2_scheme),
    api_key: Union[str, None] = Depends(api_key_header)
):
    if token:
        return await get_current_user(token)
    elif api_key:
        return verify_api_key(api_key)
    raise HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Invalid authentication credentials",
    )

@app.get("/secure")
async def secure_endpoint(auth = Depends(get_authorization)):
    return {"auth": auth}

CORS 설정

from fastapi.middleware.cors import CORSMiddleware

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

보안 미들웨어

from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware

# HTTPS 리다이렉션
app.add_middleware(HTTPSRedirectMiddleware)

# 신뢰할 수 있는 호스트
app.add_middleware(
    TrustedHostMiddleware, 
    allowed_hosts=["example.com", "*.example.com"]
)

 

주요 기능 및 장점

  1. 다양한 인증 방식
    • OAuth2 지원
    • API 키 인증
    • Basic 인증
    • JWT 토큰
  2. 유연한 구성
    • 여러 인증 방식 조합
    • 커스텀 인증 로직
    • 세분화된 접근 제어
  3. 보안 기능
    • CORS 관리
    • HTTPS 강제
    • 호스트 검증
  4. 사용자 경험
    • 명확한 에러 메시지
    • 표준 인증 흐름
    • 문서화 지원

FastAPI.security를 사용하면 안전하고 표준을 준수하는 API 보안을 구현할 수 있습니다.

저작자표시 비영리 변경금지 (새창열림)

'파이썬 > Fast API' 카테고리의 다른 글

FastAPI에서 파일 다운로드 구현 가이드: 방식별 특징과 한글 파일명 문제 해결  (1) 2025.04.16
FastAPI Response Set-Cookie와 HttpOnly 보안 구현  (1) 2025.02.27
SlowAPI: FastAPI에서 Rate Limiting 구현  (0) 2025.02.23
FastAPI Permissions: 권한 관리 구현  (0) 2025.02.22
FastAPI-Users with MongoDB: JWT 인증  (0) 2025.02.21
'파이썬/Fast API' 카테고리의 다른 글
  • FastAPI에서 파일 다운로드 구현 가이드: 방식별 특징과 한글 파일명 문제 해결
  • FastAPI Response Set-Cookie와 HttpOnly 보안 구현
  • SlowAPI: FastAPI에서 Rate Limiting 구현
  • FastAPI Permissions: 권한 관리 구현
코샵
코샵
나의 코딩 일기장
    반응형
  • 코샵
    끄적끄적 코딩 공방
    코샵
    • 분류 전체보기 (725)
      • 스마트팜 (0)
      • 상품 추천 (223)
      • MongoDB (4)
      • 하드웨어 (17)
      • 일기장 (4)
      • 파이썬 (130)
        • Basic (41)
        • OpenCV (8)
        • Pandas (15)
        • PyQT (3)
        • SBC(Single Board Computer) (1)
        • 크롤링 (14)
        • Fast API (29)
        • Package (6)
      • Unity (138)
        • Tip (41)
        • Project (1)
        • Design Pattern (8)
        • Firebase (6)
        • Asset (2)
      • Linux (4)
      • C# (97)
        • Algorithm (11)
        • Window (7)
      • TypeScript (51)
        • CSS (10)
      • Git (11)
      • SQL (5)
      • Flutter (10)
        • Tip (1)
      • System (1)
      • BaekJoon (6)
      • Portfolio (2)
      • MacOS (1)
      • 유틸리티 (1)
      • 서비스 (6)
      • 자동화 (3)
      • Hobby (10)
        • 물생활 (10)
        • 식집사 (0)
  • 인기 글

  • 태그

    unity
    list
    카페24리뷰이관
    카페24리뷰
    리스트
    devlife
    쇼핑몰리뷰
    스크립트 실행 순서
    상품 리뷰 크롤링
    라떼우유
    파이썬
    긴유통기한우유
    programming101
    programmerlife
    ipcamera
    appdevelopment
    리뷰이관
    셀레니움
    rtsp
    Python
    스크립트 실행
    리뷰관리
    codingcommunity
    유니티
    스마트스토어리뷰
    C#
    믈레코비타멸균우유
    learntocode
    codingtips
    cv2
  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
코샵
FastAPI Security: API 보안 구현
상단으로

티스토리툴바