部分组件转入独立扩展包
This commit is contained in:
parent
c820d37521
commit
a3556cf246
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 加密帮助类
|
||||
/// </summary>
|
||||
public class EncryptyWorker
|
||||
{
|
||||
/// <summary>
|
||||
/// 补位填充方式
|
||||
/// </summary>
|
||||
public PaddingMode Padding { get; set; }
|
||||
/// <summary>
|
||||
/// 加密算法模式
|
||||
/// </summary>
|
||||
public CipherMode Cipher { get; set; }
|
||||
/// <summary>
|
||||
/// 初始化向量
|
||||
/// </summary>
|
||||
public byte[] IV { get; set; }
|
||||
/// <summary>
|
||||
/// 加密字符串获取Base64密文
|
||||
/// </summary>
|
||||
/// <param name="key">加密key</param>
|
||||
/// <param name="str">要加密的字符串</param>
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 从Base64密文解密,获取字符串
|
||||
/// </summary>
|
||||
/// <param name="key">加密键</param>
|
||||
/// <param name="str">密文</param>
|
||||
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() { }
|
||||
|
||||
/// <summary>
|
||||
/// 重用模式获取默认加密帮助器
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// 生成新的加密帮助器
|
||||
/// </summary>
|
||||
/// <param name="padding">补位方式</param>
|
||||
/// <param name="cipher">加密算法模式</param>
|
||||
/// <param name="IV">初始化向量</param>
|
||||
public static EncryptyWorker GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV) {
|
||||
return new EncryptyWorker {
|
||||
Cipher = cipher,
|
||||
IV = IV,
|
||||
Padding = padding,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace FAuth.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据缓冲提供器接口
|
||||
/// </summary>
|
||||
public interface ICacheProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置缓存数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T">数据类型</typeparam>
|
||||
/// <param name="key">数据键</param>
|
||||
/// <param name="obj">数据对象</param>
|
||||
/// <param name="span">缓存时间或者从CacheTimeSpan选择值</param>
|
||||
void SetCache<T>(string key,T obj,TimeSpan span) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 从缓存中获取数据,如果未缓存返回null
|
||||
/// </summary>
|
||||
/// <typeparam name="T">数据类型</typeparam>
|
||||
/// <param name="key">缓存数据的键</param>
|
||||
/// <returns>数据对象</returns>
|
||||
T GetObj<T>(string key) where T : class, new();
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
namespace FAuth.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Json序列化接口
|
||||
/// </summary>
|
||||
public interface IJsonProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取Json字符串
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象类型</typeparam>
|
||||
/// <param name="obj">要序列化的对象</param>
|
||||
/// <returns>Json串</returns>
|
||||
string GetJson<T>(T obj);
|
||||
/// <summary>
|
||||
/// 获取对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象类型</typeparam>
|
||||
/// <param name="json">Json串</param>
|
||||
/// <returns>对象</returns>
|
||||
T GetObj<T>(string json);
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
using System.Text.Json;
|
||||
|
||||
namespace FAuth.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 微软提供的Json序列化器封装
|
||||
/// </summary>
|
||||
public class MsJsonProvider:IJsonProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 从对象序列化字符串
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象类型</typeparam>
|
||||
/// <param name="obj">要序列化的对象</param>
|
||||
/// <returns>字符串</returns>
|
||||
public string GetJson<T>(T obj) {
|
||||
return JsonSerializer.Serialize<T>(obj);
|
||||
}
|
||||
/// <summary>
|
||||
/// 从字符串反序列化对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">对象的类型</typeparam>
|
||||
/// <param name="json">json字符串</param>
|
||||
/// <returns>对象实例</returns>
|
||||
public T GetObj<T>(string json) {
|
||||
return JsonSerializer.Deserialize<T>(json);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
|
||||
namespace FAuth.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用微软Redis扩展方法组件实现Redis缓冲
|
||||
/// </summary>
|
||||
public class RedisCacheProvider:ICacheProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 基础缓冲组件
|
||||
/// </summary>
|
||||
public IDistributedCache Cache { get; set; }
|
||||
/// <summary>
|
||||
/// 基础序列化组件
|
||||
/// </summary>
|
||||
public IJsonProvider Json { get; set; }
|
||||
|
||||
public RedisCacheProvider(IJsonProvider jsonProvider,IDistributedCache distributedCache = null) {
|
||||
this.Cache = distributedCache;
|
||||
this.Json = jsonProvider;
|
||||
}
|
||||
|
||||
public T GetObj<T>(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<T>(str);
|
||||
}
|
||||
|
||||
public void SetCache<T>(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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
namespace FAuth.Extensions
|
||||
{
|
||||
public static class StringExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 扩展判断字符串是否为null或空方法
|
||||
/// </summary>
|
||||
/// <param name="str">字符串</param>
|
||||
/// <returns>如果为Null或Empty或空返回True,否则返回false</returns>
|
||||
public static bool IsNullOrEmpty(this string str) {
|
||||
return str == null ? true : str == string.Empty ? true : str == "" ? true : false;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断字符串是否非空
|
||||
/// </summary>
|
||||
/// <param name="str">字符串</param>
|
||||
/// <returns>如果为Null或Empty或空返回False,否则返回True</returns>
|
||||
public static bool IsNotNullOrEmpty(this string str) => !str.IsNullOrEmpty();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过DescriptionAttribute和summary生成枚举说明
|
||||
/// </summary>
|
||||
public class SwaggerXmlEnumFilter:IDocumentFilter
|
||||
{
|
||||
private readonly XPathNavigator _xmlNavigator;
|
||||
|
||||
public static List<Type> AllTypes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 通过提供应用程序文档生成枚举说明
|
||||
/// </summary>
|
||||
/// <param name="xmlPath"></param>
|
||||
public SwaggerXmlEnumFilter(string xmlPath) {
|
||||
_xmlNavigator = new XPathDocument(xmlPath).CreateNavigator();
|
||||
|
||||
if(AllTypes == null) {
|
||||
AllTypes = new List<Type>();
|
||||
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<OpenApiInteger>();
|
||||
foreach(var val in property.Enum) {
|
||||
list.Add((OpenApiInteger)val);
|
||||
}
|
||||
property.Description += describeEnum(itemType,list);
|
||||
}
|
||||
}
|
||||
|
||||
private string describeEnum(Type type,List<OpenApiInteger> enums) {
|
||||
var enumDescriptions = new List<string>();
|
||||
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 $"<br/>{Environment.NewLine}{string.Join("<br/>" + 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<DescriptionAttribute>()) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
@ -6,11 +6,12 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Falcon.Extend" Version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
|
||||
<PackageReference Include="NLog" Version="4.7.0" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.2.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.3.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
|
||||
</ItemGroup>
|
||||
|
@ -19,4 +20,8 @@
|
|||
<None Include="..\.editorconfig" Link=".editorconfig" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Extensions\Decryptor\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -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
|
|||
/// <param name="services">服务容器</param>
|
||||
public void ConfigureServices(IServiceCollection services) {
|
||||
//注册Json序列化
|
||||
services.AddSingleton<IJsonProvider,MsJsonProvider>();
|
||||
services.AddMsJsonProvider();
|
||||
//注册数据库
|
||||
services.AddDbContext<FAuthDb>();
|
||||
//注册Redis
|
||||
var rop = this.Configuration.GetSection("Redis").Get<RedisCacheOptions>();
|
||||
services.AddDistributedRedisCache(op => {
|
||||
op.InstanceName = rop.InstanceName;
|
||||
op.Configuration = rop.Configuration;
|
||||
});
|
||||
services.AddTransient<ICacheProvider,RedisCacheProvider>();
|
||||
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<SwaggerXmlEnumFilter>(xmlPath);
|
||||
c.AddXmlEnumEnable(xmlPath);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user