From a3556cf24651a4d476f63649facd364635e9e003 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 8 Apr 2020 14:38:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E7=BB=84=E4=BB=B6=E8=BD=AC?= =?UTF-8?q?=E5=85=A5=E7=8B=AC=E7=AB=8B=E6=89=A9=E5=B1=95=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FAuth/Extensions/Decryptor/EncryptyWorker.cs | 105 ------------------- FAuth/Extensions/ICacheProvider.cs | 27 ----- FAuth/Extensions/IJsonProvider.cs | 23 ---- FAuth/Extensions/MsJsonProvider.cs | 29 ----- FAuth/Extensions/RedisCacheProvider.cs | 45 -------- FAuth/Extensions/StringExtend.cs | 20 ---- FAuth/Extensions/SwaggerXmlEnumFilter.cs | 80 -------------- FAuth/FAuth.csproj | 9 +- FAuth/Startup.cs | 13 +-- 9 files changed, 11 insertions(+), 340 deletions(-) delete mode 100644 FAuth/Extensions/Decryptor/EncryptyWorker.cs delete mode 100644 FAuth/Extensions/ICacheProvider.cs delete mode 100644 FAuth/Extensions/IJsonProvider.cs delete mode 100644 FAuth/Extensions/MsJsonProvider.cs delete mode 100644 FAuth/Extensions/RedisCacheProvider.cs delete mode 100644 FAuth/Extensions/StringExtend.cs delete mode 100644 FAuth/Extensions/SwaggerXmlEnumFilter.cs diff --git a/FAuth/Extensions/Decryptor/EncryptyWorker.cs b/FAuth/Extensions/Decryptor/EncryptyWorker.cs deleted file mode 100644 index 56abbde..0000000 --- a/FAuth/Extensions/Decryptor/EncryptyWorker.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Security.Cryptography; -using System.Text; -using System.Threading.Tasks; - -namespace FAuth.Extensions.Decryptor -{ - /// - /// 加密帮助类 - /// - public class EncryptyWorker - { - /// - /// 补位填充方式 - /// - public PaddingMode Padding { get; set; } - /// - /// 加密算法模式 - /// - public CipherMode Cipher { get; set; } - /// - /// 初始化向量 - /// - public byte[] IV { get; set; } - /// - /// 加密字符串获取Base64密文 - /// - /// 加密key - /// 要加密的字符串 - public string Encrypty(string key,string str) { - if(key.Length < 8) { - throw new Exception("加密用的key长度为8位"); - } - byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8)); - byte[] bStr = Encoding.UTF8.GetBytes(str); - var desc = createProvider(); - using(var mStream = new MemoryStream()) { - using(var cStream = new CryptoStream(mStream,desc.CreateEncryptor(bKey,IV),CryptoStreamMode.Write)) { - cStream.Write(bStr,0,bStr.Length); - cStream.FlushFinalBlock(); - return Convert.ToBase64String(mStream.ToArray()); - } - } - } - /// - /// 从Base64密文解密,获取字符串 - /// - /// 加密键 - /// 密文 - public string DesCrypty(string key,string str) { - if(key.Length < 8) { - throw new Exception("加密用的key长度为8位"); - } - var bStr = Convert.FromBase64String(str); - byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8)); - var desc = createProvider(); - using(var mStream = new MemoryStream()) { - using(var cStream = new CryptoStream(mStream,desc.CreateDecryptor(bKey,IV),CryptoStreamMode.Write)) { - cStream.Write(bStr,0,bStr.Length); - cStream.FlushFinalBlock(); - return Encoding.UTF8.GetString(mStream.ToArray()); - } - } - } - - private DESCryptoServiceProvider createProvider() { - var desc = new DESCryptoServiceProvider(); - desc.Padding = this.Padding; - desc.Mode = this.Cipher; - return desc; - } - - private static EncryptyWorker Deshelper { get; set; } - - private EncryptyWorker() { } - - /// - /// 重用模式获取默认加密帮助器 - /// - public static EncryptyWorker GetHelper() { - if(Deshelper == null) { - byte[] IV = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 }; - Deshelper = GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV); - - } - return Deshelper; - } - /// - /// 生成新的加密帮助器 - /// - /// 补位方式 - /// 加密算法模式 - /// 初始化向量 - public static EncryptyWorker GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV) { - return new EncryptyWorker { - Cipher = cipher, - IV = IV, - Padding = padding, - }; - } - } -} diff --git a/FAuth/Extensions/ICacheProvider.cs b/FAuth/Extensions/ICacheProvider.cs deleted file mode 100644 index d9430b5..0000000 --- a/FAuth/Extensions/ICacheProvider.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FAuth.Extensions -{ - /// - /// 数据缓冲提供器接口 - /// - public interface ICacheProvider - { - /// - /// 设置缓存数据 - /// - /// 数据类型 - /// 数据键 - /// 数据对象 - /// 缓存时间或者从CacheTimeSpan选择值 - void SetCache(string key,T obj,TimeSpan span) where T : class; - - /// - /// 从缓存中获取数据,如果未缓存返回null - /// - /// 数据类型 - /// 缓存数据的键 - /// 数据对象 - T GetObj(string key) where T : class, new(); - } -} diff --git a/FAuth/Extensions/IJsonProvider.cs b/FAuth/Extensions/IJsonProvider.cs deleted file mode 100644 index d07be3d..0000000 --- a/FAuth/Extensions/IJsonProvider.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace FAuth.Extensions -{ - /// - /// Json序列化接口 - /// - public interface IJsonProvider - { - /// - /// 获取Json字符串 - /// - /// 对象类型 - /// 要序列化的对象 - /// Json串 - string GetJson(T obj); - /// - /// 获取对象 - /// - /// 对象类型 - /// Json串 - /// 对象 - T GetObj(string json); - } -} diff --git a/FAuth/Extensions/MsJsonProvider.cs b/FAuth/Extensions/MsJsonProvider.cs deleted file mode 100644 index 2fea963..0000000 --- a/FAuth/Extensions/MsJsonProvider.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Text.Json; - -namespace FAuth.Extensions -{ - /// - /// 微软提供的Json序列化器封装 - /// - public class MsJsonProvider:IJsonProvider - { - /// - /// 从对象序列化字符串 - /// - /// 对象类型 - /// 要序列化的对象 - /// 字符串 - public string GetJson(T obj) { - return JsonSerializer.Serialize(obj); - } - /// - /// 从字符串反序列化对象 - /// - /// 对象的类型 - /// json字符串 - /// 对象实例 - public T GetObj(string json) { - return JsonSerializer.Deserialize(json); - } - } -} diff --git a/FAuth/Extensions/RedisCacheProvider.cs b/FAuth/Extensions/RedisCacheProvider.cs deleted file mode 100644 index 053881f..0000000 --- a/FAuth/Extensions/RedisCacheProvider.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using Microsoft.Extensions.Caching.Distributed; - -namespace FAuth.Extensions -{ - /// - /// 使用微软Redis扩展方法组件实现Redis缓冲 - /// - public class RedisCacheProvider:ICacheProvider - { - /// - /// 基础缓冲组件 - /// - public IDistributedCache Cache { get; set; } - /// - /// 基础序列化组件 - /// - public IJsonProvider Json { get; set; } - - public RedisCacheProvider(IJsonProvider jsonProvider,IDistributedCache distributedCache = null) { - this.Cache = distributedCache; - this.Json = jsonProvider; - } - - public T GetObj(string key) where T : class, new() { - if(this.Cache == null) { - return null; - } - var str = this.Cache.GetString(key); - if(str.IsNullOrEmpty()) { - return null; - } - return this.Json.GetObj(str); - } - - public void SetCache(string key,T obj,TimeSpan span) where T : class { - if(this.Cache == null || obj == null || span == null || span == TimeSpan.Zero) { - return; - } - this.Cache.SetString(key,this.Json.GetJson(obj),new DistributedCacheEntryOptions { - AbsoluteExpirationRelativeToNow = span, - }); - } - } -} diff --git a/FAuth/Extensions/StringExtend.cs b/FAuth/Extensions/StringExtend.cs deleted file mode 100644 index a549bdc..0000000 --- a/FAuth/Extensions/StringExtend.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace FAuth.Extensions -{ - public static class StringExtend - { - /// - /// 扩展判断字符串是否为null或空方法 - /// - /// 字符串 - /// 如果为Null或Empty或空返回True,否则返回false - public static bool IsNullOrEmpty(this string str) { - return str == null ? true : str == string.Empty ? true : str == "" ? true : false; - } - /// - /// 判断字符串是否非空 - /// - /// 字符串 - /// 如果为Null或Empty或空返回False,否则返回True - public static bool IsNotNullOrEmpty(this string str) => !str.IsNullOrEmpty(); - } -} diff --git a/FAuth/Extensions/SwaggerXmlEnumFilter.cs b/FAuth/Extensions/SwaggerXmlEnumFilter.cs deleted file mode 100644 index 661313e..0000000 --- a/FAuth/Extensions/SwaggerXmlEnumFilter.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Reflection; -using System.Xml.XPath; -using Microsoft.OpenApi.Any; -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; - -namespace FAuth.Extensions -{ - /// - /// 通过DescriptionAttribute和summary生成枚举说明 - /// - public class SwaggerXmlEnumFilter:IDocumentFilter - { - private readonly XPathNavigator _xmlNavigator; - - public static List AllTypes { get; set; } - - /// - /// 通过提供应用程序文档生成枚举说明 - /// - /// - public SwaggerXmlEnumFilter(string xmlPath) { - _xmlNavigator = new XPathDocument(xmlPath).CreateNavigator(); - - if(AllTypes == null) { - AllTypes = new List(); - foreach(var ass in AppDomain.CurrentDomain.GetAssemblies()) { - AllTypes.AddRange(ass.GetTypes().Where(m => m.IsEnum)); - } - } - } - - public void Apply(OpenApiDocument swaggerDoc,DocumentFilterContext context) { - var enumList = swaggerDoc.Components.Schemas.Where(m => m.Value.Enum != null && m.Value.Enum.Count > 0); - foreach(var item in enumList) { - var key = item.Key; - var property = item.Value; - var itemType = AllTypes.Find(m => m.Name == key); - var list = new List(); - foreach(var val in property.Enum) { - list.Add((OpenApiInteger)val); - } - property.Description += describeEnum(itemType,list); - } - } - - private string describeEnum(Type type,List enums) { - var enumDescriptions = new List(); - foreach(var item in enums) { - if(type == null) - continue; - var value = Enum.Parse(type,item.Value.ToString()); - var desc = getDescription(type,value); - - if(string.IsNullOrEmpty(desc)) - enumDescriptions.Add($"{item.Value.ToString()}:{Enum.GetName(type,value)}; "); - else - enumDescriptions.Add($"{item.Value.ToString()}:{Enum.GetName(type,value)},{desc}; "); - - } - return $"
{Environment.NewLine}{string.Join("
" + Environment.NewLine,enumDescriptions)}"; - } - - private string getDescription(Type t,object value) { - foreach(var member in t.GetMembers().Where(m => m.Name == t.GetEnumName(value))) { - foreach(var attr in member.GetCustomAttributes()) { - return attr.Description; - } - } - var fullName = $"{t.FullName}.{t.GetEnumName(value)}"; - var desc = _xmlNavigator.SelectSingleNode($"doc/members/member[@name='F:{fullName}']/summary")?.InnerXml; - return desc ?? string.Empty; - } - - } -} diff --git a/FAuth/FAuth.csproj b/FAuth/FAuth.csproj index a9c2886..7dd113e 100644 --- a/FAuth/FAuth.csproj +++ b/FAuth/FAuth.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -6,11 +6,12 @@ + - + @@ -19,4 +20,8 @@ + + + + diff --git a/FAuth/Startup.cs b/FAuth/Startup.cs index 517da2b..3782d27 100644 --- a/FAuth/Startup.cs +++ b/FAuth/Startup.cs @@ -2,11 +2,10 @@ using System; using System.IO; using System.Text.Encodings.Web; using System.Text.Unicode; +using Falcon.Extend; using FAuth.DataBase; -using FAuth.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.Caching.Redis; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -32,16 +31,12 @@ namespace FAuth /// public void ConfigureServices(IServiceCollection services) { //עJsonл - services.AddSingleton(); + services.AddMsJsonProvider(); //עݿ services.AddDbContext(); //עRedis var rop = this.Configuration.GetSection("Redis").Get(); - services.AddDistributedRedisCache(op => { - op.InstanceName = rop.InstanceName; - op.Configuration = rop.Configuration; - }); - services.AddTransient(); + services.AddRedis(rop); //עMVC services.AddControllersWithViews() .AddJsonOptions(option => { @@ -62,7 +57,7 @@ namespace FAuth }); var xmlPath = Path.Combine(AppContext.BaseDirectory,typeof(Program).Assembly.GetName().Name + ".xml"); c.IncludeXmlComments(xmlPath,true); - c.DocumentFilter(xmlPath); + c.AddXmlEnumEnable(xmlPath); }); }