mirror of https://github.com/abpframework/abp.git
7 changed files with 102 additions and 1 deletions
@ -0,0 +1,47 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using Volo.Abp.Cli.ProjectBuilding.Building; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectBuilding.Templates |
||||
|
{ |
||||
|
public class RandomizeStringEncryptionStep: ProjectBuildPipelineStep |
||||
|
{ |
||||
|
public override void Execute(ProjectBuildContext context) |
||||
|
{ |
||||
|
var appSettings = context.Files |
||||
|
.Where(x => !x.IsDirectory && x.Name.EndsWith("appSettings.json", StringComparison.InvariantCultureIgnoreCase)) |
||||
|
.Where(x => x.Content.IndexOf("StringEncryption", StringComparison.InvariantCultureIgnoreCase) >= 0) |
||||
|
.ToList(); |
||||
|
|
||||
|
const string defaultPassPhrase = "gsKnGZ041HLL4IM8"; |
||||
|
var randomPassPhrase = GetRandomString(defaultPassPhrase.Length); |
||||
|
foreach (var appSetting in appSettings) |
||||
|
{ |
||||
|
appSetting.NormalizeLineEndings(); |
||||
|
|
||||
|
var appSettingLines = appSetting.GetLines(); |
||||
|
for (var i = 0; i < appSettingLines.Length; i++) |
||||
|
{ |
||||
|
if (appSettingLines[i].Contains(defaultPassPhrase) && appSettingLines[i].Contains(defaultPassPhrase)) |
||||
|
{ |
||||
|
appSettingLines[i] = appSettingLines[i].Replace(defaultPassPhrase, randomPassPhrase); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
appSetting.SetLines(appSettingLines); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static string GetRandomString(int length) |
||||
|
{ |
||||
|
const string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
||||
|
var builder = new StringBuilder(); |
||||
|
for (var i = 0; i < length; i++) |
||||
|
{ |
||||
|
builder.Append(letters[RandomHelper.GetRandom(0, letters.Length)]); |
||||
|
} |
||||
|
return builder.ToString(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,9 +1,45 @@ |
|||||
using Volo.Abp.Modularity; |
using System; |
||||
|
using System.Text; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.Security.Encryption; |
||||
|
|
||||
namespace Volo.Abp.Security |
namespace Volo.Abp.Security |
||||
{ |
{ |
||||
public class AbpSecurityModule : AbpModule |
public class AbpSecurityModule : AbpModule |
||||
{ |
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
var configuration = context.Services.GetConfiguration(); |
||||
|
context.Services.Configure<AbpStringEncryptionOptions>(options => |
||||
|
{ |
||||
|
var keySize = configuration["StringEncryption:KeySize"]; |
||||
|
if (!keySize.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
if (int.TryParse(keySize, out var intValue)) |
||||
|
{ |
||||
|
options.Keysize = intValue; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var defaultPassPhrase = configuration["StringEncryption:DefaultPassPhrase"]; |
||||
|
if (!defaultPassPhrase.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
options.DefaultPassPhrase = defaultPassPhrase; |
||||
|
} |
||||
|
|
||||
|
var initVectorBytes = configuration["StringEncryption:InitVectorBytes"]; |
||||
|
if (!initVectorBytes.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
options.InitVectorBytes = Encoding.ASCII.GetBytes(initVectorBytes);; |
||||
|
} |
||||
|
|
||||
|
var defaultSalt = configuration["StringEncryption:DefaultSalt"]; |
||||
|
if (!defaultSalt.IsNullOrWhiteSpace()) |
||||
|
{ |
||||
|
options.DefaultSalt = Encoding.ASCII.GetBytes(defaultSalt);; |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue