fix error route
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# Created by .ignore support plugin (hsz.mobi)
|
# Created by .ignore support plugin (hsz.mobi)
|
||||||
### C template
|
### C template
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
|
*.sln:
|
||||||
*.d
|
*.d
|
||||||
|
|
||||||
# Object files
|
# Object files
|
||||||
|
|||||||
+20
-7
@@ -25,6 +25,7 @@ namespace YYApi.Communications
|
|||||||
private readonly Dictionary<int, string> MsgBox = new Dictionary<int, string>()
|
private readonly Dictionary<int, string> MsgBox = new Dictionary<int, string>()
|
||||||
{
|
{
|
||||||
{200, "成功"},
|
{200, "成功"},
|
||||||
|
{204, "没有数据"},
|
||||||
{400, "参数错误"},
|
{400, "参数错误"},
|
||||||
{401, "需要登录"},
|
{401, "需要登录"},
|
||||||
{403, "无权访问"},
|
{403, "无权访问"},
|
||||||
@@ -100,7 +101,25 @@ namespace YYApi.Communications
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 动态内容
|
/// 动态内容分页
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
public class BasePageResponse<T> : BaseResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 动态内容列表
|
||||||
|
/// </summary>
|
||||||
|
public T List { get; set; }
|
||||||
|
//当前页码
|
||||||
|
public int Page { get; set; }
|
||||||
|
//每页条数
|
||||||
|
public int Count { get; set; }
|
||||||
|
//总计条数
|
||||||
|
public int Total { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 动态Data返回
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class BaseResponse<T> : BaseResponse
|
public class BaseResponse<T> : BaseResponse
|
||||||
@@ -109,11 +128,5 @@ namespace YYApi.Communications
|
|||||||
/// 动态内容列表
|
/// 动态内容列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T Data { get; set; }
|
public T Data { get; set; }
|
||||||
//当前页码
|
|
||||||
public int Page { get; set; }
|
|
||||||
//每页条数
|
|
||||||
public int Count { get; set; }
|
|
||||||
//总计条数
|
|
||||||
public int Total { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,6 +65,17 @@ namespace YYApi.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检测给定的变量是否为NUll
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="condition">检测条件</param>
|
||||||
|
/// <param name="code">错误代码</param>
|
||||||
|
/// <param name="message">错误描述</param>
|
||||||
|
protected static void ErrorWhenNull(object condition, int code, string message = null)
|
||||||
|
{
|
||||||
|
ErrorWhen(condition == null, code, message);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 错误回馈页面
|
/// 错误回馈页面
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -78,5 +89,10 @@ namespace YYApi.Controllers
|
|||||||
response.SetCode(code);
|
response.SetCode(code);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected BaseResponse<string> UploadFile()
|
||||||
|
{
|
||||||
|
return new BaseResponse<string>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-2
@@ -47,7 +47,7 @@ namespace YYApi
|
|||||||
{
|
{
|
||||||
return builder.AddSwaggerGen(x =>
|
return builder.AddSwaggerGen(x =>
|
||||||
{
|
{
|
||||||
x.SwaggerDoc("api", new OpenApiInfo {Title = title, Description = description});
|
x.SwaggerDoc("api", new OpenApiInfo { Title = title, Description = description });
|
||||||
x.EnableAnnotations();
|
x.EnableAnnotations();
|
||||||
x.AddSecurityRequirement(new OpenApiSecurityRequirement
|
x.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||||
{
|
{
|
||||||
@@ -60,10 +60,11 @@ namespace YYApi
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
x.AddSecurityDefinition("HeaderToken",
|
x.AddSecurityDefinition("HeaderToken",
|
||||||
new OpenApiSecurityScheme {Name = "Token", In = ParameterLocation.Header});
|
new OpenApiSecurityScheme { Name = "Token", In = ParameterLocation.Header });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 使用异常中间价扩展
|
/// 使用异常中间价扩展
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -65,6 +65,12 @@ namespace YYApi.Helpers
|
|||||||
Types = new[] { "user" };
|
Types = new[] { "user" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//只检测一种用户类型的快捷方式
|
||||||
|
public CheckAuthAttribute(string type)
|
||||||
|
{
|
||||||
|
Types = new[] { type };
|
||||||
|
}
|
||||||
|
|
||||||
//设定特定类型的检测
|
//设定特定类型的检测
|
||||||
public CheckAuthAttribute(string[] types)
|
public CheckAuthAttribute(string[] types)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
using System;
|
||||||
|
using Hangfire.Dashboard;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using StackExchange.Redis;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace YYApi.JobService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Redis缓存Key定义
|
||||||
|
/// </summary>
|
||||||
|
public struct CacheKey
|
||||||
|
{
|
||||||
|
public const string HangfireDashboardAuthPrefix = "{hangfire}:dashboard:auth:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// HangFire Dashboard Digest认证.
|
||||||
|
/// </summary>
|
||||||
|
public class HFDashboardAuth : IDashboardAuthorizationFilter
|
||||||
|
{
|
||||||
|
private IConfiguration _config { get; }
|
||||||
|
private IDatabase _redis { get; }
|
||||||
|
public HFDashboardAuth(IConfiguration config, IDatabase redis)
|
||||||
|
{
|
||||||
|
_redis = redis;
|
||||||
|
_config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Authorize(DashboardContext context)
|
||||||
|
{
|
||||||
|
var http = context.GetHttpContext();
|
||||||
|
if (http.Request.Headers.ContainsKey("Authorization"))
|
||||||
|
{
|
||||||
|
var authObj = _processAuthHeader(http.Request.Headers["Authorization"].ToString());
|
||||||
|
if (http.Request.QueryString.ToString().Contains("logout"))
|
||||||
|
{
|
||||||
|
_redis.KeyDelete(CacheKey.HangfireDashboardAuthPrefix + authObj["opaque"]);
|
||||||
|
_redirect(http);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (authObj["username"] == _config["Hangfire:User"])
|
||||||
|
{
|
||||||
|
var a1 = _md5(string.Format("{0}:Need Login:{1}", authObj["username"], _config["Hangfire:Pass"]));
|
||||||
|
var a2 = _md5(string.Format("{0}:{1}", http.Request.Method, authObj["uri"]));
|
||||||
|
var nonce = _redis.StringGet(CacheKey.HangfireDashboardAuthPrefix + authObj["opaque"]);
|
||||||
|
var validCode = _md5(string.Format("{0}:{1}:{2}:{3}:{4}:{5}", a1, nonce, authObj["nc"], authObj["cnonce"], authObj["qop"], a2));
|
||||||
|
if (authObj["response"] == validCode)
|
||||||
|
{
|
||||||
|
_redis.StringSet(CacheKey.HangfireDashboardAuthPrefix + authObj["opaque"], nonce, new TimeSpan(0, 5, 0));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_challenge(http);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成401认证header
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The challenge.</returns>
|
||||||
|
private void _challenge(HttpContext http)
|
||||||
|
{
|
||||||
|
var response = http.Response;
|
||||||
|
var guid = Guid.NewGuid().ToString();
|
||||||
|
var opaque = _md5(guid);
|
||||||
|
_redis.StringSet(CacheKey.HangfireDashboardAuthPrefix + opaque, guid, new TimeSpan(0, 0, 30));
|
||||||
|
response.StatusCode = 401;
|
||||||
|
response.Headers.Add("WWW-Authenticate", string.Format("Digest realm=\"Need Login\",qop=\"auth\",nonce=\"{0}\",opaque=\"{1}\"", guid, opaque));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 跳转到页面不附加参数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="http">Http.</param>
|
||||||
|
private void _redirect(HttpContext http)
|
||||||
|
{
|
||||||
|
var response = http.Response;
|
||||||
|
response.StatusCode = 302;
|
||||||
|
response.Headers.Add("Location", (http.Request.PathBase + http.Request.Path).ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 处理DigestHeader中的参数为字典
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The auth header.</returns>
|
||||||
|
/// <param name="authData">Auth data.</param>
|
||||||
|
private Dictionary<string, string> _processAuthHeader(string authData)
|
||||||
|
{
|
||||||
|
var authDataArray = authData.Replace("Digest ", string.Empty).Replace("\"", string.Empty).Split(", ");
|
||||||
|
var authDic = new Dictionary<string, string>();
|
||||||
|
foreach (var item in authDataArray)
|
||||||
|
{
|
||||||
|
var tmp = item.Split("=", 2);
|
||||||
|
authDic.Add(tmp[0], tmp[1]);
|
||||||
|
}
|
||||||
|
return authDic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 计算字符串32位MD5
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The md5.</returns>
|
||||||
|
/// <param name="source">Source.</param>
|
||||||
|
private string _md5(string source)
|
||||||
|
{
|
||||||
|
byte[] sor = Encoding.UTF8.GetBytes(source);
|
||||||
|
var md5 = MD5.Create();
|
||||||
|
byte[] result = md5.ComputeHash(sor);
|
||||||
|
StringBuilder strbul = new StringBuilder(40);
|
||||||
|
for (int i = 0; i < result.Length; i++)
|
||||||
|
{
|
||||||
|
strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
|
||||||
|
|
||||||
|
}
|
||||||
|
return strbul.ToString();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
namespace YYApi.JobService
|
||||||
|
{
|
||||||
|
public class JobDashboardConfig
|
||||||
|
{
|
||||||
|
public string Path { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
namespace YYApi.JobService
|
||||||
|
{
|
||||||
|
public class JobStorage
|
||||||
|
{
|
||||||
|
public JobStorage()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
-3
@@ -4,7 +4,7 @@
|
|||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<PackOnBuild>true</PackOnBuild>
|
<PackOnBuild>true</PackOnBuild>
|
||||||
<Version>0.1.6</Version>
|
<Version>0.2.1</Version>
|
||||||
<Authors>xRain@YYTech</Authors>
|
<Authors>xRain@YYTech</Authors>
|
||||||
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
||||||
<PackageId>YY-Tech.YYApi</PackageId>
|
<PackageId>YY-Tech.YYApi</PackageId>
|
||||||
@@ -17,10 +17,15 @@
|
|||||||
<Folder Include="Helpers\" />
|
<Folder Include="Helpers\" />
|
||||||
<Folder Include="Communications\" />
|
<Folder Include="Communications\" />
|
||||||
<Folder Include="Controllers\" />
|
<Folder Include="Controllers\" />
|
||||||
|
<Folder Include="JobService\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc5" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc5" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0" />
|
||||||
|
<PackageReference Include="HangFire.Core" Version="1.7.8" />
|
||||||
|
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.8" />
|
||||||
|
<PackageReference Include="Hangfire.Console" Version="1.4.2" />
|
||||||
|
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.8.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
|
|||||||
Reference in New Issue
Block a user