Browse Source

新增验证码服务,使用第三方组件lazy-slide-captcha生成滑块验证码
缓存默认新增分布式内存缓存或分布式redis缓存,移除ip限流配置的分布式redis缓存

zhontai 2 years ago
parent
commit
cab69f0e0c

+ 13 - 1
src/hosts/ZhonTai.Host/Program.cs

@@ -1,7 +1,11 @@
-using FreeScheduler;
+using Autofac.Core;
+using FreeRedis;
+using FreeScheduler;
+using Microsoft.Extensions.Caching.Distributed;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.DependencyModel;
 using Microsoft.Extensions.Hosting;
+using Newtonsoft.Json;
 using Savorboard.CAP.InMemoryMessageQueue;
 using System.Linq;
 using System.Reflection;
@@ -10,6 +14,7 @@ using ZhonTai.Admin.Core;
 using ZhonTai.Admin.Core.Configs;
 using ZhonTai.Admin.Core.Consts;
 using ZhonTai.Admin.Core.Startup;
+using ZhonTai.Admin.Tools.Cache;
 using ZhonTai.Admin.Tools.TaskScheduler;
 using ZhonTai.ApiUI;
 using ZhonTai.Common.Helpers;
@@ -54,7 +59,14 @@ new HostApp(new HostAppOptions
             options.TaskHandler = new TaskHandler(options.FreeSql);
         });
 
+        //oss文件上传
         //context.Services.AddOSS();
+
+        //滑块验证码
+        context.Services.AddSlideCaptcha(context.Configuration, options =>
+        {
+            options.StoreageKeyPrefix = CacheKeys.Captcha;
+        });
     },
 
     //配置Autofac容器

+ 41 - 1
src/hosts/ZhonTai.Host/appsettings.json

@@ -5,5 +5,45 @@
       "Microsoft.AspNetCore": "Warning"
     }
   },
-  "AllowedHosts": "*"
+  "AllowedHosts": "*",
+  "SlideCaptcha": {
+    "ExpirySeconds": 300, // 缓存过期时长
+    "StoreageKeyPrefix": "admin:captcha:", // 缓存前缀
+    "Tolerant": 0.02, // 容错值(校验时用,缺口位置与实际滑动位置匹配容错范围)
+    "Backgrounds": [ // 背景图配置
+      {
+        "Type": "file",
+        "Data": "wwwroot/captcha/jigsaw/backgrounds/1.jpg"
+      },
+      {
+        "Type": "file",
+        "Data": "wwwroot/captcha/jigsaw/backgrounds/2.jpg"
+      },
+      {
+        "Type": "file",
+        "Data": "wwwroot/captcha/jigsaw/backgrounds/3.jpg"
+      },
+      {
+        "Type": "file",
+        "Data": "wwwroot/captcha/jigsaw/backgrounds/4.jpg"
+      },
+      {
+        "Type": "file",
+        "Data": "wwwroot/captcha/jigsaw/backgrounds/5.jpg"
+      }
+    ],
+    // Templates不配置,则使用默认模板
+    "Templates": [
+      //{
+      //  "Slider": {
+      //    "Type": "file",
+      //    "Data": "wwwroot/captcha/jigsaw/templates/1/transparent.png"
+      //  },
+      //  "Hole": {
+      //    "Type": "file",
+      //    "Data": "wwwroot/captcha/jigsaw/templates/1/dark.png"
+      //  }
+      //}
+    ]
+  }
 }

BIN
src/hosts/ZhonTai.Host/wwwroot/captcha/jigsaw/backgrounds/1.jpg


BIN
src/hosts/ZhonTai.Host/wwwroot/captcha/jigsaw/backgrounds/2.jpg


BIN
src/hosts/ZhonTai.Host/wwwroot/captcha/jigsaw/backgrounds/3.jpg


BIN
src/hosts/ZhonTai.Host/wwwroot/captcha/jigsaw/backgrounds/4.jpg


BIN
src/hosts/ZhonTai.Host/wwwroot/captcha/jigsaw/backgrounds/5.jpg


+ 0 - 12
src/platform/ZhonTai.Admin/Core/Extensions/RateLimitServiceCollectionExtensions.cs

@@ -1,9 +1,6 @@
 using AspNetCoreRateLimit;
-using FreeRedis;
-using Microsoft.Extensions.Caching.Distributed;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
-using Newtonsoft.Json;
 using ZhonTai.Admin.Tools.Cache;
 
 namespace ZhonTai.Admin.Core.Extensions;
