增加了SSO,现在token也可以从query的token读入
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
|
using SimApi.Interfaces;
|
||||||
|
using static SimApi.Helpers.SimApiError;
|
||||||
|
|
||||||
namespace SimApi.Attributes;
|
namespace SimApi.Attributes;
|
||||||
|
|
||||||
@@ -37,17 +39,16 @@ public class SimApiAuthAttribute : ActionFilterAttribute
|
|||||||
public override void OnActionExecuting(ActionExecutingContext context)
|
public override void OnActionExecuting(ActionExecutingContext context)
|
||||||
{
|
{
|
||||||
var loginInfo = (SimApiLoginItem)context.HttpContext.Items["LoginInfo"]!;
|
var loginInfo = (SimApiLoginItem)context.HttpContext.Items["LoginInfo"]!;
|
||||||
//检测是否登录
|
var token = (string)context.HttpContext.Items["LoginToken"]!;
|
||||||
if (loginInfo == null)
|
ErrorWhenNull(loginInfo, 401);
|
||||||
|
var checkers = context.HttpContext.RequestServices.GetServices<ISimApiAuthChecker>();
|
||||||
|
foreach (var checker in checkers)
|
||||||
{
|
{
|
||||||
throw new SimApiException(401);
|
checker.Run(loginInfo, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Types == null) return;
|
if (Types == null) return;
|
||||||
//检测用户类型
|
//检测用户类型
|
||||||
if (!Types.Intersect(loginInfo.Type).Any())
|
ErrorWhenFalse(Types.Intersect(loginInfo.Type).Any(), 403);
|
||||||
{
|
|
||||||
throw new SimApiException(403);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,15 +10,11 @@ using static SimApiError;
|
|||||||
public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检测用户登陆的控制器
|
/// 获取已登录用户信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost, SimApiDoc("认证", "检测登陆")]
|
[HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")]
|
||||||
public string CheckLogin()
|
public SimApiLoginItem UserInfo() => LoginInfo;
|
||||||
{
|
|
||||||
ErrorWhenNull(LoginInfo, 401, "未登录");
|
|
||||||
return LoginInfo.Id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 退出登陆
|
/// 退出登陆
|
||||||
@@ -32,8 +28,4 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
|||||||
auth.Logout(value!);
|
auth.Logout(value!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")]
|
|
||||||
public SimApiLoginItem UserInfo() => LoginInfo;
|
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected SimApiLoginItem LoginInfo => (SimApiLoginItem)HttpContext.Items["LoginInfo"]!;
|
protected SimApiLoginItem LoginInfo => (SimApiLoginItem)HttpContext.Items["LoginInfo"]!;
|
||||||
|
|
||||||
protected SimApiLoginItem LoginToken => (SimApiLoginItem)HttpContext.Items["LoginToken"]!;
|
protected string LoginToken => (string)HttpContext.Items["LoginToken"]!;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 验证请求参数
|
/// 验证请求参数
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using SimApi.Attributes;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
using static SimApi.Helpers.SimApiError;
|
||||||
|
|
||||||
namespace SimApi.Controllers;
|
namespace SimApi.Controllers;
|
||||||
|
|
||||||
@@ -28,4 +30,15 @@ public class SimApiCommonController : SimApiBaseController
|
|||||||
{ "App", SimApiUtil.AppVersion }
|
{ "App", SimApiUtil.AppVersion }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检测用户登陆的控制器
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost, SimApiAuth, SimApiDoc("认证", "检测登陆")]
|
||||||
|
public string CheckLogin()
|
||||||
|
{
|
||||||
|
ErrorWhenNull(LoginInfo, 401, "未登录");
|
||||||
|
return LoginInfo.Id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.Json;
|
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
@@ -137,8 +136,9 @@ public class SimApiAuth(IDistributedCache cache, IConnectionMultiplexer redis)
|
|||||||
{
|
{
|
||||||
var setCacheKey = TokenSetCacheKey.Replace("{userId}", item.Id);
|
var setCacheKey = TokenSetCacheKey.Replace("{userId}", item.Id);
|
||||||
_redisDb.SetRemove(setCacheKey, token);
|
_redisDb.SetRemove(setCacheKey, token);
|
||||||
var cacheKey = TokenCacheKey.Replace("{token}", token);
|
|
||||||
cache.Remove(cacheKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cacheKey = TokenCacheKey.Replace("{token}", token);
|
||||||
|
cache.Remove(cacheKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using SimApi.Communications;
|
||||||
|
|
||||||
|
namespace SimApi.Interfaces;
|
||||||
|
|
||||||
|
public interface ISimApiAuthChecker
|
||||||
|
{
|
||||||
|
public void Run(SimApiLoginItem loginItem, string token);
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
@@ -11,11 +12,9 @@ public class SimApiAuthMiddleware(RequestDelegate next)
|
|||||||
{
|
{
|
||||||
public Task Invoke(HttpContext httpContext, SimApiAuth auth)
|
public Task Invoke(HttpContext httpContext, SimApiAuth auth)
|
||||||
{
|
{
|
||||||
string? token = null;
|
var token =
|
||||||
if (httpContext.Request.Headers.TryGetValue("Token", out var header))
|
httpContext.Request.Headers["Token"].FirstOrDefault()
|
||||||
{
|
?? httpContext.Request.Query["token"].FirstOrDefault();
|
||||||
token = header;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(token)) return next(httpContext);
|
if (string.IsNullOrEmpty(token)) return next(httpContext);
|
||||||
var login = auth.GetLogin(token);
|
var login = auth.GetLogin(token);
|
||||||
|
|||||||
+31
-12
@@ -18,6 +18,7 @@ using Microsoft.Extensions.Logging;
|
|||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
using SimApi.CoceSdk;
|
using SimApi.CoceSdk;
|
||||||
using SimApi.Configurations;
|
using SimApi.Configurations;
|
||||||
|
using SimApi.Interfaces;
|
||||||
using SimApi.Logger;
|
using SimApi.Logger;
|
||||||
using SimApi.SwaggerFilters;
|
using SimApi.SwaggerFilters;
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
@@ -63,6 +64,20 @@ public static class SimApiExtensions
|
|||||||
builder.AddSingleton<CoceApp>();
|
builder.AddSingleton<CoceApp>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var simApiAuthChecker = typeof(ISimApiAuthChecker);
|
||||||
|
var stackTrace = new StackTrace();
|
||||||
|
var callingMethod = stackTrace.GetFrame(stackTrace.FrameCount - 1)?.GetMethod();
|
||||||
|
var callerAssembly = callingMethod?.DeclaringType?.Assembly;
|
||||||
|
var callerTypes = callerAssembly?.GetTypes() ?? [];
|
||||||
|
|
||||||
|
foreach (var type in callerTypes)
|
||||||
|
{
|
||||||
|
if (type is { IsClass: true, IsAbstract: false } && simApiAuthChecker.IsAssignableFrom(type))
|
||||||
|
{
|
||||||
|
builder.AddScoped(simApiAuthChecker, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (simApiOptions.EnableJob)
|
if (simApiOptions.EnableJob)
|
||||||
{
|
{
|
||||||
builder.AddHangfire(x =>
|
builder.AddHangfire(x =>
|
||||||
@@ -96,12 +111,7 @@ public static class SimApiExtensions
|
|||||||
if (simApiOptions.EnableSynapse)
|
if (simApiOptions.EnableSynapse)
|
||||||
{
|
{
|
||||||
builder.AddSingleton<Synapse>();
|
builder.AddSingleton<Synapse>();
|
||||||
//自动依赖注入
|
foreach (var type in callerTypes)
|
||||||
var stackTrace = new StackTrace();
|
|
||||||
var callingMethod = stackTrace.GetFrame(stackTrace.FrameCount - 1)?.GetMethod();
|
|
||||||
var assembly = callingMethod?.DeclaringType?.Assembly;
|
|
||||||
var types = assembly?.GetTypes() ?? [];
|
|
||||||
foreach (var type in types)
|
|
||||||
{
|
{
|
||||||
var methodsWithSynapse = type.GetMethods()
|
var methodsWithSynapse = type.GetMethods()
|
||||||
.Where(m => m.GetCustomAttribute<SynapseRpcAttribute>() != null ||
|
.Where(m => m.GetCustomAttribute<SynapseRpcAttribute>() != null ||
|
||||||
@@ -394,6 +404,14 @@ public static class SimApiExtensions
|
|||||||
builder.MapControllers();
|
builder.MapControllers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var checkers = builder.Services.CreateScope().ServiceProvider.GetServices<ISimApiAuthChecker>().ToArray();
|
||||||
|
if (checkers.Length != 0)
|
||||||
|
{
|
||||||
|
var msg = checkers.Aggregate("开始配置SimApiAuthChecker...",
|
||||||
|
(current, checker) => current + $"\n|- {checker.GetType().FullName}");
|
||||||
|
logger.LogInformation(msg);
|
||||||
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiGateAuth)
|
if (options.EnableSimApiGateAuth)
|
||||||
{
|
{
|
||||||
logger.LogInformation("开始配置SimApiGateAuth...");
|
logger.LogInformation("开始配置SimApiGateAuth...");
|
||||||
@@ -408,6 +426,12 @@ public static class SimApiExtensions
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
builder.MapControllerRoute(name: "CheckLogin", pattern: "/auth/check",
|
||||||
|
defaults: new
|
||||||
|
{
|
||||||
|
controller = "SimApiCommon",
|
||||||
|
action = "CheckLogin"
|
||||||
|
});
|
||||||
if (options.EnableSimApiAuth)
|
if (options.EnableSimApiAuth)
|
||||||
{
|
{
|
||||||
logger.LogInformation("开始配置SimApiAuth...");
|
logger.LogInformation("开始配置SimApiAuth...");
|
||||||
@@ -418,12 +442,7 @@ public static class SimApiExtensions
|
|||||||
controller = "SimApiAuth",
|
controller = "SimApiAuth",
|
||||||
action = "UserInfo"
|
action = "UserInfo"
|
||||||
});
|
});
|
||||||
builder.MapControllerRoute(name: "CheckLogin", pattern: "/auth/check",
|
|
||||||
defaults: new
|
|
||||||
{
|
|
||||||
controller = "SimApiAuth",
|
|
||||||
action = "CheckLogin"
|
|
||||||
});
|
|
||||||
builder.MapControllerRoute(name: "Logout", pattern: "/auth/logout",
|
builder.MapControllerRoute(name: "Logout", pattern: "/auth/logout",
|
||||||
defaults: new
|
defaults: new
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user