完成权限点对点
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.DTOModel;
|
||||
using Yi.Framework.Model.Models;
|
||||
|
||||
namespace Yi.Framework.WebCore.AuthorizationPolicy
|
||||
{
|
||||
//策略验证的Handler 继承AuthorizationHandler 泛型类 泛型参数为 策略参数
|
||||
public class CustomAuthorizationHandler : AuthorizationHandler<CustomAuthorizationRequirement>
|
||||
{
|
||||
|
||||
private CacheClientDB _cacheClientDB;
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public CustomAuthorizationHandler(CacheClientDB cacheClientDB)
|
||||
{
|
||||
_cacheClientDB= cacheClientDB;
|
||||
}
|
||||
|
||||
//验证的方法就在这里
|
||||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CustomAuthorizationRequirement requirement)
|
||||
{
|
||||
var currentClaim = context.User.Claims.FirstOrDefault(u => u.Type == ClaimTypes.Sid);
|
||||
|
||||
if (currentClaim==null) //说明没有写入Sid 没有登录
|
||||
{
|
||||
return Task.CompletedTask; //验证不同过
|
||||
}
|
||||
|
||||
int currentUserId = 0;
|
||||
if (!string.IsNullOrWhiteSpace(currentClaim.Value))
|
||||
{
|
||||
currentUserId = Convert.ToInt32(currentClaim.Value);
|
||||
}
|
||||
DefaultHttpContext httpcontext = (DefaultHttpContext)context.Resource;
|
||||
Dictionary<string, string> dicMenueDictionary = new Dictionary<string, string>();
|
||||
//现在只需要登录的时候把用户的api路径添加到redis去
|
||||
//每次访问的时候进行redis判断一下即可
|
||||
//注意一下,redis不能一直保存,和jwt一样搞一个期限
|
||||
var menuList=_cacheClientDB.Get<List<menuDto>>(RedisConst.userMenusApi+":"+currentUserId);
|
||||
foreach (var k in menuList)
|
||||
{
|
||||
if (k.mould != null)
|
||||
{
|
||||
dicMenueDictionary.Add(k.mould?.id.ToString(), "/api"+ k.mould?.url);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (dicMenueDictionary.ContainsValue(httpcontext.Request.Path))
|
||||
{
|
||||
context.Succeed(requirement); //验证通过了
|
||||
}
|
||||
return Task.CompletedTask; //验证不同过
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 菜单权限策略
|
||||
/// </summary>
|
||||
public static class CustomAuthorizationHandlerExtension
|
||||
{
|
||||
public static Task AuthorizationMenueExtension(this AuthorizationHandlerContext handlerContext, CustomAuthorizationRequirement requirement)
|
||||
{
|
||||
bool bog = true;
|
||||
if (bog)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
handlerContext.Succeed(requirement); //验证通过了
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
return Task.CompletedTask; //验证不同过
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.WebCore.AuthorizationPolicy
|
||||
{
|
||||
//定义策略参数,必须实现这个IAuthorizationRequirement接口
|
||||
public class CustomAuthorizationRequirement: IAuthorizationRequirement
|
||||
{
|
||||
public CustomAuthorizationRequirement(PolicyEnum policyname)
|
||||
{
|
||||
this.PolicyName = policyname;
|
||||
}
|
||||
public PolicyEnum PolicyName { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.WebCore.AuthorizationPolicy
|
||||
{
|
||||
public enum PolicyEnum
|
||||
{
|
||||
/// <summary>
|
||||
/// 菜单
|
||||
/// </summary>
|
||||
MenuPermissions,
|
||||
//...还可以定义其他的各种权限策略名称
|
||||
}
|
||||
public static class PolicyName
|
||||
{
|
||||
public const string Menu = "Menu";
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ namespace Yi.Framework.WebCore
|
||||
|
||||
/// <summary>
|
||||
/// 基于HttpContext,当前鉴权方式解析,获取用户信息
|
||||
/// 现在使用redis作为缓存,不需要将菜单存放至jwt中了
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
@@ -28,5 +28,13 @@ namespace Yi.Framework.WebCore.Mapper
|
||||
IMapper mapper = new AutoMapper.Mapper(config);
|
||||
return mapper.Map<Source, Target>(source);
|
||||
}
|
||||
public static List<Target> MapList<Target, Source>(List<Source> source)
|
||||
{
|
||||
var cfg = new MapperConfigurationExpression();
|
||||
cfg.CreateMap<Source, Target>();
|
||||
var config = new MapperConfiguration(cfg);
|
||||
IMapper mapper = new AutoMapper.Mapper(config);
|
||||
return mapper.Map<List<Source>, List<Target>>(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.WebCore.AuthorizationPolicy;
|
||||
|
||||
namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
public static class AuthorizationExtension
|
||||
{
|
||||
public static IServiceCollection AddAuthorizationService(this IServiceCollection services)
|
||||
{
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy(PolicyName.Menu, polic =>
|
||||
{
|
||||
polic.AddRequirements(new CustomAuthorizationRequirement(PolicyEnum.MenuPermissions));
|
||||
});
|
||||
});
|
||||
|
||||
services.AddSingleton<IAuthorizationHandler, CustomAuthorizationHandler>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user