0
0
Просмотр исходного кода

更新1.3.4
升级 vue cli到4.4.4版本,升级eslint相关插件
新增 支持api和跨域地址多地址配置
新增 ip帮助类增加获得mac地址方法
增加前后端iis发布部分代码注释说明
权限管理输入更改Dto编码问题

xiaoxue 4 лет назад
Родитель
Сommit
6f31ad204c

+ 4 - 4
Admin.Core.Common/Admin.Core.Common.csproj

@@ -21,7 +21,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="CSRedisCore" Version="3.6.3" />
+    <PackageReference Include="CSRedisCore" Version="3.6.5" />
     <PackageReference Include="FreeSql" Version="1.6.0-preview0602" />
     <PackageReference Include="FreeSql.Provider.MySql" Version="1.6.0-preview0602" />
     <PackageReference Include="FreeSql.Provider.Oracle" Version="1.6.0-preview0602" />
@@ -30,9 +30,9 @@
     <PackageReference Include="FreeSql.Provider.SqlServer" Version="1.6.0-preview0602" />
     <PackageReference Include="FreeSql.Repository" Version="1.6.0-preview0602" />
     <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
-    <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.4" />
-    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
-    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.4" />
+    <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.5" />
+    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.5" />
+    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.5" />
     <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.6.0" />
     <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
     <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.6.0" />

+ 1 - 1
Admin.Core.Common/Auth/IUserToken.cs

@@ -4,7 +4,7 @@ namespace Admin.Core.Common.Auth
 {
     public interface IUserToken
     {
-        string Build(Claim[] claims);
+        string Create(Claim[] claims);
 
         Claim[] Decode(string jwtToken);
     }

+ 1 - 2
Admin.Core.Common/Auth/UserToken.cs

@@ -7,7 +7,6 @@ using Microsoft.IdentityModel.Tokens;
 using Admin.Core.Common.Configs;
 using Admin.Core.Common.Attributes;
 using System.Linq;
-using Google.Protobuf.WellKnownTypes;
 
 namespace Admin.Core.Common.Auth
 {
@@ -21,7 +20,7 @@ namespace Admin.Core.Common.Auth
             _jwtConfig = jwtConfig;
         }
 
-        public string Build(Claim[] claims)
+        public string Create(Claim[] claims)
         {
             var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtConfig.SecurityKey));
             var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

+ 8 - 3
Admin.Core.Common/Configs/AppConfig.cs

@@ -8,7 +8,12 @@
         /// <summary>
         /// Api地址,默认 http://*:8888
         /// </summary>
-        public string Urls { get; set; } = "http://*:8888";
+        public string[] Urls { get; set; }// = new[]{ "http://*:8888" };
+
+        /// <summary>
+        /// 跨域地址,默认 http://*:9999
+        /// </summary>
+        public string[] CorUrls { get; set; }// = new[]{ "http://*:9999" };
 
         /// <summary>
         /// Swagger文档
@@ -67,10 +72,10 @@
         /// 启用
         /// </summary>
         public bool Enable { get; set; } = true;
-        
+
         /// <summary>
         /// 操作日志
         /// </summary>
-        public string[] Font { get; set; } = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
+        public string[] Fonts { get; set; }// = new[] { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
     }
 }

+ 1 - 1
Admin.Core.Common/Helpers/ConfigHelper.cs

@@ -72,7 +72,7 @@ namespace Admin.Core.Common.Helpers
         public void Bind(string fileName, object instance, string environmentName = "", bool reloadOnChange = false)
         {
             var configuration = Load(fileName, environmentName, reloadOnChange);
-            if (configuration == null)
+            if (configuration == null || instance == null)
                 return;
 
             configuration.Bind(instance);

+ 55 - 1
Admin.Core.Common/Helpers/IPHelper.cs

@@ -1,4 +1,5 @@
 using System.Linq;
+using System.Net.NetworkInformation;
 using System.Text.RegularExpressions;
 using Microsoft.AspNetCore.Http;
 
@@ -16,13 +17,18 @@ namespace Admin.Core.Common.Helpers
             return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
         }
 
+        /// <summary>
+        /// 获得IP地址
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
         public static string GetIP(HttpRequest request)
         {
             if (request == null)
             {
                 return "";
             }
-
+            
             string ip = request.Headers["X-Real-IP"].FirstOrDefault();
             if (ip.IsNull())
             {
@@ -39,5 +45,53 @@ namespace Admin.Core.Common.Helpers
 
             return ip;
         }
+
+        /// <summary>
+        /// 获得MAC地址
+        /// </summary>
+        /// <returns></returns>
+        public static string GetMACIp()
+        {
+            //本地计算机网络连接信息
+            //IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
+            //获取本机电脑名
+            //var HostName = computerProperties.HostName;
+            //获取域名
+            //var DomainName = computerProperties.DomainName;
+
+            //获取本机所有网络连接
+            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
+
+            if (nics == null || nics.Length < 1)
+            {
+                return "";
+            }
+
+            var MACIp = "";
+            foreach (NetworkInterface adapter in nics)
+            {
+                var adapterName = adapter.Name;
+
+                var adapterDescription = adapter.Description;
+                var NetworkInterfaceType = adapter.NetworkInterfaceType;
+                if (adapterName == "本地连接" || adapterName == "WLAN")
+                {
+                    PhysicalAddress address = adapter.GetPhysicalAddress();
+                    byte[] bytes = address.GetAddressBytes();
+
+                    for (int i = 0; i < bytes.Length; i++)
+                    {
+                        MACIp += bytes[i].ToString("X2");
+
+                        if (i != bytes.Length - 1)
+                        {
+                            MACIp += "-";
+                        }
+                    }
+                }
+            }
+
+            return MACIp;
+        }
     }
 }

+ 2 - 2
Admin.Core.Common/Helpers/VerifyCodeHelper.cs

@@ -40,7 +40,7 @@ namespace Admin.Core.Common.Helpers
             //颜色列表,用于验证码、噪线、噪点 
             Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
             //字体列表,用于验证码 
-            string[] font = _appConfig.VarifyCode.Font;
+            string[] fonts = _appConfig.VarifyCode.Fonts;
 
             code = GenerateRandom(length);
 
@@ -69,7 +69,7 @@ namespace Admin.Core.Common.Helpers
                     Color clr;
                     for (int i = 0; i < code.Length; i++)
                     {
-                        fnt = font[rnd.Next(font.Length)];
+                        fnt = fonts[rnd.Next(fonts.Length)];
                         ft = new Font(fnt, fontSize);
                         clr = color[rnd.Next(color.Length)];
                         g.DrawString(code[i].ToString(), ft, new SolidBrush(clr), (float)i * 24 + 2, (float)0);

+ 3 - 3
Admin.Core.Services/Admin/Permission/Input/PermissionUpdateApiInput.cs

@@ -1,14 +1,14 @@
-namespace Admin.Core.Service.Admin.Permission.Input
+锘縩amespace Admin.Core.Service.Admin.Permission.Input
 {
     public class PermissionUpdateApiInput : PermissionAddApiInput
     {
         /// <summary>
-        /// 权限Id
+        /// 鏉冮檺Id
         /// </summary>
         public long Id { get; set; }
 
         /// <summary>
-        /// 版本
+        /// 鐗堟湰
         /// </summary>
         public long Version { get; set; }
     }

+ 3 - 3
Admin.Core.Services/Admin/Permission/Input/PermissionUpdateDotInput.cs

@@ -1,14 +1,14 @@
-namespace Admin.Core.Service.Admin.Permission.Input
+锘縩amespace Admin.Core.Service.Admin.Permission.Input
 {
     public class PermissionUpdateDotInput : PermissionAddDotInput
     {
         /// <summary>
-        /// 权限Id
+        /// 鏉冮檺Id
         /// </summary>
         public long Id { get; set; }
 
         /// <summary>
-        /// 版本
+        /// 鐗堟湰
         /// </summary>
         public long Version { get; set; }
     }

+ 3 - 3
Admin.Core.Services/Admin/Permission/Input/PermissionUpdateGroupInput.cs

@@ -1,15 +1,15 @@
-
+
 namespace Admin.Core.Service.Admin.Permission.Input
 {
     public class PermissionUpdateGroupInput : PermissionAddGroupInput
     {
         /// <summary>
-        /// ȨÏÞId
+        /// ��Id
         /// </summary>
         public long Id { get; set; }
 
         /// <summary>
-        /// °æ±¾
+        /// 版本
         /// </summary>
         public long Version { get; set; }
     }

+ 3 - 3
Admin.Core.Services/Admin/Permission/Input/PermissionUpdateMenuInput.cs

@@ -1,15 +1,15 @@
-
+
 namespace Admin.Core.Service.Admin.Permission.Input
 {
     public class PermissionUpdateMenuInput : PermissionAddMenuInput
     {
         /// <summary>
-        /// ȨÏÞId
+        /// ��Id
         /// </summary>
         public long Id { get; set; }
 
         /// <summary>
-        /// °æ±¾
+        /// 版本
         /// </summary>
         public long Version { get; set; }
     }

+ 2 - 2
Admin.Core.Tests/Admin.Core.Tests.csproj

@@ -7,10 +7,10 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.4" />
+    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.5" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
     <PackageReference Include="xunit" Version="2.4.1" />
-    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>

+ 3 - 3
Admin.Core.Tests/Repository/Admin/RepositoryBaseTest.cs

@@ -16,9 +16,9 @@ namespace Admin.Core.Tests.Service.Repository.Admin
         [Fact]
         public async void GetAsyncByExpression()
         {
-            var id = 1;
-            var user = await _repositoryBase.GetAsync(a => a.Id == id);
-            Assert.Equal(id, user?.Id);
+            var userName = "admin";
+            var user = await _repositoryBase.GetAsync(a => a.UserName == userName);
+            Assert.Equal(userName, user?.UserName);
         }
     }
 }

+ 19 - 1
Admin.Core/Admin.Core.Common.xml

@@ -365,6 +365,11 @@
             Api地址,默认 http://*:8888
             </summary>
         </member>
+        <member name="P:Admin.Core.Common.Configs.AppConfig.CorUrls">
+            <summary>
+            跨域地址,默认 http://*:9999
+            </summary>
+        </member>
         <member name="P:Admin.Core.Common.Configs.AppConfig.Swagger">
             <summary>
             Swagger文档
@@ -420,7 +425,7 @@
             启用
             </summary>
         </member>
-        <member name="P:Admin.Core.Common.Configs.VarifyCodeConfig.Font">
+        <member name="P:Admin.Core.Common.Configs.VarifyCodeConfig.Fonts">
             <summary>
             操作日志
             </summary>
@@ -1006,6 +1011,19 @@
             <param name="ip"></param>
             <returns></returns>
         </member>
+        <member name="M:Admin.Core.Common.Helpers.IPHelper.GetIP(Microsoft.AspNetCore.Http.HttpRequest)">
+            <summary>
+            获得IP地址
+            </summary>
+            <param name="request"></param>
+            <returns></returns>
+        </member>
+        <member name="M:Admin.Core.Common.Helpers.IPHelper.GetMACIp">
+            <summary>
+            获得MAC地址
+            </summary>
+            <returns></returns>
+        </member>
         <member name="T:Admin.Core.Common.Helpers.MD5Encrypt">
             <summary>
             MD5加密

+ 3 - 3
Admin.Core/Admin.Core.csproj

@@ -43,10 +43,10 @@
     <PackageReference Include="AspNetCoreRateLimit" Version="3.0.5" />
     <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
     <PackageReference Include="Autofac.Extras.DynamicProxy" Version="5.0.0" />
-    <PackageReference Include="Caching.CSRedis" Version="3.6.3" />
+    <PackageReference Include="Caching.CSRedis" Version="3.6.5" />
     <PackageReference Include="FluentValidation.AspNetCore" Version="8.6.2" />
-    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.4" />
-    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
+    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.5" />
+    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.5" />
     <PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" />
 

+ 7 - 4
Admin.Core/Controllers/Admin/AuthController.cs

@@ -55,7 +55,7 @@ namespace Admin.Core.Controllers.Admin
             }
 
             var user = output.Data;
-            var token = _userToken.Build(new[]
+            var token = _userToken.Create(new[]
             {
                 new Claim(ClaimAttributes.UserId, user.Id.ToString()),
                 new Claim(ClaimAttributes.UserName, user.UserName),
@@ -156,7 +156,6 @@ namespace Admin.Core.Controllers.Admin
         /// <returns></returns>
         [HttpGet]
         [AllowAnonymous]
-        [NoOprationLog]
         public async Task<IResponseOutput> Refresh([BindRequired] string token)
         {
             var userClaims = _userToken.Decode(token);
@@ -165,7 +164,7 @@ namespace Admin.Core.Controllers.Admin
                 return ResponseOutput.NotOk();
             }
 
-            var refreshExpiresValue = userClaims.FirstOrDefault(a => a.Type == ClaimAttributes.RefreshExpires).Value;
+            var refreshExpiresValue = userClaims.FirstOrDefault(a => a.Type == ClaimAttributes.RefreshExpires)?.Value;
             if (refreshExpiresValue.IsNull())
             {
                 return ResponseOutput.NotOk();
@@ -177,7 +176,11 @@ namespace Admin.Core.Controllers.Admin
                 return ResponseOutput.NotOk("登录信息已过期");
             }
 
-            var userId = userClaims.FirstOrDefault(a => a.Type == ClaimAttributes.UserId).Value;
+            var userId = userClaims.FirstOrDefault(a => a.Type == ClaimAttributes.UserId)?.Value;
+            if (userId.IsNull())
+            {
+                return ResponseOutput.NotOk();
+            }
             var output = await _userServices.GetLoginUserAsync(userId.ToLong());
 
             return GetToken(output);

+ 1 - 1
Admin.Core/Db/ServiceCollectionExtensions.cs

@@ -130,7 +130,7 @@ namespace Admin.Core.Db
             #endregion
             #endregion
 
-            Console.WriteLine($"{appConfig.Urls}\r\n");
+            Console.WriteLine($"{string.Join("\r\n", appConfig.Urls)}\r\n");
         }
     }
 }

+ 3 - 1
Admin.Core/Extensions/UploadConfigApplicationBuilderExtensions.cs

@@ -23,11 +23,13 @@ namespace Admin.Core.Extensions
             });
         }
 
-        public static void UseUploadConfig(this IApplicationBuilder app)
+        public static IApplicationBuilder UseUploadConfig(this IApplicationBuilder app)
         {
             var uploadConfig = app.ApplicationServices.GetRequiredService<IOptions<UploadConfig>>();
             UseFileUploadConfig(app, uploadConfig.Value.Avatar);
             UseFileUploadConfig(app, uploadConfig.Value.Document);
+
+            return app;
         }
     }
 

+ 0 - 1
Admin.Core/Logs/LogHandler.cs

@@ -30,7 +30,6 @@ namespace Admin.Core.Logs
         {
             var sw = new Stopwatch();
             sw.Start();
-
             dynamic actionResult = (await next()).Result;
             sw.Stop();
 

+ 14 - 8
Admin.Core/Startup.cs

@@ -81,9 +81,10 @@ namespace Admin.Core
                 c.AddPolicy("Limit", policy =>
                 {
                     policy
-                    .WithOrigins(_appConfig.Urls)
+                    .WithOrigins(_appConfig.CorUrls)
                     .AllowAnyHeader()
-                    .AllowAnyMethod();
+                    .AllowAnyMethod()
+                    .AllowCredentials();
                 });
 
                 /*
@@ -94,7 +95,8 @@ namespace Admin.Core
                     .AllowAnyOrigin()
                     .SetPreflightMaxAge(new TimeSpan(0, 10, 0))
                     .AllowAnyHeader()
-                    .AllowAnyMethod();
+                    .AllowAnyMethod()
+                    .AllowCredentials();
                 });
                 */
             });
