feat:验证码替换成Lazy.Captcha

This commit is contained in:
不做码农 2023-03-15 16:00:16 +08:00
parent f897007688
commit 0b3cf9e0b8
6 changed files with 16 additions and 65 deletions

View File

@ -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<OptionsSetting> 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<int>("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);
}
/// <summary>
/// 生成图片验证码
/// </summary>
/// <param name="captchaOff"></param>
/// <param name="code"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 记录用户登陆信息
/// </summary>
@ -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, "验证码错误");
}

View File

@ -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上

View File

@ -10,10 +10,11 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider>
<PublishUrl>bin\Release\net6.0\publish\</PublishUrl>
<PublishUrl>bin\Release\net7.0\publish\</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod>
<_TargetId>Folder</_TargetId>
<SiteUrlToLaunchAfterPublish />
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ProjectGuid>e5497bb4-b0c1-4794-9fae-163f626ec399</ProjectGuid>
<SelfContained>false</SelfContained>
</PropertyGroup>

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<DeleteExistingFiles>False</DeleteExistingFiles>
<ExcludeApp_Data>False</ExcludeApp_Data>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider>
<PublishUrl>bin\Release\net6.0\publish\</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod>
</PropertyGroup>
</Project>

View File

@ -16,6 +16,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
<PackageReference Include="Lazy.Captcha.Core" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
@ -24,11 +25,11 @@
<PackageReference Include="NLog" Version="5.0.4" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.1.4" />
<PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="Hei.Captcha" Version="0.3.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="Properties\PublishProfiles\" />
</ItemGroup>
<ItemGroup>

View File

@ -103,6 +103,6 @@
},
//
"CaptchaOptions": {
"length": 4//
"IgnoreCase": true //
}
}