Skip to content

配置说明

本文档说明 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;"
}
参数说明
ServerMySQL 服务器地址
PortMySQL 端口
Database数据库名称
User用户名
Password密码

Redis 配置

json
"Redis": {
  "ConnectionString": "localhost:6379,password=,defaultDatabase=0"
}
参数说明
ConnectionStringRedis 连接字符串

JWT 配置

json
"Security": {
  "Session": {
    "Jwt": {
      "SecretKey": "your-secret-key-at-least-32-characters-long",
      "Issuer": "youlai-aspnet",
      "AccessTokenTTL": 7200,
      "RefreshTokenTTL": 604800
    }
  }
}
参数说明默认值
SecretKeyJWT 签名密钥(至少 32 字符)-
IssuerToken 签发者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.AspNetCoreASP.NET Core 框架日志级别
Microsoft.EntityFrameworkCoreEF Core 日志级别

Kestrel 配置

json
"Kestrel": {
  "Endpoints": {
    "Http": {
      "Url": "http://*:8000"
    }
  }
}
参数说明
Url监听地址和端口

环境变量覆盖

生产环境可通过环境变量覆盖配置:

环境变量对应配置
Database__ConnectionString数据库连接字符串
Redis__ConnectionStringRedis 连接字符串
Security__Session__Jwt__SecretKeyJWT 密钥

示例:

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生产环境配置

基于 MIT 许可发布