大版本更新
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -29,16 +30,21 @@ namespace Yi.Framework.WebCore.AttributeExtend
|
||||
{
|
||||
throw new Exception("权限不能为空!");
|
||||
}
|
||||
|
||||
//可以从Redis得到用户菜单列表,或者直接从jwt中获取
|
||||
|
||||
var result = false;
|
||||
|
||||
//判断权限是否存在Redis中
|
||||
if (permission.Length>0)
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
|
||||
//可以从Redis得到用户菜单列表,或者直接从jwt中获取
|
||||
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();
|
||||
//判断权限是否存在Redis中,或者jwt中
|
||||
|
||||
//if (perList.Contains(permission.ToLower()))
|
||||
//{
|
||||
// result = true;
|
||||
//}
|
||||
result = true;
|
||||
|
||||
|
||||
if (!result)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -15,51 +17,30 @@ namespace Yi.Framework.WebCore.AuthorizationPolicy
|
||||
public class CustomAuthorizationHandler : AuthorizationHandler<CustomAuthorizationRequirement>
|
||||
{
|
||||
|
||||
private CacheClientDB _cacheClientDB;
|
||||
//private CacheClientDB _cacheClientDB;
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public CustomAuthorizationHandler(CacheClientDB cacheClientDB)
|
||||
public CustomAuthorizationHandler()
|
||||
{
|
||||
_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))
|
||||
var currentClaim = context.User.Claims.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid);
|
||||
//DefaultHttpContext httpcontext = (DefaultHttpContext)context.AuthenticateAsync();
|
||||
if (currentClaim!=null) //说明没有写入Sid 没有登录
|
||||
{
|
||||
context.Succeed(requirement); //验证通过了
|
||||
}
|
||||
//string currentUserId = "";
|
||||
//if (!string.IsNullOrWhiteSpace(currentClaim.Value))
|
||||
//{
|
||||
// currentUserId = currentClaim.Value;
|
||||
//}
|
||||
//DefaultHttpContext httpcontext = (DefaultHttpContext)context.Resource;
|
||||
return Task.CompletedTask; //验证不同过
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,6 @@ namespace Yi.Framework.WebCore.AuthorizationPolicy
|
||||
}
|
||||
public static class PolicyName
|
||||
{
|
||||
public const string Menu = "Menu";
|
||||
public const string Sid = "Sid";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,9 +33,8 @@ namespace Yi.Framework.WebCore
|
||||
public static UserEntity GetCurrentUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
|
||||
{
|
||||
IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
|
||||
|
||||
var resId= new Guid (claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value);
|
||||
|
||||
|
||||
long.TryParse(claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value,out var resId) ;
|
||||
|
||||
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList();
|
||||
|
||||
|
||||
@@ -13,15 +13,15 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
public static IServiceCollection AddAuthorizationService(this IServiceCollection services)
|
||||
{
|
||||
//services.AddAuthorization(options =>
|
||||
//{
|
||||
// options.AddPolicy(PolicyName.Menu, polic =>
|
||||
// {
|
||||
// polic.AddRequirements(new CustomAuthorizationRequirement(PolicyEnum.MenuPermissions));
|
||||
// });
|
||||
//});
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy(PolicyName.Sid, polic =>
|
||||
{
|
||||
polic.AddRequirements(new CustomAuthorizationRequirement(PolicyEnum.MenuPermissions));
|
||||
});
|
||||
});
|
||||
|
||||
//services.AddSingleton<IAuthorizationHandler, CustomAuthorizationHandler>();
|
||||
services.AddSingleton<IAuthorizationHandler, CustomAuthorizationHandler>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Yi.Framework.Common.IOCOptions;
|
||||
using Yi.Framework.Model;
|
||||
|
||||
namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
@@ -20,6 +21,11 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
#endregion
|
||||
services.AddSingleton(new Appsettings(configuration));
|
||||
|
||||
#region
|
||||
//数据库连接字符串
|
||||
#endregion
|
||||
services.Configure<SqlConnOptions>(Appsettings.appConfiguration("DbConn"));
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,11 +27,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,//是否验证Issuer
|
||||
ValidateIssuer = true,//是否验证Issuer
|
||||
ValidateAudience = true,//是否验证Audience
|
||||
ValidateLifetime = true,//是否验证失效时间
|
||||
|
||||
|
||||
ValidateIssuerSigningKey = true,//是否验证SecurityKey
|
||||
ValidAudience = jwtOptions.Audience,//Audience
|
||||
ValidIssuer = jwtOptions.Issuer,//Issuer,这两项和前面签发jwt的设置一致
|
||||
|
||||
@@ -12,11 +12,36 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
public static void AddSqlsugarServer(this IServiceCollection services)
|
||||
{
|
||||
DbType dbType;
|
||||
var slavaConFig = new List<SlaveConnectionConfig>();
|
||||
if (Appsettings.appBool("MutiDB_Enabled"))
|
||||
{
|
||||
var readCon = Appsettings.app<List<string>>("DbConn", "ReadUrl");
|
||||
|
||||
readCon.ForEach(s => {
|
||||
slavaConFig.Add(new SlaveConnectionConfig() { ConnectionString = s });
|
||||
});
|
||||
}
|
||||
|
||||
switch (Appsettings.app("DbSelect"))
|
||||
{
|
||||
case "Mysql": dbType = DbType.MySql; break;
|
||||
case "Sqlite": dbType = DbType.Sqlite; break;
|
||||
case "Sqlserver": dbType = DbType.SqlServer; break;
|
||||
case "Oracle": dbType = DbType.Oracle; break;
|
||||
default:throw new Exception("DbSelect配置写的TM是个什么东西?");
|
||||
}
|
||||
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
DbType = SqlSugar.DbType.MySql,
|
||||
DbType = dbType,
|
||||
ConnectionString = Appsettings.app("DbConn", "WriteUrl"),
|
||||
IsAutoCloseConnection = true
|
||||
IsAutoCloseConnection = true,
|
||||
MoreSettings = new ConnMoreSettings()
|
||||
{
|
||||
DisableNvarchar = true
|
||||
},
|
||||
SlaveConnectionConfigs = slavaConFig,
|
||||
|
||||
},
|
||||
db =>
|
||||
{
|
||||
@@ -31,7 +56,6 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
//entityInfo.SetValue(new Guid(httpcontext.Request.Headers["Id"].ToString()));
|
||||
}
|
||||
|
||||
if (entityInfo.PropertyName == "TenantId")
|
||||
{
|
||||
//entityInfo.SetValue(new Guid(httpcontext.Request.Headers["TenantId"].ToString()));
|
||||
|
||||
Reference in New Issue
Block a user