@@ -25,19 +22,10 @@ public static class RateLimitServiceCollectionExtensions
 
         if (cacheConfig.TypeRateLimit == CacheType.Redis)
         {
-            //redis
-            var redis = new RedisClient(cacheConfig.Redis.ConnectionStringRateLimit)
-            {
-                Serialize = JsonConvert.SerializeObject,
-                Deserialize = JsonConvert.DeserializeObject
-            };
-            services.AddSingleton<IDistributedCache>(new DistributedCache(redis));
             services.AddDistributedRateLimiting();
         }
         else
         {
-            //内存
-            services.AddMemoryCache();
             services.AddInMemoryRateLimiting();
         }
         services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();

+ 3 - 0
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -57,6 +57,7 @@ using System.Text.Json.Serialization;
 using FreeRedis;
 using HealthChecks.UI.Client;
 using Microsoft.AspNetCore.Diagnostics.HealthChecks;
+using Microsoft.Extensions.Caching.Distributed;
 
 namespace ZhonTai.Admin.Core;
 
@@ -576,10 +577,12 @@ public class HostApp
             };
             services.AddSingleton(redis);
             services.AddSingleton<ICacheTool, RedisCacheTool>();
+            services.AddSingleton<IDistributedCache>(new DistributedCache(redis));
         }
         else
         {
             services.AddMemoryCache();
+            services.AddDistributedMemoryCache();
             services.AddSingleton<ICacheTool, MemoryCacheTool>();
         }
 

+ 48 - 0
src/platform/ZhonTai.Admin/Services/Captcha/CaptchaService.cs

@@ -0,0 +1,48 @@
+using ZhonTai.DynamicApi;
+using ZhonTai.DynamicApi.Attributes;
+using ZhonTai.Admin.Core.Consts;
+using Lazy.SlideCaptcha.Core;
+using Lazy.SlideCaptcha.Core.Validator;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Authorization;
+using ZhonTai.Admin.Core.Attributes;
+
+namespace ZhonTai.Admin.Services.Cache;
+
+/// <summary>
+/// 验证码服务
+/// </summary>
+[Order(210)]
+[DynamicApi(Area = AdminConsts.AreaName)]
+public class CaptchaService : BaseService, IDynamicApi
+{
+    private readonly ICaptcha _captcha;
+    public CaptchaService(ICaptcha captcha)
+    {
+        _captcha = captcha;
+    }
+
+    /// <summary>
+    /// 生成
+    /// </summary>
+    /// <returns></returns>
+    [AllowAnonymous]
+    [NoOprationLog]
+    public CaptchaData Generate()
+    {
+        return _captcha.Generate();
+    }
+
+    /// <summary>
+    /// 验证
+    /// </summary>
+    /// <param name="id"></param>
+    /// <param name="track"></param>
+    /// <returns></returns>
+    [AllowAnonymous]
+    [NoOprationLog]
+    public ValidateResult CheckAsync([FromQuery] string id, SlideTrack track)
+    {
+        return _captcha.Validate(id, track);
+    }
+}

+ 1 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.csproj

@@ -23,6 +23,7 @@
 		<PackageReference Include="FreeSql.Repository" Version="3.2.691" />
 		<PackageReference Include="FreeScheduler" Version="1.0.9" />
 		<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
+		<PackageReference Include="Lazy.SlideCaptcha.Core" Version="2.0.0" />
 		<PackageReference Include="Mapster" Version="7.3.0" />
 		<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
 		<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.3" />

+ 19 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -4041,6 +4041,25 @@
             <param name="cacheKey"></param>
             <returns></returns>
         </member>
+        <member name="T:ZhonTai.Admin.Services.Cache.CaptchaService">
+            <summary>
+            验证码服务
+            </summary>
+        </member>
+        <member name="M:ZhonTai.Admin.Services.Cache.CaptchaService.Generate">
+            <summary>
+            生成
+            </summary>
+            <returns></returns>
+        </member>
+        <member name="M:ZhonTai.Admin.Services.Cache.CaptchaService.CheckAsync(System.String,Lazy.SlideCaptcha.Core.Validator.SlideTrack)">
+            <summary>
+            验证
+            </summary>
+            <param name="id"></param>
+            <param name="track"></param>
+            <returns></returns>
+        </member>
         <member name="T:ZhonTai.Admin.Services.DictionaryType.DictionaryTypeService">
             <summary>
             数据字典类型服务