优化功能、类目录

This commit is contained in:
不做码农 2023-08-29 16:40:25 +08:00
parent 94fa69e8ef
commit 07b4b95732
22 changed files with 62 additions and 56 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Hosting;
using Infrastructure.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

View File

@ -2,6 +2,9 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>8632</NoWarn>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
@ -16,6 +19,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="IPTools.China" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.7" />
</ItemGroup>
</Project>

View File

@ -1,14 +1,20 @@
using Microsoft.IdentityModel.Tokens;
using Infrastructure.Extensions;
using Infrastructure.Model;
using Infrastructure.WebExtensins;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using ZR.Model.System.Dto;
namespace ZR.Admin.WebApi.Framework
namespace Infrastructure
{
/// <summary>
/// 2020-11-20
/// 2023-8-29已从WebApi移至此
/// </summary>
public class JwtUtil
{
@ -17,7 +23,7 @@ namespace ZR.Admin.WebApi.Framework
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
public static LoginUser GetLoginUser(HttpContext httpContext)
public static TokenModel GetLoginUser(HttpContext httpContext)
{
string token = httpContext.GetToken();
@ -53,7 +59,7 @@ namespace ZR.Admin.WebApi.Framework
IssuedAt = authTime,//token生成时间
Expires = expiresAt,
//NotBefore = authTime,
TokenType = "Bearer",
TokenType = jwtSettings.TokenType,
//对称秘钥,签名证书
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
@ -118,18 +124,18 @@ namespace ZR.Admin.WebApi.Framework
/// </summary>
/// <param name="jwtSecurityToken"></param>
/// <returns></returns>
public static LoginUser? ValidateJwtToken(JwtSecurityToken jwtSecurityToken)
public static TokenModel? ValidateJwtToken(JwtSecurityToken jwtSecurityToken)
{
try
{
if (jwtSecurityToken == null) return null;
IEnumerable<Claim> claims = jwtSecurityToken?.Claims;
LoginUser loginUser = null;
TokenModel loginUser = null;
var userData = claims.FirstOrDefault(x => x.Type == ClaimTypes.UserData)?.Value;
if (userData != null)
{
loginUser = JsonConvert.DeserializeObject<LoginUser>(userData);
loginUser = JsonConvert.DeserializeObject<TokenModel>(userData);
loginUser.ExpireTime = jwtSecurityToken.ValidTo;
}
return loginUser;
@ -146,7 +152,7 @@ namespace ZR.Admin.WebApi.Framework
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public static List<Claim> AddClaims(LoginUser user)
public static List<Claim> AddClaims(TokenModel user)
{
var claims = new List<Claim>()
{

View File

@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Infrastructure
namespace Infrastructure.Model
{
/// <summary>
/// 获取配置文件POCO实体类
@ -80,6 +80,14 @@ namespace Infrastructure
/// token时间
/// </summary>
public int Expire { get; set; } = 1440;
/// <summary>
/// 刷新token时长
/// </summary>
public int RefreshTokenTime { get; set; }
/// <summary>
/// token类型
/// </summary>
public string TokenType { get; set; } = "Bearer";
}
public class Gen

View File

@ -1,11 +1,10 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ZR.Model.System.Dto
namespace Infrastructure.Model
{
/// <summary>
/// 登录用户信息存储
/// </summary>
public class LoginUser
public class TokenModel
{
public long UserId { get; set; }
public long DeptId { get; set; }
@ -26,15 +25,15 @@ namespace ZR.Model.System.Dto
/// 权限集合
/// </summary>
//public List<string> Permissions { get; set; } = new List<string>();
public LoginUser()
public TokenModel()
{
}
public LoginUser(SysUser user, List<Roles> roles)
public TokenModel(TokenModel info, List<Roles> roles)
{
UserId = user.UserId;
UserName = user.UserName;
DeptId = user.DeptId;
UserId = info.UserId;
UserName = info.UserName;
DeptId = info.DeptId;
Roles = roles;
RoleIds = roles.Select(f => f.RoleKey).ToList();
}

View File

@ -1,10 +1,7 @@
using IPTools.Core;
using Lazy.Captcha.Core;
using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using UAParser;
using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Framework;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.Service.System;
@ -86,7 +83,7 @@ namespace ZR.Admin.WebApi.Controllers.System
//权限集合 eg *:*:*,system:user:list
List<string> permissions = permissionService.GetMenuPermission(user);
LoginUser loginUser = new(user, roles.Adapt<List<Roles>>());
TokenModel loginUser = new(user.Adapt<TokenModel>(), roles.Adapt<List<Roles>>());
CacheService.SetUserPerms(GlobalConstant.UserPermKEY + user.UserId, permissions);
return SUCCESS(JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser)));
}

View File

@ -1,5 +1,4 @@
using Infrastructure.Constant;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Hubs;

View File

@ -1,7 +1,5 @@
using Infrastructure;
using SqlSugar;
using SqlSugar.IOC;
using SqlSugar;
using ZR.Admin.WebApi.Framework;
using ZR.Model.System;
namespace ZR.Admin.WebApi.Extensions

View File

@ -1,4 +1,3 @@
using Infrastructure;
using Infrastructure.Extensions;
using SqlSugar;
using SqlSugar.IOC;

View File

@ -14,7 +14,7 @@ namespace Infrastructure
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("🎉源码地址: https://gitee.com/izory/ZrAdminNetCore");
Console.WriteLine("📖官方文档http://www.izhaorui.cn/doc");
Console.WriteLine("🤑打赏作者http://www.izhaorui.cn/doc/support.html");
Console.WriteLine("💰打赏作者http://www.izhaorui.cn/doc/support.html");
Console.WriteLine("📱移动端体验http://www.izhaorui.cn/h5");
}
}

View File

@ -1,10 +1,6 @@
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Data;
using ZR.Admin.WebApi.Framework;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.Service.System;
using ZR.Service.System.IService;
@ -41,7 +37,7 @@ namespace ZR.Admin.WebApi.Filters
/// <returns></returns>
public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
LoginUser info = JwtUtil.GetLoginUser(context.HttpContext);
TokenModel info = JwtUtil.GetLoginUser(context.HttpContext);
if (info != null && info?.UserId > 0)
{
@ -79,7 +75,7 @@ namespace ZR.Admin.WebApi.Filters
string[] denyPerms = new string[] { "update", "add", "remove", "add", "edit", "delete", "import", "run", "start", "stop", "clear", "send", "export", "upload", "common", "gencode", "reset" };
if (isDemoMode && denyPerms.Any(f => Permission.ToLower().Contains(f)))
{
context.Result = new JsonResult(new { code = ResultCode.FORBIDDEN, msg = "演示模式 , 不允许操作" });
context.Result = new JsonResult(new { code = (int)ResultCode.FORBIDDEN, msg = "演示模式 , 不允许操作" });
}
if (!HasPermi && !Permission.Equals("common"))
{

View File

@ -2,8 +2,6 @@
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using NLog;
using ZR.Admin.WebApi.Framework;
using ZR.Model.System.Dto;
namespace ZR.Admin.WebApi.Filters
{
@ -36,7 +34,7 @@ namespace ZR.Admin.WebApi.Filters
var isAuthed = context.HttpContext.User.Identity.IsAuthenticated;
//使用jwt token校验2020-11-21
LoginUser loginUser = JwtUtil.GetLoginUser(context.HttpContext);
TokenModel loginUser = JwtUtil.GetLoginUser(context.HttpContext);
if (loginUser != null)
{
var nowTime = DateTime.UtcNow;
@ -56,7 +54,7 @@ namespace ZR.Admin.WebApi.Filters
if (loginUser == null || !isAuthed)
{
string msg = $"请求访问[{url}]失败,无法访问系统资源";
logger.Info($"{msg}");
//logger.Info(msg);
context.Result = new JsonResult(new ApiResult((int)ResultCode.DENY, msg));
}

View File

@ -1,4 +1,4 @@
namespace Infrastructure.Constant
namespace ZR.Admin.WebApi.Hubs
{
public class HubsConstant
{

View File

@ -1,5 +1,4 @@
using Infrastructure.Constant;
using IPTools.Core;
using IPTools.Core;
using Microsoft.AspNetCore.SignalR;
using System.Web;
using UAParser;
@ -81,7 +80,7 @@ namespace ZR.Admin.WebApi.Hubs
userInfo.TodayOnlineTime = 0;
}
var clientUser = onlineClients.Find(x => x.Userid == userid);
userInfo.TodayOnlineTime += clientUser?.OnlineTime ?? 0;
userInfo.TodayOnlineTime += Math.Round(clientUser?.OnlineTime ?? 0, 2);
}
//给当前所有登录当前账号的用户下发登录时长
var connIds = onlineClients.Where(f => f.Userid == userid).ToList();
@ -123,7 +122,7 @@ namespace ZR.Admin.WebApi.Hubs
{
userInfo.TodayOnlineTime += user?.OnlineTime ?? 0;
}
Log.WriteLine(ConsoleColor.Green, msg: $"用户{user?.Name}离开了,已在线{userInfo?.TodayOnlineTime}分,当前已连接{onlineClients.Count}个");
Log.WriteLine(ConsoleColor.Red, msg: $"用户{user?.Name}离开了,已在线{userInfo?.TodayOnlineTime}分,当前已连接{onlineClients.Count}个");
}
return base.OnDisconnectedAsync(exception);
}

View File

@ -1,16 +1,13 @@
using AspNetCoreRateLimit;
using Infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.IdentityModel.Tokens;
using System.Text.Json.Serialization;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Framework;
using ZR.Admin.WebApi.Hubs;
using ZR.Admin.WebApi.Middleware;
using ZR.Common.Cache;
using ZR.Model.System.Dto;
var builder = WebApplication.CreateBuilder(args);

View File

@ -17,7 +17,6 @@
<ItemGroup>
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
<PackageReference Include="Lazy.Captcha.Core" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
<PackageReference Include="NLog" Version="5.2.3" />

View File

@ -30,7 +30,8 @@
"Audience": "ZRAdmin.NET", //token
"SecretKey": "SecretKey-ZRADMIN.NET-20210101",
"Expire": 1440, //jwt
"refreshTokenTime": 5
"RefreshTokenTime": 5,//
"TokenType": "Bearer"
},
"InjectClass": [ "ZR.Repository", "ZR.Service", "ZR.Tasks" ], //
"ShowDbLog": true, //db

View File

@ -1,5 +1,6 @@
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Model;
using JinianNet.JNTemplate;
using SqlSugar;
using System;

View File

@ -1,4 +1,5 @@
using Infrastructure;
using Infrastructure.Model;
using SqlSugar;
using System;
using System.Collections.Generic;

View File

@ -1,4 +1,5 @@
using Infrastructure;
using Infrastructure.Model;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;

View File

@ -1,4 +1,5 @@
using Infrastructure;
using Infrastructure.Model;
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;

View File

@ -1,6 +1,7 @@
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Infrastructure.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;