refactor: 所有配置优先从环境变量读取,完全支持Docker环境变量注入
This commit is contained in:
parent
739a37f24d
commit
f2d4351145
@ -52,15 +52,26 @@ builder.Services.AddCors(options =>
|
|||||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||||
.AddJwtBearer(options =>
|
.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
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
{
|
{
|
||||||
ValidateIssuer = true,
|
ValidateIssuer = true,
|
||||||
ValidateAudience = true,
|
ValidateAudience = true,
|
||||||
ValidateLifetime = true,
|
ValidateLifetime = true,
|
||||||
ValidateIssuerSigningKey = true,
|
ValidateIssuerSigningKey = true,
|
||||||
ValidIssuer = "AssetManager",
|
ValidIssuer = jwtIssuer,
|
||||||
ValidAudience = "AssetManager",
|
ValidAudience = jwtAudience,
|
||||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your-strong-secret-key-here-2026"))
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecretKey))
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -22,9 +22,10 @@ public static class SqlSugarConfig
|
|||||||
throw new InvalidOperationException("SqlSugarConfig has not been initialized. Call Initialize() first.");
|
throw new InvalidOperationException("SqlSugarConfig has not been initialized. Call Initialize() first.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从配置读取连接字符串,支持环境变量和appsettings.json
|
// 优先从环境变量读取连接字符串
|
||||||
var connectionString = _configuration.GetConnectionString("Default")
|
var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__Default")
|
||||||
?? throw new InvalidOperationException("Connection string 'Default' not found in configuration.");
|
?? _configuration.GetConnectionString("Default")
|
||||||
|
?? throw new InvalidOperationException("Connection string 'Default' not found in environment variables or configuration.");
|
||||||
|
|
||||||
return new SqlSugarScope(new ConnectionConfig()
|
return new SqlSugarScope(new ConnectionConfig()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -23,9 +23,10 @@ public class MarketDataService : IMarketDataService
|
|||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_httpClient = httpClientFactory.CreateClient();
|
_httpClient = httpClientFactory.CreateClient();
|
||||||
// 从配置读取 Tiingo API Key,优先环境变量
|
// 优先从环境变量读取 Tiingo API Key
|
||||||
_tiingoApiKey = Environment.GetEnvironmentVariable("TIINGO_API_KEY")
|
_tiingoApiKey = Environment.GetEnvironmentVariable("Tiingo__ApiKey")
|
||||||
?? configuration["Tiingo:ApiKey"]
|
?? Environment.GetEnvironmentVariable("TIINGO_API_KEY")
|
||||||
|
?? configuration["Tiingo:ApiKey"]
|
||||||
?? "bd00fee76d3012b047473078904001b33322cb46";
|
?? "bd00fee76d3012b047473078904001b33322cb46";
|
||||||
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}");
|
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Token {_tiingoApiKey}");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,9 +14,18 @@ public class JwtService
|
|||||||
|
|
||||||
public JwtService(IConfiguration configuration)
|
public JwtService(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_secretKey = configuration["Jwt:SecretKey"] ?? "your-strong-secret-key-here-2026";
|
// 优先从环境变量读取
|
||||||
_issuer = configuration["Jwt:Issuer"] ?? "AssetManager";
|
_secretKey = Environment.GetEnvironmentVariable("Jwt__SecretKey")
|
||||||
_audience = configuration["Jwt:Audience"] ?? "AssetManager";
|
?? 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)
|
public string GenerateToken(string userId, string userName, string email)
|
||||||
|
|||||||
@ -14,8 +14,14 @@ public class WechatService
|
|||||||
public WechatService(HttpClient httpClient, IConfiguration configuration)
|
public WechatService(HttpClient httpClient, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_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<WechatAuthResult> GetOpenIdAsync(string code)
|
public async Task<WechatAuthResult> GetOpenIdAsync(string code)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user