验证key

This commit is contained in:
龟男日记\www 2026-02-16 04:39:32 +08:00
parent 029c85f1a3
commit ee3ec61548
3 changed files with 20 additions and 2 deletions

View File

@ -10,8 +10,8 @@ REDIS_PORT=6379
REDIS_PASSWORD= REDIS_PASSWORD=
REDIS_DB=0 REDIS_DB=0
# 公开 API 密钥(用于外部调用 /api/public/* 端点 # Store API Key用于访问 hero-slider 和 product-recommendations 接口
PUBLIC_API_KEY=your-secret-api-key-here STORE_API_KEY=your-store-api-key-here
# Cloudflare R2 配置 # Cloudflare R2 配置
CLOUDFLARE_R2_BUCKET=your-bucket CLOUDFLARE_R2_BUCKET=your-bucket

View File

@ -6,9 +6,18 @@ import { getCache, setCache } from '@/lib/redis'
/** /**
* GET /api/public/hero-slider * GET /api/public/hero-slider
* *
* x-store-api-key
*/ */
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
try { try {
// 验证 API Key
const apiKey = req.headers.get('x-store-api-key')
const validApiKey = process.env.STORE_API_KEY
if (!apiKey || !validApiKey || apiKey !== validApiKey) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
}
// 生成缓存 key // 生成缓存 key
const cacheKey = 'hero-slider:data' const cacheKey = 'hero-slider:data'

View File

@ -6,9 +6,18 @@ import { getCache, setCache } from '@/lib/redis'
/** /**
* GET /api/public/product-recommendations * GET /api/public/product-recommendations
* *
* x-store-api-key
*/ */
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
try { try {
// 验证 API Key
const apiKey = req.headers.get('x-store-api-key')
const validApiKey = process.env.STORE_API_KEY
if (!apiKey || !validApiKey || apiKey !== validApiKey) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
}
// 生成缓存 key // 生成缓存 key
const cacheKey = 'product-recommendations:data' const cacheKey = 'product-recommendations:data'