From 0b3cf9e0b80887195743665c5964f02e60637d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Wed, 15 Mar 2023 16:00:16 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=88=90Lazy.Captcha?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/System/SysLoginController.cs | 52 ++++--------------- ZR.Admin.WebApi/Program.cs | 3 +- .../PublishProfiles/FolderProfile.pubxml | 5 +- .../PublishProfiles/FolderProfile1.pubxml | 16 ------ ZR.Admin.WebApi/ZR.Admin.WebApi.csproj | 3 +- ZR.Admin.WebApi/appsettings.json | 2 +- 6 files changed, 16 insertions(+), 65 deletions(-) delete mode 100644 ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile1.pubxml diff --git a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs index 4ec9336..a64ec93 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysLoginController.cs @@ -1,8 +1,8 @@ -using Hei.Captcha; -using Infrastructure; +using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Model; using IPTools.Core; +using Lazy.Captcha.Core; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -32,7 +32,7 @@ namespace ZR.Admin.WebApi.Controllers.System private readonly ISysMenuService sysMenuService; private readonly ISysLoginService sysLoginService; private readonly ISysPermissionService permissionService; - private readonly SecurityCodeHelper SecurityCodeHelper; + private readonly ICaptcha SecurityCodeHelper; private readonly ISysConfigService sysConfigService; private readonly ISysRoleService roleService; private readonly OptionsSetting jwtSettings; @@ -45,7 +45,7 @@ namespace ZR.Admin.WebApi.Controllers.System ISysPermissionService permissionService, ISysConfigService configService, ISysRoleService sysRoleService, - SecurityCodeHelper captcha, + ICaptcha captcha, IOptions jwtSettings) { httpContextAccessor = contextAccessor; @@ -73,7 +73,7 @@ namespace ZR.Admin.WebApi.Controllers.System if (loginBody == null) { throw new CustomException("请求参数错误"); } loginBody.LoginIP = HttpContextExtension.GetClientUserIp(HttpContext); SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff"); - if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(loginBody.Uuid) is string str && !str.ToLower().Equals(loginBody.Code.ToLower())) + if (sysConfig?.ConfigValue != "off" && SecurityCodeHelper.Validate(loginBody.Uuid, loginBody.Code)) { return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误"); } @@ -154,46 +154,12 @@ namespace ZR.Admin.WebApi.Controllers.System SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff"); var captchaOff = sysConfig?.ConfigValue ?? "0"; - - var length = AppSettings.GetAppConfig("CaptchaOptions:length", 4); - var code = SecurityCodeHelper.GetRandomEnDigitalText(length); - byte[] imgByte = GenerateCaptcha(captchaOff, code); - string base64Str = Convert.ToBase64String(imgByte); - CacheHelper.SetCache(uuid, code); - var obj = new { captchaOff, uuid, img = base64Str };// File(stream, "image/png") + var info = SecurityCodeHelper.Generate(uuid, 60); + var obj = new { captchaOff, uuid, img = info.Base64 };// File(stream, "image/png") return ToJson(1, obj); } - /// - /// 生成图片验证码 - /// - /// - /// - /// - private byte[] GenerateCaptcha(string captchaOff, string code) - { - byte[] imgByte; - if (captchaOff == "1") - { - imgByte = SecurityCodeHelper.GetGifEnDigitalCodeByte(code);//动态gif数字字母 - } - else if (captchaOff == "2") - { - imgByte = SecurityCodeHelper.GetGifBubbleCodeByte(code);//动态gif泡泡 - } - else if (captchaOff == "3") - { - imgByte = SecurityCodeHelper.GetBubbleCodeByte(code);//泡泡 - } - else - { - imgByte = SecurityCodeHelper.GetEnDigitalCodeByte(code);//英文字母加数字 - } - - return imgByte; - } - /// /// 记录用户登陆信息 /// @@ -212,7 +178,7 @@ namespace ZR.Admin.WebApi.Controllers.System UserName = context.GetName(), LoginLocation = ip_info?.Province + "-" + ip_info?.City }; - + return sysLogininfor; } @@ -232,7 +198,7 @@ namespace ZR.Admin.WebApi.Controllers.System return ToResponse(ResultCode.CUSTOM_ERROR, "当前系统没有开启注册功能!"); } SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff"); - if (sysConfig?.ConfigValue != "off" && CacheHelper.Get(dto.Uuid) is string str && !str.ToLower().Equals(dto.Code.ToLower())) + if (sysConfig?.ConfigValue != "off" && SecurityCodeHelper.Validate(dto.Uuid, dto.Code)) { return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误"); } diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index 0c6d343..1ea9a5a 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -2,7 +2,6 @@ using Infrastructure; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.DataProtection; using ZR.Admin.WebApi.Framework; -using Hei.Captcha; using Infrastructure.Extensions; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; @@ -41,7 +40,7 @@ builder.Services.AddSignalR(); builder.Services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection")); //普通验证码 -builder.Services.AddHeiCaptcha(); +builder.Services.AddCaptcha(builder.Configuration); //builder.Services.AddSession(); builder.Services.AddHttpContextAccessor(); //绑定整个对象到Model上 diff --git a/ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml b/ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml index 5dc798d..426a9b5 100644 --- a/ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml +++ b/ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile.pubxml @@ -10,10 +10,11 @@ https://go.microsoft.com/fwlink/?LinkID=208121. Release Any CPU FileSystem - bin\Release\net6.0\publish\ + bin\Release\net7.0\publish\ FileSystem + <_TargetId>Folder - net6.0 + net7.0 e5497bb4-b0c1-4794-9fae-163f626ec399 false diff --git a/ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile1.pubxml b/ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile1.pubxml deleted file mode 100644 index d0d2ce9..0000000 --- a/ZR.Admin.WebApi/Properties/PublishProfiles/FolderProfile1.pubxml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - False - False - True - Release - Any CPU - FileSystem - bin\Release\net6.0\publish\ - FileSystem - - \ No newline at end of file diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj index 41f2f0d..57d11fa 100644 --- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj +++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj @@ -16,6 +16,7 @@ + @@ -24,11 +25,11 @@ - + diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index c2f2870..57a973d 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -103,6 +103,6 @@ }, //验证码配置 "CaptchaOptions": { - "length": 4//验证码长度 + "IgnoreCase": true // 比较时是否忽略大小写 } }