feat: 后端添加管理员认证
This commit is contained in:
parent
3f2c772eeb
commit
546b9250fa
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Security.Claims;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
|
@ -61,6 +62,15 @@ try
|
||||||
options.Authority = "http://localhost:5000";
|
options.Authority = "http://localhost:5000";
|
||||||
options.RequireHttpsMetadata = false;
|
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
|
// Add CORS policy
|
||||||
if (builder.Environment.IsDevelopment())
|
if (builder.Environment.IsDevelopment())
|
||||||
|
@ -159,6 +169,7 @@ try
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.UseCors();
|
app.UseCors();
|
||||||
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
// Swagger
|
// Swagger
|
||||||
|
|
|
@ -69,6 +69,7 @@ public class DataController : ControllerBase
|
||||||
{
|
{
|
||||||
new Claim(ClaimTypes.Name, user.Name),
|
new Claim(ClaimTypes.Name, user.Name),
|
||||||
new Claim(ClaimTypes.Email, user.EMail),
|
new Claim(ClaimTypes.Email, user.EMail),
|
||||||
|
new Claim(ClaimTypes.Role, user.Permission.ToString())
|
||||||
}),
|
}),
|
||||||
Expires = DateTime.UtcNow.AddHours(1),
|
Expires = DateTime.UtcNow.AddHours(1),
|
||||||
SigningCredentials = new SigningCredentials(
|
SigningCredentials = new SigningCredentials(
|
||||||
|
@ -96,6 +97,20 @@ public class DataController : ControllerBase
|
||||||
return Ok("认证成功!");
|
return Ok("认证成功!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试管理员用户认证,需携带有效 JWT
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>认证成功信息</returns>
|
||||||
|
[Authorize("Admin")]
|
||||||
|
[HttpGet("TestAdminAuth")]
|
||||||
|
[EnableCors("Users")]
|
||||||
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||||
|
public IActionResult TestAdminAuth()
|
||||||
|
{
|
||||||
|
return Ok("认证成功!");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取当前用户信息
|
/// 获取当前用户信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -182,7 +182,6 @@ onMounted(() => {
|
||||||
|
|
||||||
// 监听路由变化
|
// 监听路由变化
|
||||||
router.afterEach(() => {
|
router.afterEach(() => {
|
||||||
console.log("Route is changing, reloading user info...");
|
|
||||||
loadUserInfo();
|
loadUserInfo();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="min-h-screen bg-base-100 container mx-auto p-6 space-y-6 flex flex-row">
|
<div
|
||||||
|
class="min-h-screen bg-base-100 container mx-auto p-6 space-y-6 flex flex-row"
|
||||||
|
>
|
||||||
<ul class="menu bg-base-200 w-56 gap-2 rounded-2xl p-5">
|
<ul class="menu bg-base-200 w-56 gap-2 rounded-2xl p-5">
|
||||||
<li id="1" @click="setActivePage">
|
<li id="1" @click="setActivePage">
|
||||||
<a :class="{ 'menu-active': activePage === 1 }">用户信息</a>
|
<a :class="{ 'menu-active': activePage === 1 }">用户信息</a>
|
||||||
|
@ -7,8 +9,8 @@
|
||||||
<li id="2" @click="setActivePage">
|
<li id="2" @click="setActivePage">
|
||||||
<a :class="{ 'menu-active': activePage === 2 }">Item 2</a>
|
<a :class="{ 'menu-active': activePage === 2 }">Item 2</a>
|
||||||
</li>
|
</li>
|
||||||
<li id="" @click="setActivePage">
|
<li id="100" @click="setActivePage">
|
||||||
<a :class="{ 'menu-active': activePage === 2 }">Item 2</a>
|
<a :class="{ 'menu-active': activePage === 100 }">实验板控制台</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<div class="divider divider-horizontal h-full"></div>
|
<div class="divider divider-horizontal h-full"></div>
|
||||||
|
|
Loading…
Reference in New Issue