配置说明
本文档说明 youlai-aspnet 后端项目的核心配置项,配置入口为 appsettings*.json。
配置文件
| 文件 | 说明 |
|---|---|
appsettings.json | 基础配置(所有环境共享) |
appsettings.Development.json | 开发环境配置 |
appsettings.Production.json | 生产环境配置 |
数据库配置
json
"Database": {
"ConnectionString": "Server=localhost;Port=3306;Database=youlai_admin;User=root;Password=123456;CharSet=utf8mb4;"
}| 参数 | 说明 |
|---|---|
Server | MySQL 服务器地址 |
Port | MySQL 端口 |
Database | 数据库名称 |
User | 用户名 |
Password | 密码 |
Redis 配置
json
"Redis": {
"ConnectionString": "localhost:6379,password=,defaultDatabase=0"
}| 参数 | 说明 |
|---|---|
ConnectionString | Redis 连接字符串 |
JWT 配置
json
"Security": {
"Session": {
"Jwt": {
"SecretKey": "your-secret-key-at-least-32-characters-long",
"Issuer": "youlai-aspnet",
"AccessTokenTTL": 7200,
"RefreshTokenTTL": 604800
}
}
}| 参数 | 说明 | 默认值 |
|---|---|---|
SecretKey | JWT 签名密钥(至少 32 字符) | - |
Issuer | Token 签发者 | youlai-aspnet |
AccessTokenTTL | 访问令牌有效期(秒) | 7200(2 小时) |
RefreshTokenTTL | 刷新令牌有效期(秒) | 604800(7 天) |
限流配置
json
"RateLimit": {
"Enabled": true,
"PermitLimit": 100,
"Window": 60
}| 参数 | 说明 |
|---|---|
Enabled | 是否启用限流 |
PermitLimit | 时间窗口内允许的最大请求数 |
Window | 时间窗口(秒) |
文件存储配置
json
"FileStorage": {
"Type": "local",
"Local": {
"StoragePath": "/data/upload",
"BaseUrl": "http://localhost:8000"
}
}| 参数 | 说明 |
|---|---|
Type | 存储类型(local) |
StoragePath | 文件存储路径 |
BaseUrl | 访问基础 URL |
日志配置
json
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Warning"
}
}| 参数 | 说明 |
|---|---|
Default | 默认日志级别 |
Microsoft.AspNetCore | ASP.NET Core 框架日志级别 |
Microsoft.EntityFrameworkCore | EF Core 日志级别 |
Kestrel 配置
json
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:8000"
}
}
}| 参数 | 说明 |
|---|---|
Url | 监听地址和端口 |
环境变量覆盖
生产环境可通过环境变量覆盖配置:
| 环境变量 | 对应配置 |
|---|---|
Database__ConnectionString | 数据库连接字符串 |
Redis__ConnectionString | Redis 连接字符串 |
Security__Session__Jwt__SecretKey | JWT 密钥 |
示例:
bash
export Database__ConnectionString="Server=prod-mysql;Port=3306;Database=youlai_admin;User=prod_user;Password=prod_password"
export Security__Session__Jwt__SecretKey="your-production-secret-key"配置类映射
配置通过 Options 模式映射到强类型:
csharp
// JwtOptions.cs
public class JwtOptions
{
public string SecretKey { get; set; } = string.Empty;
public string Issuer { get; set; } = "youlai-aspnet";
public int AccessTokenTTL { get; set; } = 7200;
public int RefreshTokenTTL { get; set; } = 604800;
}
// Program.cs
builder.Services.Configure<JwtOptions>(
builder.Configuration.GetSection("Security:Session:Jwt"));相关文件
| 文件 | 说明 |
|---|---|
appsettings.json | 基础配置 |
appsettings.Development.json | 开发环境配置 |
appsettings.Production.json | 生产环境配置 |
