Bug:OpenApi,判断重复调用的条件有误
Fix:Demo增加测试接口 Fix:生成签命的接口支持直接返回服务器的timestamp,便于调试
This commit is contained in:
parent
49fda57277
commit
9bd18a1005
@ -4,13 +4,14 @@
|
|||||||
//
|
//
|
||||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
|
||||||
namespace Admin.NET.Application;
|
namespace Admin.NET.Application;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 开放接口示例
|
/// 开放接口示例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiDescriptionSettings("开放接口", Name = "Demo", Order = 100)]
|
[ApiDescriptionSettings("开放接口", Name = "Demo", Order = 100)]
|
||||||
[Authorize(AuthenticationSchemes = SignatureAuthenticationDefaults.AuthenticationScheme)]
|
|
||||||
public class DemoOpenApi : IDynamicApiController
|
public class DemoOpenApi : IDynamicApiController
|
||||||
{
|
{
|
||||||
private readonly UserManager _userManager;
|
private readonly UserManager _userManager;
|
||||||
@ -20,9 +21,25 @@ public class DemoOpenApi : IDynamicApiController
|
|||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("helloWord")]
|
/// <summary>
|
||||||
public Task<string> HelloWord()
|
/// SignatureAuthentication
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("helloWordSignatureAuthentication")]
|
||||||
|
[Authorize(AuthenticationSchemes = SignatureAuthenticationDefaults.AuthenticationScheme)]
|
||||||
|
public Task<string> HelloWordSignatureAuthentication()
|
||||||
{
|
{
|
||||||
return Task.FromResult($"Hello, {_userManager.Account}.");
|
return Task.FromResult($"Hello 只能支持 OpenApi, {_userManager.Account}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SignatureAuthenticationJwt
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("HelloWordSignatureAuthenticationOrJwt")]
|
||||||
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)]
|
||||||
|
public Task<string> HelloWordSignatureAuthenticationOrJwt()
|
||||||
|
{
|
||||||
|
return Task.FromResult($"Hello 支持 OpenApi 或 Jwt, {_userManager.Account}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||||
|
//
|
||||||
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||||
|
//
|
||||||
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Admin.NET.Core.Service;
|
||||||
|
public class GenerateSignatureOutput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 时间戳
|
||||||
|
/// </summary>
|
||||||
|
public long Timestamp { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 签名
|
||||||
|
/// </summary>
|
||||||
|
public string Signature { get; set; }
|
||||||
|
}
|
||||||
@ -34,8 +34,12 @@ public class SysOpenAccessService : IDynamicApiController, ITransient
|
|||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[DisplayName("生成签名")]
|
[DisplayName("生成签名")]
|
||||||
public string GenerateSignature(GenerateSignatureInput input)
|
public GenerateSignatureOutput GenerateSignature(GenerateSignatureInput input)
|
||||||
{
|
{
|
||||||
|
if (input.Timestamp==0)
|
||||||
|
{
|
||||||
|
input.Timestamp = DateTimeUtil.ToUnixTimestampByMilliseconds(DateTime.Now);
|
||||||
|
}
|
||||||
// 密钥
|
// 密钥
|
||||||
var appSecretByte = Encoding.UTF8.GetBytes(input.AccessSecret);
|
var appSecretByte = Encoding.UTF8.GetBytes(input.AccessSecret);
|
||||||
|
|
||||||
@ -45,7 +49,11 @@ public class SysOpenAccessService : IDynamicApiController, ITransient
|
|||||||
using HMAC hmac = new HMACSHA256();
|
using HMAC hmac = new HMACSHA256();
|
||||||
hmac.Key = appSecretByte;
|
hmac.Key = appSecretByte;
|
||||||
var sign = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(parameter)));
|
var sign = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(parameter)));
|
||||||
return sign;
|
return new GenerateSignatureOutput
|
||||||
|
{
|
||||||
|
Timestamp = input.Timestamp,
|
||||||
|
Signature = sign
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||||
//
|
//
|
||||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||||
//
|
//
|
||||||
@ -75,7 +75,7 @@ public sealed class SignatureAuthenticationHandler : AuthenticationHandler<Signa
|
|||||||
|
|
||||||
// 重放检测
|
// 重放检测
|
||||||
var cache = App.GetRequiredService<SysCacheService>();
|
var cache = App.GetRequiredService<SysCacheService>();
|
||||||
var cacheKey = $"{CacheConst.KeyOpenAccessNonce}{accessKey}|{nonce}";
|
var cacheKey = $"{CacheConst.KeyOpenAccessNonce}{accessKey}|{timestampStr}|{nonce}";
|
||||||
if (cache.ExistKey(cacheKey))
|
if (cache.ExistKey(cacheKey))
|
||||||
return await AuthenticateResultFailAsync("重复的请求");
|
return await AuthenticateResultFailAsync("重复的请求");
|
||||||
cache.Set(cacheKey, null, Options.AllowedDateDrift * 2); // 缓存过期时间为偏差范围时间的2倍
|
cache.Set(cacheKey, null, Options.AllowedDateDrift * 2); // 缓存过期时间为偏差范围时间的2倍
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user