NewLife/Stardust

新增TraceAnonymous属性以允许未登录用户访问调用链页面;更新TraceController以支持匿名访问控制

Co-authored-by: Copilot <copilot@github.com>
大石头 authored at 2026-05-08 15:39:29
8c2b2b2
Tree
1 Parent(s) 9d6e73b
Summary: 2 changed files with 19 additions and 1 deletions.
Modified +4 -0
Modified +15 -1
Modified +4 -0
diff --git a/Stardust.Server/Setting.cs b/Stardust.Server/Setting.cs
index 02dc635..3ee69e2 100644
--- a/Stardust.Server/Setting.cs
+++ b/Stardust.Server/Setting.cs
@@ -138,6 +138,10 @@ public class StarServerSetting : Config<StarServerSetting>, ITokenSetting
     [Description("固定城市。默认自动根据IP计算所在城市,开启后不再自动计算,改为人工设置")]
     public Boolean FixedCity { get; set; }
 
+    /// <summary>调用链允许匿名。允许未登录用户访问/trace调用链页面,监控数据可能含敏感信息,默认false</summary>
+    [Description("调用链允许匿名。允许未登录用户访问/trace调用链页面,监控数据可能含敏感信息,默认false")]
+    public Boolean TraceAnonymous { get; set; } = false;
+
     ///// <summary>新服务器。节点自动迁移到新的服务器地址</summary>
     //[Description("新服务器。节点自动迁移到新的服务器地址")]
     //public String NewServer { get; set; }
Modified +15 -1
diff --git a/Stardust.Web/Controllers/TraceController.cs b/Stardust.Web/Controllers/TraceController.cs
index 5001dfa..95a5ef0 100644
--- a/Stardust.Web/Controllers/TraceController.cs
+++ b/Stardust.Web/Controllers/TraceController.cs
@@ -1,18 +1,29 @@
-using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
 using NewLife;
 using NewLife.Cube;
 using NewLife.Web;
 using Stardust.Data.Monitors;
+using Stardust.Server;
 using Stardust.Web.Models;
 using XCode.Membership;
 
 namespace Stardust.Web.Controllers;
 
+[AllowAnonymous]
 public class TraceController : ControllerBaseX
 {
+    private readonly StarServerSetting _setting;
+
+    /// <summary>实例化</summary>
+    public TraceController(StarServerSetting setting) => _setting = setting;
+
     [Route("[controller]")]
     public ActionResult Index(String id, Pager pager)
     {
+        if (!_setting.TraceAnonymous && ManageProvider.User == null)
+            return Redirect($"/Admin/User/Login?r={Uri.EscapeDataString(Request.Path + Request.QueryString)}");
+
         if (id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(id));
 
         // id可能不是traceId,而是traceParent
@@ -46,6 +57,9 @@ public class TraceController : ControllerBaseX
     [Route("[action]")]
     public ActionResult Graph(String id, Pager pager)
     {
+        if (!_setting.TraceAnonymous && ManageProvider.User == null)
+            return Redirect($"/Admin/User/Login?r={Uri.EscapeDataString(Request.Path + Request.QueryString)}");
+
         if (id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(id));
 
         // id可能不是traceId,而是traceParent