Compare commits

...
2 Commits
Author SHA1 Message Date
wanghu 5972f3b7e1 debug
build / build-push (push) Successful in 27s
2026-09-07 08:19:37 +08:00
wanghu 8abba50e0e 添加deugUser 2026-09-05 16:17:42 +08:00
4 changed files with 68 additions and 2 deletions
@@ -44,7 +44,7 @@ namespace YhCore.WebApi.Auth
var auth = configuration.GetSection(YhAuthOptions.SectionName).Get<YhAuthOptions>() ?? new YhAuthOptions(); var auth = configuration.GetSection(YhAuthOptions.SectionName).Get<YhAuthOptions>() ?? new YhAuthOptions();
options.Cookie.Name = auth.CookieName; options.Cookie.Name = auth.CookieName;
options.Cookie.HttpOnly = true; options.Cookie.HttpOnly = true;
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax; options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
// 会话票据:TicketDataFormat 把票据存服务端,Cookie 只写 32 位 key; // 会话票据:TicketDataFormat 把票据存服务端,Cookie 只写 32 位 key;
// IsPersistent=false(CasAuthenticationHandler)会话 Cookie,关闭浏览器即失效; // IsPersistent=false(CasAuthenticationHandler)会话 Cookie,关闭浏览器即失效;
// SlidingExpiration=false 固定过期,不滑动续期。 // SlidingExpiration=false 固定过期,不滑动续期。
@@ -0,0 +1,61 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace YhCore.WebApi.Middleware
{
/// <summary>
/// DebugUser 模拟登录中间件(对齐老 BestSchool/Java 系统的 DebugUser 调试机制;
/// 前端 api/index.js 每次请求固定携带 DebugAppType=true 与 DebugUser=&lt;loginName&gt; 头)。
///
/// 用途:解决「本地前端(localhost:8103) 直连部署后端」的登录态跨域问题——
/// Cookie 按 host 隔离且 SameSite=Lax 不随跨站 XHR 携带,CAS 在线上域名种下的登录态
/// 永远无法回流 localhost 页面的请求;本中间件以请求头承载身份,绕开该物理限制。
///
/// 生效条件(全部满足):
/// 1. 配置开关 yh:auth:DebugUserEnabled = true(默认 false,生产环境勿开——伪造该头即可冒充任意用户);
/// 2. 当前请求未认证(正常 CAS 登录态优先,互不干扰);
/// 3. DebugAppType=true 且 DebugUser 非空。
///
/// 动作:按 DebugUser 值构造 loginName 身份(Claims:Name/NameIdentifier/loginName,与 CAS handler 一致),
/// 写入 Context.User。后续 UseYhUserContext 据此经 IUserDetailsProvider(UserBase /oapi/user/byLoginName)
/// 解析完整 BoaUser;loginName 无对应用户时接口返回空数据,可据此发现账号填错。
///
/// 注册位置:UseAuthentication 之后、SsoProtectionMiddleware 之前。
/// </summary>
public class DebugUserMiddleware
{
private readonly RequestDelegate _next;
private readonly bool _enabled;
public DebugUserMiddleware(RequestDelegate next, IConfiguration configuration)
{
_next = next;
_enabled = configuration.GetValue("yh:auth:DebugUserEnabled", false);
}
public async Task InvokeAsync(HttpContext context)
{
if (_enabled
&& context.User?.Identity?.IsAuthenticated != true
&& string.Equals(context.Request.Headers["DebugAppType"].ToString(), "true", StringComparison.OrdinalIgnoreCase))
{
var loginName = context.Request.Headers["DebugUser"].ToString().Trim();
if (!string.IsNullOrEmpty(loginName))
{
var claims = new[]
{
new Claim(ClaimTypes.Name, loginName),
new Claim(ClaimTypes.NameIdentifier, loginName),
new Claim("loginName", loginName),
};
context.User = new ClaimsPrincipal(new ClaimsIdentity(claims, "DebugUser"));
}
}
await _next(context);
}
}
}
+4
View File
@@ -230,6 +230,10 @@ namespace YhCore.WebApi
app.UseMiddleware<ApiAccessLogMiddleware>(); // 0. 接口访问日志(唯一日志出口) app.UseMiddleware<ApiAccessLogMiddleware>(); // 0. 接口访问日志(唯一日志出口)
app.UseMiddleware<GlobalExceptionMiddleware>(); // 1. 异常兜底(异常摘要并入接口日志) app.UseMiddleware<GlobalExceptionMiddleware>(); // 1. 异常兜底(异常摘要并入接口日志)
app.UseAuthentication(); // 2. CAS ticket 校验 + Cookie 恢复(Yh.Platform.Auth) app.UseAuthentication(); // 2. CAS ticket 校验 + Cookie 恢复(Yh.Platform.Auth)
// 2.5 DebugUser 模拟登录(开关 yh:auth:DebugUserEnabled,默认关):本地前端直连部署后端的
// 跨域联调身份方案——Cookie 按 host 隔离导致线上登录态无法回流 localhost 页面请求,
// 以 DebugUser 请求头承载身份绕开。必须位于认证之后、SSO 保护之前。
app.UseMiddleware<DebugUserMiddleware>();
app.UseMiddleware<SsoProtectionMiddleware>(); // 3. SSO 路径保护(未认证 → 302 CAS;认证通过后才有用户身份) app.UseMiddleware<SsoProtectionMiddleware>(); // 3. SSO 路径保护(未认证 → 302 CAS;认证通过后才有用户身份)
app.UseYhUserContext(); // 4. 新框架用户上下文:BoaUserContext + UserContextHolder(LoginUser) app.UseYhUserContext(); // 4. 新框架用户上下文:BoaUserContext + UserContextHolder(LoginUser)
// 5. 应用间签名(oapi 渠道)。必须排在 UseYhUserContext 之后: // 5. 应用间签名(oapi 渠道)。必须排在 UseYhUserContext 之后:
+2 -1
View File
@@ -14,7 +14,8 @@
"auth": { "auth": {
"Enabled": true, "Enabled": true,
"CookieExpireMinutes": 120, "CookieExpireMinutes": 120,
"cookieName": ".yh.auth" "cookieName": ".yh.auth",
"DebugUserEnabled": true
}, },
"sso": { "sso": {
"Enabled": true, "Enabled": true,