权限code过滤器

This commit is contained in:
陈淳
2022-09-27 16:22:34 +08:00
parent 26e08774b0
commit 6ce05984d5
9 changed files with 44 additions and 27 deletions

View File

@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Yi.Framework.Common.Const;
namespace Yi.Framework.WebCore.AttributeExtend
{
@@ -25,12 +26,13 @@ namespace Yi.Framework.WebCore.AttributeExtend
/// <exception cref="Exception"></exception>
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
if (string.IsNullOrEmpty(permission))
{
throw new Exception("权限不能为空!");
}
var result = false;
@@ -38,16 +40,20 @@ namespace Yi.Framework.WebCore.AttributeExtend
var sid = context.HttpContext.User.Claims.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid);
//jwt存在的权限列表
var perList = context.HttpContext.User.Claims.Where(u => u.Type == "permission").Select(u=> u.Value.ToString().ToLower()). ToList();
var perList = context.HttpContext.User.Claims.Where(u => u.Type == SystemConst.PermissionClaim).Select(u => u.Value.ToString().ToLower()).ToList();
//判断权限是否存在Redis中,或者jwt中
//进行正则表达式的匹配以code开头
Regex regex = new Regex($"^{permission.ToLower()}");
foreach (var p in perList)
{
//过滤多余的标签
p.Replace("Entity","");
p.Replace("entity","");
//如果存在超级管理员权限,直接放行
if (SystemConst.AdminPermissionCode.Equals(p))
{
result = true;
break;
}
if (regex.IsMatch(p))
{
result = true;
@@ -56,11 +62,6 @@ namespace Yi.Framework.WebCore.AttributeExtend
}
//用户的增删改查直接可以user:*即可
//这里暂时全部放行即可
result = true;
if (!result)
{
throw new Exception("拦截未授权请求!");

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
@@ -15,10 +16,11 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
public class ErrorHandExtension
{
private readonly RequestDelegate next;
public ErrorHandExtension(RequestDelegate next)
private ILogger<ErrorHandExtension> _logger;
public ErrorHandExtension(RequestDelegate next,ILogger<ErrorHandExtension> logger)
{
this.next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
@@ -34,6 +36,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
{
statusCode = 200;
}
_logger.LogError($"中间件抓取错误\r\n错误信息{ex.Message}\r\n堆栈信息“{ex.StackTrace}");
await HandleExceptionAsync(context, statusCode, ex.Message);
}
finally