From f2d4351145b7dad7eaf89052672663c7044dbd3b Mon Sep 17 00:00:00 2001 From: claw_bot Date: Mon, 9 Mar 2026 08:56:26 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=89=80=E6=9C=89=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BC=98=E5=85=88=E4=BB=8E=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E8=AF=BB=E5=8F=96=EF=BC=8C=E5=AE=8C=E5=85=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81Docker=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E6=B3=A8?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AssetManager.API/Program.cs | 17 ++++++++++++++--- AssetManager.Data/SqlSugarConfig.cs | 7 ++++--- .../Services/MarketDataService.cs | 7 ++++--- AssetManager.Services/Services/JwtService.cs | 15 ++++++++++++--- AssetManager.Services/Services/WechatService.cs | 10 ++++++++-- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/AssetManager.API/Program.cs b/AssetManager.API/Program.cs index 53d8c1c..b1fabd0 100644 --- a/AssetManager.API/Program.cs +++ b/AssetManager.API/Program.cs @@ -52,15 +52,26 @@ builder.Services.AddCors(options => builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { + // 优先从环境变量读取JWT配置 + var jwtSecretKey = Environment.GetEnvironmentVariable("Jwt__SecretKey") + ?? builder.Configuration["Jwt:SecretKey"] + ?? "your-strong-secret-key-here-2026"; + var jwtIssuer = Environment.GetEnvironmentVariable("Jwt__Issuer") + ?? builder.Configuration["Jwt:Issuer"] + ?? "AssetManager"; + var jwtAudience = Environment.GetEnvironmentVariable("Jwt__Audience") + ?? builder.Configuration["Jwt:Audience"] + ?? "AssetManager"; + options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, - ValidIssuer = "AssetManager", - ValidAudience = "AssetManager", - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your-strong-secret-key-here-2026")) + ValidIssuer = jwtIssuer, + ValidAudience = jwtAudience, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecretKey)) }; }); diff --git a/AssetManager.Data/SqlSugarConfig.cs b/AssetManager.Data/SqlSugarConfig.cs index 49924c9..55244c1 100644 --- a/AssetManager.Data/SqlSugarConfig.cs +++ b/AssetManager.Data/SqlSugarConfig.cs @@ -22,9 +22,10 @@ public static class SqlSugarConfig throw new InvalidOperationException("SqlSugarConfig has not been initialized. Call Initialize() first."); } - // 从配置读取连接字符串,支持环境变量和appsettings.json - var connectionString = _configuration.GetConnectionString("Default") - ?? throw new InvalidOperationException("Connection string 'Default' not found in configuration."); + // 优先从环境变量读取连接字符串 + var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__Default") + ?? _configuration.GetConnectionString("Default") + ?? throw new InvalidOperationException("Connection string 'Default' not found in environment variables or configuration."); return new SqlSugarScope(new ConnectionConfig() { diff --git a/AssetManager.Infrastructure/Services/MarketDataService.cs b/AssetManager.Infrastructure/Services/MarketDataService.cs index ea124a7..2f62bdf 100644 --- a/AssetManager.Infrastructure/Services/MarketDataService.cs +++ b/AssetManager.Infrastructure/Services/MarketDataService.cs @@ -23,9 +23,10 @@ public class MarketDataService : IMarketDataService { _logger = logger; _httpClient = httpClientFactory.CreateClient(); - // 从配置读取 Tiingo API Key,优先环境变量 - _tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY") - ?? configuration["Tiingo:ApiKey"] + // 优先从环境变量读取 Tiingo API Key + _tiingoApiKey = Environment.GetEnvironmentVariable("Tiingo__ApiKey") + ?? Environment.GetEnvironmentVariable("TIINGO_API_KEY") + ?? configuration["Tiingo:ApiKey"] ?? "bd00fee76d3012b047473078904001b33322cb46"; _httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}"); } diff --git a/AssetManager.Services/Services/JwtService.cs b/AssetManager.Services/Services/JwtService.cs index 2fcad7e..7df2917 100644 --- a/AssetManager.Services/Services/JwtService.cs +++ b/AssetManager.Services/Services/JwtService.cs @@ -14,9 +14,18 @@ public class JwtService public JwtService(IConfiguration configuration) { - _secretKey = configuration["Jwt:SecretKey"] ?? "your-strong-secret-key-here-2026"; - _issuer = configuration["Jwt:Issuer"] ?? "AssetManager"; - _audience = configuration["Jwt:Audience"] ?? "AssetManager"; + // 优先从环境变量读取 + _secretKey = Environment.GetEnvironmentVariable("Jwt__SecretKey") + ?? configuration["Jwt:SecretKey"] + ?? "your-strong-secret-key-here-2026"; + + _issuer = Environment.GetEnvironmentVariable("Jwt__Issuer") + ?? configuration["Jwt:Issuer"] + ?? "AssetManager"; + + _audience = Environment.GetEnvironmentVariable("Jwt__Audience") + ?? configuration["Jwt:Audience"] + ?? "AssetManager"; } public string GenerateToken(string userId, string userName, string email) diff --git a/AssetManager.Services/Services/WechatService.cs b/AssetManager.Services/Services/WechatService.cs index 206dcfd..444f724 100644 --- a/AssetManager.Services/Services/WechatService.cs +++ b/AssetManager.Services/Services/WechatService.cs @@ -14,8 +14,14 @@ public class WechatService public WechatService(HttpClient httpClient, IConfiguration configuration) { _httpClient = httpClient; - _appId = configuration["Wechat:AppId"] ?? "wx245f0f3ebcfcf5a7"; - _appSecret = configuration["Wechat:AppSecret"] ?? "809c740129bc8b434177ce12ef292dd0"; + // 优先从环境变量读取 + _appId = Environment.GetEnvironmentVariable("Wechat__AppId") + ?? configuration["Wechat:AppId"] + ?? "wx245f0f3ebcfcf5a7"; + + _appSecret = Environment.GetEnvironmentVariable("Wechat__AppSecret") + ?? configuration["Wechat:AppSecret"] + ?? "809c740129bc8b434177ce12ef292dd0"; } public async Task GetOpenIdAsync(string code)