From eb0479552c9904d9b9f4fdfb67012e2a2090bcae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=B0=8F=E8=82=A5=E7=BE=8A?= <1048382248@qq.com>
Date: Thu, 14 Aug 2025 18:25:05 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=20=E7=94=A8=E6=88=B7?=
=?UTF-8?q?=E4=B8=AD=E5=BF=83=E7=9A=84=E9=89=B4=E6=9D=83Token=20=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=20=E7=8F=AD=E7=BA=A7=20=E5=AD=A6=E6=A0=A1=E6=8E=A5?=
=?UTF-8?q?=E5=8F=A3=20[=E4=BD=BF=E7=94=A8=E7=94=A8=E6=88=B7=E4=B8=AD?=
=?UTF-8?q?=E5=BF=83=E6=8E=A5=E5=8F=A3]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/AdminController.cs | 1 +
.../Controllers/ClassController.cs | 35 -------------------
.../Controllers/SchoolController.cs | 26 --------------
.../Controllers/_BaseController.cs | 7 ++--
Learn.Archives.API/Expand/AuthorizeExpand.cs | 10 +++---
Learn.Archives.API/Program.cs | 3 +-
Learn.Archives.API/appsettings.json | 2 +-
Learn.Archives.Core/Common/AppConfig.cs | 16 ++++++++-
.../Common/AuthenticationSchemes.cs | 2 +-
Learn.Archives.Core/Common/ClaimEnum.cs | 2 +-
.../Learn.Archives.Core.csproj | 1 +
.../Model/Dto/QueryRequestBase.cs | 6 ++--
Learn.Archives.Core/Model/Grade.cs | 6 ++++
13 files changed, 39 insertions(+), 78 deletions(-)
delete mode 100644 Learn.Archives.API/Controllers/ClassController.cs
delete mode 100644 Learn.Archives.API/Controllers/SchoolController.cs
diff --git a/Learn.Archives.API/Controllers/AdminController.cs b/Learn.Archives.API/Controllers/AdminController.cs
index 3dbdccd..b3d5740 100644
--- a/Learn.Archives.API/Controllers/AdminController.cs
+++ b/Learn.Archives.API/Controllers/AdminController.cs
@@ -66,6 +66,7 @@ namespace Learn.Archives.API.Controllers
AccessToken = JwtHelper.GetToken(AppCommon.Config.AuthKey,
[
new Claim(ClaimEnum.Role,admin.RoleId.ToString()),
+ new Claim(ClaimEnum.UserId,admin.RoleId.ToString()),
new Claim(ClaimEnum.Id, admin.Id.ToString()),
new Claim(ClaimEnum.Name, admin.Name),
])
diff --git a/Learn.Archives.API/Controllers/ClassController.cs b/Learn.Archives.API/Controllers/ClassController.cs
deleted file mode 100644
index ea43f1d..0000000
--- a/Learn.Archives.API/Controllers/ClassController.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-using Learn.Archives.API.Controllers.Dto;
-using Learn.Archives.API.Expand;
-using Learn.Archives.Core.Common;
-using Learn.Archives.Core.Model;
-using Learn.Archives.Core.Model.Dto;
-using Mapster;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.RazorPages;
-using System.Diagnostics;
-using System.Security.Claims;
-using UserCenter.Model;
-using static System.Runtime.InteropServices.JavaScript.JSType;
-
-namespace Learn.Archives.API.Controllers
-{
- ///
- /// 班级控制器
- ///
- public class ClassController : BackController
- {
- readonly Repository baseService;
- readonly LiveUserInfo userInfo;
- public ClassController(Repository baseService, LiveUserInfo userInfo) : base(baseService)
- {
- this.baseService = baseService;
- this.userInfo = userInfo;
- }
- public override async Task PageList([FromBody] QueryRequestBase model)
- {
- var res = (PageResult) await base.PageList(model);
- return new PageResult() { Data = res.Data.Adapt>(), Total = res.Total };
- }
- }
-}
diff --git a/Learn.Archives.API/Controllers/SchoolController.cs b/Learn.Archives.API/Controllers/SchoolController.cs
deleted file mode 100644
index 7701775..0000000
--- a/Learn.Archives.API/Controllers/SchoolController.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using Learn.Archives.API.Controllers.Dto;
-using Learn.Archives.API.Expand;
-using Learn.Archives.Core.Common;
-using Learn.Archives.Core.Model;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-using System.Diagnostics;
-using System.Security.Claims;
-using UserCenter.Model;
-
-namespace Learn.Archives.API.Controllers
-{
- public class SchoolController : BackController
- {
- readonly Repository baseService;
- public SchoolController(Repository baseService) : base(baseService)
- {
- this.baseService = baseService;
- }
-
- public override Task Del([FromBody] params long[] ids)
- {
- return base.Del(ids);
- }
- }
-}
diff --git a/Learn.Archives.API/Controllers/_BaseController.cs b/Learn.Archives.API/Controllers/_BaseController.cs
index 0a18f8b..88d3e04 100644
--- a/Learn.Archives.API/Controllers/_BaseController.cs
+++ b/Learn.Archives.API/Controllers/_BaseController.cs
@@ -45,7 +45,8 @@ namespace Learn.Archives.API.Controllers
///
///
///
- [HttpGet]
+ [HttpGet()]
+ [Route("/api/[controller]/{id}")]
public virtual async Task Info(long id)
{
return await _baseRepository.GetByIdAsync(id);
@@ -139,8 +140,8 @@ namespace Learn.Archives.API.Controllers
[HttpPost]
public virtual async Task> QueryCombo([FromBody] QueryCombo model)
{
- if (string.IsNullOrEmpty(model.ValueName) || string.IsNullOrEmpty(model.TextName))
- Oh.ModelError("ValueName TextName 是必填项");
+ //if (string.IsNullOrEmpty(model.ValueName) || string.IsNullOrEmpty(model.TextName))
+ // Oh.ModelError("ValueName TextName 是必填项");
var sqlquery = BaseQuery(model);
var res = await sqlquery.Select($"{model.TextName} as Text , {model.ValueName} as Value").ToListAsync();
return res;
diff --git a/Learn.Archives.API/Expand/AuthorizeExpand.cs b/Learn.Archives.API/Expand/AuthorizeExpand.cs
index 9008358..4beb4c3 100644
--- a/Learn.Archives.API/Expand/AuthorizeExpand.cs
+++ b/Learn.Archives.API/Expand/AuthorizeExpand.cs
@@ -20,6 +20,8 @@ namespace Learn.Archives.API.Expand
{
options.RequireHttpsMetadata = false;
options.UseSecurityTokenValidators = true;
+ options.MapInboundClaims = false; // .NET 5+
+ JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
options.TokenValidationParameters = new TokenValidationParameters
{
SaveSigninToken = false,//保存token,后台验证token是否生效(重要)
@@ -47,18 +49,14 @@ namespace Learn.Archives.API.Expand
},
OnAuthenticationFailed = context =>
{
- context.Response.Clear();
- context.Response.ContentType = "application/json";
context.Response.StatusCode = 403;
- var data = new BaseReturn() { Code = 403, Message = context.Exception.Message + context.Exception?.StackTrace };
- context.Response.WriteAsync(data.ToJson());
return Task.CompletedTask;
},
OnChallenge = context =>
{
- if(context.Response.StatusCode == 403 || context.Response.StatusCode == 401)
- return Task.CompletedTask;
context.HandleResponse();
+ if (context.Response.StatusCode == 403)
+ return Task.CompletedTask;
context.Response.Clear();
context.Response.ContentType = "application/json";
context.Response.StatusCode = 401;
diff --git a/Learn.Archives.API/Program.cs b/Learn.Archives.API/Program.cs
index bdd7f8d..d21200e 100644
--- a/Learn.Archives.API/Program.cs
+++ b/Learn.Archives.API/Program.cs
@@ -55,9 +55,10 @@ app.UseRouting();
//Զ Ӧ
app.UseCorsExpand();
+app.UseAuthorization();
+
app.UseSqlSugarExpand();
-app.UseAuthorization();
app.MapControllers();
diff --git a/Learn.Archives.API/appsettings.json b/Learn.Archives.API/appsettings.json
index 2b7ae8b..d3ddfa0 100644
--- a/Learn.Archives.API/appsettings.json
+++ b/Learn.Archives.API/appsettings.json
@@ -17,7 +17,7 @@
"UpdateTable": false
},
"AuthKey": {
- "Secret": "9FAB7AC7-F2DB-4C52-B81F-044055A34AF2",
+ "Secret": "9FAB7AC7-F1DB-4C56-B84F-044055A34AF2",
"Issuer": "Learn.Archive", //签发人
"Audience": "Admin",
"Expires": 120 // 过期时间120小时
diff --git a/Learn.Archives.Core/Common/AppConfig.cs b/Learn.Archives.Core/Common/AppConfig.cs
index c3aada0..544d712 100644
--- a/Learn.Archives.Core/Common/AppConfig.cs
+++ b/Learn.Archives.Core/Common/AppConfig.cs
@@ -70,8 +70,22 @@ namespace Learn.Archives.Core.Common
///
public SimpLetexConfig SimpLetex { get; set; } = new SimpLetexConfig();
- }
+ ///
+ /// 用户中心服务配置
+ ///
+ public UserCenterServiceConfig UserCenterService { get; set; } = new UserCenterServiceConfig();
+ }
+ ///
+ /// 用户中心服务配置
+ ///
+ public class UserCenterServiceConfig
+ {
+ ///
+ /// api请求地址
+ ///
+ public string API { get; set; }
+ }
public class AuthKeyConfig
{
///
diff --git a/Learn.Archives.Core/Common/AuthenticationSchemes.cs b/Learn.Archives.Core/Common/AuthenticationSchemes.cs
index b487679..4f36af1 100644
--- a/Learn.Archives.Core/Common/AuthenticationSchemes.cs
+++ b/Learn.Archives.Core/Common/AuthenticationSchemes.cs
@@ -8,6 +8,6 @@ namespace Learn.Archives.Core.Common
{
public class Authentication
{
- public const string Admin = "Admin";
+ public const string Admin = "admin";
}
}
diff --git a/Learn.Archives.Core/Common/ClaimEnum.cs b/Learn.Archives.Core/Common/ClaimEnum.cs
index 51339de..e456ae0 100644
--- a/Learn.Archives.Core/Common/ClaimEnum.cs
+++ b/Learn.Archives.Core/Common/ClaimEnum.cs
@@ -6,7 +6,7 @@
public static string PositionId => "position";
public static string UserId => "user";
public static string Id => "id";
- public static string Role => "roleid";
+ public static string Role => "role";
public static string Scope => "scope";
public static string Name => "name";
}
diff --git a/Learn.Archives.Core/Learn.Archives.Core.csproj b/Learn.Archives.Core/Learn.Archives.Core.csproj
index 3541293..50292c9 100644
--- a/Learn.Archives.Core/Learn.Archives.Core.csproj
+++ b/Learn.Archives.Core/Learn.Archives.Core.csproj
@@ -26,6 +26,7 @@
+
diff --git a/Learn.Archives.Core/Model/Dto/QueryRequestBase.cs b/Learn.Archives.Core/Model/Dto/QueryRequestBase.cs
index 49989d2..2d3cc54 100644
--- a/Learn.Archives.Core/Model/Dto/QueryRequestBase.cs
+++ b/Learn.Archives.Core/Model/Dto/QueryRequestBase.cs
@@ -45,13 +45,13 @@ namespace Learn.Archives.Core.Model.Dto
///
/// 值对应属性名称
///
- [Required(ErrorMessage = "{0}是必填项")]
- public string ValueName { get; set; }
+ [Required(ErrorMessage = "{0}是必填项")]
+ public string ValueName { get; set; } = "Id";
///
/// 文本对应属性名称
///
[Required(ErrorMessage = "{0}是必填项")]
- public string TextName { get; set; }
+ public string TextName { get; set; } = "Name";
}
///
diff --git a/Learn.Archives.Core/Model/Grade.cs b/Learn.Archives.Core/Model/Grade.cs
index fdd5e1c..bde757d 100644
--- a/Learn.Archives.Core/Model/Grade.cs
+++ b/Learn.Archives.Core/Model/Grade.cs
@@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Text.Json;
using UserCenter.Model;
+using UserCenter.Model.Common;
using UserCenter.Model.Enum;
using UserCenter.Model.Interface;
@@ -26,6 +27,11 @@ namespace Learn.Archives.Core.Model
[SugarColumn(Length = 6)]
public int Year { get; set; }
///
+ /// 年级名称
+ ///
+ [SugarColumn(IsIgnore =true)]
+ public string Name => GradeHelper.GetGrade(Level.ToString(), Year);
+ ///
/// 学校名称
///
[SugarColumn(Length = 12)]