Initial_commit_SecMPS_v2
This commit is contained in:
31
api_sqlsugar/VolPro.Core/Middleware/ActionLog.cs
Normal file
31
api_sqlsugar/VolPro.Core/Middleware/ActionLog.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VolPro.Core.Middleware
|
||||
{
|
||||
public class ActionLog : Attribute
|
||||
{
|
||||
public string LogType { get; set; }
|
||||
/// <summary>
|
||||
/// 是否写入日志
|
||||
/// </summary>
|
||||
public bool Write { get; set; }
|
||||
public ActionLog() : this(true)
|
||||
{
|
||||
|
||||
}
|
||||
public ActionLog(bool write = true)
|
||||
{
|
||||
Write = write;
|
||||
}
|
||||
public ActionLog(string logType)
|
||||
{
|
||||
LogType = logType;
|
||||
Write = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VolPro.Core.Configuration;
|
||||
using VolPro.Core.Const;
|
||||
using VolPro.Core.EFDbContext;
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.Extensions;
|
||||
using VolPro.Core.ManageUser;
|
||||
using VolPro.Core.Services;
|
||||
|
||||
namespace VolPro.Core.Middleware
|
||||
{
|
||||
public class ExceptionHandlerMiddleWare
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
public ExceptionHandlerMiddleWare(RequestDelegate next)
|
||||
{
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
context.Request.EnableBuffering();
|
||||
(context.RequestServices.GetService(typeof(ActionObserver)) as ActionObserver).RequestDate = DateTime.Now;
|
||||
//文件授权
|
||||
if (AppSetting.FileAuth
|
||||
&& context.Request.Path.StartsWithSegments("/upload", StringComparison.OrdinalIgnoreCase)
|
||||
&& !context.Request.Path.StartsWithSegments("/upload/tables/sys_user", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string key = context.Request.Query["access_token"];
|
||||
bool authResult = false;
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
//先从缓存读怪
|
||||
string value = context.GetService<Core.CacheManager.ICacheService>().Get(key);
|
||||
if (!string.IsNullOrEmpty(value) && value.GetDateTime() > DateTime.Now)
|
||||
{
|
||||
authResult = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{ //缓存丢失直接从解密
|
||||
var dt = key.DecryptDES(AppSetting.Secret.User).Split("_")[1].GetDateTime();
|
||||
authResult = dt > DateTime.Now;
|
||||
if (authResult)
|
||||
{
|
||||
context.GetService<Core.CacheManager.ICacheService>().Add(key, dt.ToString("yyyy-MM-dd HH:mm"));
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
if (!authResult)
|
||||
{
|
||||
context.Response.StatusCode = 401;
|
||||
context.Response.ContentType = ApplicationContentType.JSON;
|
||||
await context.Response.WriteAsync(new { message = "Unauthorized", status = false }.Serialize(), Encoding.UTF8);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
await next(context);
|
||||
|
||||
//app.UseMiddleware<ExceptionHandlerMiddleWare>()放在 app.UseRouting()后才可以在await next(context);前执行
|
||||
Endpoint endpoint = context.Features.Get<IEndpointFeature>()?.Endpoint;
|
||||
if (endpoint != null && endpoint is RouteEndpoint routeEndpoint)
|
||||
{
|
||||
ActionLog log = endpoint.Metadata.GetMetadata<ActionLog>();
|
||||
if (log != null && log.Write)
|
||||
{
|
||||
Logger.Add(log?.LogType, null, null, null, status: LoggerStatus.Info);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Info(LoggerType.Info);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var env = context.RequestServices.GetService(typeof(IWebHostEnvironment)) as IWebHostEnvironment;
|
||||
string message = exception.Message + exception.InnerException;
|
||||
Logger.Error(LoggerType.Exception, message);
|
||||
if (!env.IsDevelopment())
|
||||
{
|
||||
message = "服务器处理异常".Translator();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"服务器处理出现异常:{message}");
|
||||
}
|
||||
context.Response.StatusCode = 500;
|
||||
context.Response.ContentType = ApplicationContentType.JSON;
|
||||
await context.Response.WriteAsync(new { message, status = false }.Serialize(), Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
api_sqlsugar/VolPro.Core/Middleware/HttpRequestMiddleware.cs
Normal file
27
api_sqlsugar/VolPro.Core/Middleware/HttpRequestMiddleware.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
|
||||
namespace VolPro.Core.Middleware
|
||||
{
|
||||
public class HttpRequestMiddleware
|
||||
{
|
||||
public static Func<RequestDelegate, RequestDelegate> Context
|
||||
{
|
||||
get
|
||||
{
|
||||
return next => async context =>
|
||||
{
|
||||
//动态标识刷新token(2021.05.01)
|
||||
context.Response.Headers["Access-Control-Expose-Headers"] = "vol_exp";
|
||||
await next(context);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
32
api_sqlsugar/VolPro.Core/Middleware/LanguageMiddleWare.cs
Normal file
32
api_sqlsugar/VolPro.Core/Middleware/LanguageMiddleWare.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using VolPro.Core.Language;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VolPro.Core.Middleware
|
||||
{
|
||||
public class LanguageMiddleWare
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
public LanguageMiddleWare(RequestDelegate next)
|
||||
{
|
||||
this.next = next;
|
||||
}
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
if (context.Request.Path.Value.StartsWith("/lang"))
|
||||
{
|
||||
context.Response.Headers.TryAdd("Access-Control-Allow-Origin", "*");
|
||||
context.Response.ContentType = "application/json";
|
||||
//application/json
|
||||
}
|
||||
if (!context.Request.Headers.ContainsKey("lang"))
|
||||
{
|
||||
context.Request.Headers["lang"] = LangConst.简体中文;
|
||||
}
|
||||
await next(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user