110 lines
4.1 KiB
110 lines
4.1 KiB
3 months ago
|
using medical.insu.transfomer.Attributes;
|
||
|
using ReZero.SuperAPI;
|
||
|
using System;
|
||
|
using System.Linq;
|
||
|
using System.Reflection;
|
||
|
|
||
|
namespace medical.insu.transfomer
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 定义不需要登录验证的API路径
|
||
|
/// </summary>
|
||
|
public static class NoAuthPaths
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 医保交易接口
|
||
|
/// </summary>
|
||
|
public const string MedicalInsuranceExecuteTransaction = "/api/200100/medicalinsurancecontroller/executetransaction";
|
||
|
|
||
|
/// <summary>
|
||
|
/// 检查路径是否在免验证列表中
|
||
|
/// </summary>
|
||
|
/// <param name="path">请求路径</param>
|
||
|
/// <returns>true表示不需要验证</returns>
|
||
|
public static bool IsNoAuthPath(string path)
|
||
|
{
|
||
|
path = path.ToLower();
|
||
|
|
||
|
// 检查是否匹配医保交易接口
|
||
|
if (path.Contains(MedicalInsuranceExecuteTransaction))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// 如果需要添加更多免验证路径,可以在这里扩展
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 通过特性检查是否需要验证
|
||
|
/// </summary>
|
||
|
/// <param name="context">接口上下文</param>
|
||
|
/// <returns>true表示不需要验证</returns>
|
||
|
public static bool IsSkipAuthByAttribute(InterfaceContext context)
|
||
|
{
|
||
|
// 先检查路径
|
||
|
if (IsNoAuthPath(context.HttpContext.Request.Path))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
try
|
||
|
{
|
||
|
// 检查具体接口信息
|
||
|
if (context.InterfaceInfo?.DataModel?.MyMethodInfo != null)
|
||
|
{
|
||
|
var methodInfo = context.InterfaceInfo.DataModel.MyMethodInfo;
|
||
|
var classFullName = methodInfo.MethodClassFullName;
|
||
|
var methodName = methodInfo.MethodName;
|
||
|
|
||
|
if (!string.IsNullOrEmpty(classFullName) && !string.IsNullOrEmpty(methodName))
|
||
|
{
|
||
|
// 获取所有程序集
|
||
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||
|
|
||
|
foreach (var assembly in assemblies)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
// 查找类型
|
||
|
var type = assembly.GetType(classFullName);
|
||
|
if (type != null)
|
||
|
{
|
||
|
// 检查类是否有SkipAuthAttribute
|
||
|
if (type.GetCustomAttributes(typeof(SkipAuthAttribute), true).Any())
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// 查找方法
|
||
|
var method = type.GetMethod(methodName);
|
||
|
if (method != null)
|
||
|
{
|
||
|
// 检查方法是否有SkipAuthAttribute
|
||
|
if (method.GetCustomAttributes(typeof(SkipAuthAttribute), true).Any())
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
// 忽略查找过程中的异常
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
// 捕获任何异常,防止验证过程崩溃
|
||
|
Console.WriteLine($"检查免验证特性时发生异常: {ex.Message}");
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|