diff --git a/server/Program.cs b/server/Program.cs
index 2199e1f..bb45f6c 100644
--- a/server/Program.cs
+++ b/server/Program.cs
@@ -1,3 +1,4 @@
+using System.Security.Claims;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.Features;
@@ -61,6 +62,15 @@ try
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
});
+ builder.Services.AddAuthorization(options =>
+ {
+ options.AddPolicy("Admin", policy =>
+ {
+ policy.RequireClaim(ClaimTypes.Role, new string[] {
+ Database.User.UserPermission.Admin.ToString(),
+ });
+ });
+ });
// Add CORS policy
if (builder.Environment.IsDevelopment())
@@ -159,6 +169,7 @@ try
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
+ app.UseAuthentication();
app.UseAuthorization();
// Swagger
diff --git a/server/src/Controllers/DataController.cs b/server/src/Controllers/DataController.cs
index 89fd1b5..1a895c9 100644
--- a/server/src/Controllers/DataController.cs
+++ b/server/src/Controllers/DataController.cs
@@ -69,6 +69,7 @@ public class DataController : ControllerBase
{
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Email, user.EMail),
+ new Claim(ClaimTypes.Role, user.Permission.ToString())
}),
Expires = DateTime.UtcNow.AddHours(1),
SigningCredentials = new SigningCredentials(
@@ -96,6 +97,20 @@ public class DataController : ControllerBase
return Ok("认证成功!");
}
+ ///
+ /// 测试管理员用户认证,需携带有效 JWT
+ ///
+ /// 认证成功信息
+ [Authorize("Admin")]
+ [HttpGet("TestAdminAuth")]
+ [EnableCors("Users")]
+ [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status401Unauthorized)]
+ public IActionResult TestAdminAuth()
+ {
+ return Ok("认证成功!");
+ }
+
///
/// 获取当前用户信息
///
diff --git a/src/components/Navbar.vue b/src/components/Navbar.vue
index 9f61f57..440bafa 100644
--- a/src/components/Navbar.vue
+++ b/src/components/Navbar.vue
@@ -182,7 +182,6 @@ onMounted(() => {
// 监听路由变化
router.afterEach(() => {
- console.log("Route is changing, reloading user info...");
loadUserInfo();
});
});
diff --git a/src/views/User/Index.vue b/src/views/User/Index.vue
index 54fc5c1..98766be 100644
--- a/src/views/User/Index.vue
+++ b/src/views/User/Index.vue
@@ -1,5 +1,7 @@
-