@@ -185,11 +187,14 @@ namespace Admin.Core
             .AddScheme<AuthenticationSchemeOptions, ResponseAuthenticationHandler>(nameof(ResponseAuthenticationHandler), o => { }); ;
             #endregion
 
-            #region 控制器
+            #region 操作日志
             if (_appConfig.Log.Operation)
             {
                 services.AddSingleton<ILogHandler, LogHandler>();
             }
+            #endregion
+
+            #region 控制器
             services.AddControllers(options =>
             {
                 options.Filters.Add<AdminExceptionFilter>();
@@ -231,11 +236,12 @@ namespace Admin.Core
             }
             #endregion
 
-            //IP限流
+            #region IP限流
             if (_appConfig.RateLimit)
             {
                 services.AddIpRateLimit(_configuration, cacheConfig);
-            }
+            } 
+            #endregion
 
             //阻止NLog接收状态消息
             services.Configure<ConsoleLifetimeOptions>(opts => opts.SuppressStatusMessages = true);
@@ -301,13 +307,13 @@ namespace Admin.Core
             //    Console.WriteLine($"{_appConfig.Urls}\r\n");
             //});
 
+            #region app配置
             //IP限流
             if (_appConfig.RateLimit)
             {
                 app.UseIpRateLimiting();
             }
 
-            #region app配置
             //异常
             app.UseExceptionHandler("/Error");
 
@@ -343,7 +349,7 @@ namespace Admin.Core
                     {
                         c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Admin.Core {version}");
                     });
-                    c.RoutePrefix = "";//直接根目录访问
+                    c.RoutePrefix = "";//直接根目录访问,如果是IIS发布可以注释该语句,并打开launchSettings.launchUrl
                     c.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);//折叠Api
                     //c.DefaultModelsExpandDepth(-1);//不显示Models
                 });

+ 5 - 3
Admin.Core/configs/appconfig.json

@@ -1,6 +1,8 @@
 {
   //Api地址
-  "urls": "http://*:8888",
+  "urls": [ "http://*:8888" ],
+  //跨域地址
+  "corUrls": [ "http://*:9999" ],
   //Swagger文档
   "swagger": false,
   //面向切面编程
@@ -19,7 +21,7 @@
   "varifyCode": {
     //启用
     "enable": true,
-    // 字体
-    "font": [ "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" ]
+    //字体列表
+    "fonts": [ "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" ]
   }
 }

+ 3 - 3
Admin.Core/configs/ratelimitconfig.json

@@ -9,7 +9,7 @@
     "RealIpHeader": "X-Real-IP",
     "ClientIdHeader": "X-ClientId",
     "IpWhitelist": [], // "127.0.0.1"
-    "EndpointWhitelist": [], // "get:/api/a", "*:/api/b"
+    "EndpointWhitelist": ["get:/api/admin/auth/refresh"], // "get:/api/a", "*:/api/b"
     "ClientWhitelist": [],
     "HttpStatusCode": 429,
     "QuotaExceededResponse": {
@@ -21,12 +21,12 @@
       {
         "Endpoint": "*",
         "Period": "1s",
-        "Limit": 2
+        "Limit": 3
       },
       {
         "Endpoint": "*",
         "Period": "10m",
-        "Limit": 100
+        "Limit": 200
       }
     ]
   }