Commit 1354d36a by ryun

开关控制数据库密码加密

parent cfd2cf3e
using Microsoft.Extensions.Configuration; using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Performance.DtoModels.AppSettings; using Performance.DtoModels.AppSettings;
using Performance.Infrastructure; using Performance.Infrastructure;
using System; using Performance.Repository;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Performance.Api.Configurations namespace Performance.Api.Configurations
{ {
...@@ -21,7 +20,12 @@ public static void AddAppSettingConfiguration(this IServiceCollection services, ...@@ -21,7 +20,12 @@ public static void AddAppSettingConfiguration(this IServiceCollection services,
.Configure<HuyiSmsConfig>(configuration.GetSection("HuyiSmsConfig")) .Configure<HuyiSmsConfig>(configuration.GetSection("HuyiSmsConfig"))
.Configure<EmailOptions>(configuration.GetSection("EmailOptions")) .Configure<EmailOptions>(configuration.GetSection("EmailOptions"))
.Configure<RateLimitingConfig>(configuration.GetSection("RateLimitingConfig")) .Configure<RateLimitingConfig>(configuration.GetSection("RateLimitingConfig"))
.Configure<WebapiUrl>(configuration.GetSection("WebapiUrl")); .Configure<WebapiUrl>(configuration.GetSection("WebapiUrl"))
.Configure<AppSQLEncrypt>(configuration.GetSection("AppSQLEncrypt"));
var options = services.BuildServiceProvider().GetRequiredService<IOptions<AppSQLEncrypt>>();
AppSQLEncryptConfig.Instance = options?.Value ?? new AppSQLEncrypt();
} }
} }
} }
...@@ -49,6 +49,16 @@ ...@@ -49,6 +49,16 @@
相对 相对
</summary> </summary>
</member> </member>
<member name="T:Performance.DtoModels.AppSettings.AppSQLEncrypt">
<summary>
数据库密码加密
</summary>
</member>
<member name="P:Performance.DtoModels.AppSettings.AppSQLEncrypt.IsEncryption">
<summary>
是否加密 true 加密 false 明文
</summary>
</member>
<member name="P:Performance.DtoModels.AppSettings.RateLimitingConfig.Endpoints"> <member name="P:Performance.DtoModels.AppSettings.RateLimitingConfig.Endpoints">
<summary> <summary>
路径 路径
......
using System; namespace Performance.DtoModels.AppSettings
using System.Collections.Generic;
using System.Text;
namespace Performance.DtoModels.AppSettings
{ {
/// <summary> /// <summary>
/// 数据库连接字符串 /// 数据库连接字符串
......
namespace Performance.DtoModels.AppSettings
{
/// <summary>
/// 数据库密码加密
/// </summary>
public class AppSQLEncrypt
{
/// <summary>
/// 是否加密 true 加密 false 明文
/// </summary>
public bool IsEncryption { get; set; } = false;
public string TempMySqlConnectionString { get; set; } = "";
public string TempSqlServerConnectionString { get; set; } = "";
public string TempOracleConnectionString { get; set; } = "";
}
}
using System;
using System.Security.Cryptography;
using System.Text;
namespace Performance.Infrastructure.Helper
{
public static class AESHelper
{
/// <summary>
/// AES加密
/// </summary>
/// <param name="plainStr">明文字符串</param>
/// <returns>密文</returns>
public static string AESEncrypt(string encryptStr, string key = "lr1hxmoejv7czxt4mo50gfopebqkwp8l")
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = Encoding.UTF8.GetBytes(encryptStr);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="encryptStr"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AESDEncrypt(string encryptStr, string key = "lr1hxmoejv7czxt4mo50gfopebqkwp8l")
{
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = Convert.FromBase64String(encryptStr);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
}
}
using Performance.DtoModels.AppSettings;
namespace Performance.Repository
{
public static class AppSQLEncryptConfig
{
public static AppSQLEncrypt Instance { get; set; }
}
}
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client; using Oracle.ManagedDataAccess.Client;
using Performance.Infrastructure.Helper;
using System; using System;
using System.Data; using System.Data;
using System.Data.SqlClient; using System.Data.SqlClient;
...@@ -46,25 +47,35 @@ public static IDbConnection Create(DatabaseType type, string connectionString) ...@@ -46,25 +47,35 @@ public static IDbConnection Create(DatabaseType type, string connectionString)
/// <returns></returns> /// <returns></returns>
public static string GetConnectionString(DatabaseType type, string ip, string database, string uid, string pwd) public static string GetConnectionString(DatabaseType type, string ip, string database, string uid, string pwd)
{ {
string connectionString = ""; pwd = (AppSQLEncryptConfig.Instance.IsEncryption) ? AESHelper.AESDEncrypt(pwd) : pwd;
switch (type)
if (type == DatabaseType.MySql)
{ {
case DatabaseType.MySql: string connectionString = string.IsNullOrEmpty(AppSQLEncryptConfig.Instance.TempMySqlConnectionString)
connectionString = $"Server={ip};Database={database};Uid={uid};Pwd={pwd};connection timeout=12000;pooling=true;charset=utf8;Convert Zero Datetime=True;port=3306;Allow User Variables=True;"; ? "Server={0};Database={1};Uid={2};Pwd={3};connection timeout=12000;pooling=true;charset=utf8;Convert Zero Datetime=True;port=3306;Allow User Variables=True;"
break; : AppSQLEncryptConfig.Instance.TempMySqlConnectionString;
case DatabaseType.SqlServer: return string.Format(connectionString, ip, database, uid, pwd);
connectionString = $"data source={ip};initial catalog={database};user id={uid};password={pwd};connection timeout=12000;"; }
break; if (type == DatabaseType.SqlServer)
{
string connectionString = string.IsNullOrEmpty(AppSQLEncryptConfig.Instance.TempSqlServerConnectionString)
? "data source={0};initial catalog={1};user id={2};password={3};connection timeout=12000;"
: AppSQLEncryptConfig.Instance.TempSqlServerConnectionString;
case DatabaseType.Oracle: return string.Format(connectionString, ip, database, uid, pwd);
connectionString = $"Password={pwd};User ID={uid};Connection Timeout=12000;Pooling=true;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={ip})(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME={database})));"; }
break;
default: if (type == DatabaseType.Oracle)
throw new Exception($"nonsupport {DatabaseType.MySql}"); {
string connectionString = string.IsNullOrEmpty(AppSQLEncryptConfig.Instance.TempOracleConnectionString)
? "Password={3};User ID={2};Connection Timeout=12000;Pooling=true;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME={1})));"
: AppSQLEncryptConfig.Instance.TempOracleConnectionString;
return string.Format(connectionString, ip, database, uid, pwd);
} }
return connectionString;
return "";
} }
/// <summary> /// <summary>
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
using Performance.DtoModels; using Performance.DtoModels;
using Performance.EntityModels; using Performance.EntityModels;
using Performance.Infrastructure; using Performance.Infrastructure;
using Performance.Infrastructure.Helper;
using Performance.Infrastructure.Models; using Performance.Infrastructure.Models;
using Performance.Repository; using Performance.Repository;
using System; using System;
...@@ -70,6 +71,8 @@ public bool CreateHospitalConfig(sys_hospitalconfig hospitalconfig) ...@@ -70,6 +71,8 @@ public bool CreateHospitalConfig(sys_hospitalconfig hospitalconfig)
|| string.IsNullOrEmpty(hospitalconfig.DbUser) || string.IsNullOrEmpty(hospitalconfig.DbPassword)) || string.IsNullOrEmpty(hospitalconfig.DbUser) || string.IsNullOrEmpty(hospitalconfig.DbPassword))
throw new PerformanceException("配置信息不可为空"); throw new PerformanceException("配置信息不可为空");
hospitalconfig.DbPassword = (AppSQLEncryptConfig.Instance.IsEncryption) ? AESHelper.AESEncrypt(hospitalconfig.DbPassword) : hospitalconfig.DbPassword;
var databases = EnumHelper.GetItems<DatabaseType>(); var databases = EnumHelper.GetItems<DatabaseType>();
if (!databases.Select(t => t.Value).Contains(hospitalconfig.DataBaseType)) if (!databases.Select(t => t.Value).Contains(hospitalconfig.DataBaseType))
throw new PerformanceException("数据库类型错误"); throw new PerformanceException("数据库类型错误");
...@@ -107,7 +110,7 @@ public bool UpdateHospitalConfig(sys_hospitalconfig hospitalconfig) ...@@ -107,7 +110,7 @@ public bool UpdateHospitalConfig(sys_hospitalconfig hospitalconfig)
entity.DbSource = hospitalconfig.DbSource; entity.DbSource = hospitalconfig.DbSource;
entity.DbName = hospitalconfig.DbName; entity.DbName = hospitalconfig.DbName;
entity.DbUser = hospitalconfig.DbUser; entity.DbUser = hospitalconfig.DbUser;
entity.DbPassword = hospitalconfig.DbPassword; entity.DbPassword = (AppSQLEncryptConfig.Instance.IsEncryption) ? AESHelper.AESEncrypt(hospitalconfig.DbPassword) : hospitalconfig.DbPassword;
return hospitalconfigRepository.Update(entity); return hospitalconfigRepository.Update(entity);
} }
...@@ -404,9 +407,9 @@ public static List<TitleValue<int>> GetSheettypes() ...@@ -404,9 +407,9 @@ public static List<TitleValue<int>> GetSheettypes()
}; };
var list = EnumHelper.GetItems<SheetType>().Where(w => showItems.Contains(w.Value)); var list = EnumHelper.GetItems<SheetType>().Where(w => showItems.Contains(w.Value));
var data = list.Select(t => new TitleValue<int> var data = list.Select(t => new TitleValue<int>
{ {
Title = t.Description, Title = t.Description,
Value = t.Value Value = t.Value
}).ToList(); }).ToList();
data.First(w => w.Value == (int)SheetType.Employee).Title = "HRP人员"; data.First(w => w.Value == (int)SheetType.Employee).Title = "HRP人员";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment