feat:验证码替换成Lazy.Captcha
This commit is contained in:
parent
f897007688
commit
0b3cf9e0b8
@ -1,8 +1,8 @@
|
|||||||
using Hei.Captcha;
|
using Infrastructure;
|
||||||
using Infrastructure;
|
|
||||||
using Infrastructure.Attribute;
|
using Infrastructure.Attribute;
|
||||||
using Infrastructure.Model;
|
using Infrastructure.Model;
|
||||||
using IPTools.Core;
|
using IPTools.Core;
|
||||||
|
using Lazy.Captcha.Core;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -32,7 +32,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
private readonly ISysMenuService sysMenuService;
|
private readonly ISysMenuService sysMenuService;
|
||||||
private readonly ISysLoginService sysLoginService;
|
private readonly ISysLoginService sysLoginService;
|
||||||
private readonly ISysPermissionService permissionService;
|
private readonly ISysPermissionService permissionService;
|
||||||
private readonly SecurityCodeHelper SecurityCodeHelper;
|
private readonly ICaptcha SecurityCodeHelper;
|
||||||
private readonly ISysConfigService sysConfigService;
|
private readonly ISysConfigService sysConfigService;
|
||||||
private readonly ISysRoleService roleService;
|
private readonly ISysRoleService roleService;
|
||||||
private readonly OptionsSetting jwtSettings;
|
private readonly OptionsSetting jwtSettings;
|
||||||
@ -45,7 +45,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
ISysPermissionService permissionService,
|
ISysPermissionService permissionService,
|
||||||
ISysConfigService configService,
|
ISysConfigService configService,
|
||||||
ISysRoleService sysRoleService,
|
ISysRoleService sysRoleService,
|
||||||
SecurityCodeHelper captcha,
|
ICaptcha captcha,
|
||||||
IOptions<OptionsSetting> jwtSettings)
|
IOptions<OptionsSetting> jwtSettings)
|
||||||
{
|
{
|
||||||
httpContextAccessor = contextAccessor;
|
httpContextAccessor = contextAccessor;
|
||||||
@ -73,7 +73,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
if (loginBody == null) { throw new CustomException("请求参数错误"); }
|
if (loginBody == null) { throw new CustomException("请求参数错误"); }
|
||||||
loginBody.LoginIP = HttpContextExtension.GetClientUserIp(HttpContext);
|
loginBody.LoginIP = HttpContextExtension.GetClientUserIp(HttpContext);
|
||||||
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
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, "验证码错误");
|
return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误");
|
||||||
}
|
}
|
||||||
@ -154,46 +154,12 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
|
|
||||||
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
||||||
var captchaOff = sysConfig?.ConfigValue ?? "0";
|
var captchaOff = sysConfig?.ConfigValue ?? "0";
|
||||||
|
var info = SecurityCodeHelper.Generate(uuid, 60);
|
||||||
var length = AppSettings.GetAppConfig<int>("CaptchaOptions:length", 4);
|
var obj = new { captchaOff, uuid, img = info.Base64 };// File(stream, "image/png")
|
||||||
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")
|
|
||||||
|
|
||||||
return ToJson(1, obj);
|
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>
|
||||||
/// 记录用户登陆信息
|
/// 记录用户登陆信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -232,7 +198,7 @@ namespace ZR.Admin.WebApi.Controllers.System
|
|||||||
return ToResponse(ResultCode.CUSTOM_ERROR, "当前系统没有开启注册功能!");
|
return ToResponse(ResultCode.CUSTOM_ERROR, "当前系统没有开启注册功能!");
|
||||||
}
|
}
|
||||||
SysConfig sysConfig = sysConfigService.GetSysConfigByKey("sys.account.captchaOnOff");
|
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, "验证码错误");
|
return ToResponse(ResultCode.CAPTCHA_ERROR, "验证码错误");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@ using Infrastructure;
|
|||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.AspNetCore.DataProtection;
|
using Microsoft.AspNetCore.DataProtection;
|
||||||
using ZR.Admin.WebApi.Framework;
|
using ZR.Admin.WebApi.Framework;
|
||||||
using Hei.Captcha;
|
|
||||||
using Infrastructure.Extensions;
|
using Infrastructure.Extensions;
|
||||||
using ZR.Admin.WebApi.Extensions;
|
using ZR.Admin.WebApi.Extensions;
|
||||||
using ZR.Admin.WebApi.Filters;
|
using ZR.Admin.WebApi.Filters;
|
||||||
@ -41,7 +40,7 @@ builder.Services.AddSignalR();
|
|||||||
builder.Services.AddDataProtection()
|
builder.Services.AddDataProtection()
|
||||||
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
||||||
//普通验证码
|
//普通验证码
|
||||||
builder.Services.AddHeiCaptcha();
|
builder.Services.AddCaptcha(builder.Configuration);
|
||||||
//builder.Services.AddSession();
|
//builder.Services.AddSession();
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
//绑定整个对象到Model上
|
//绑定整个对象到Model上
|
||||||
|
|||||||
@ -10,10 +10,11 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
|
|||||||
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
|
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
|
||||||
<LastUsedPlatform>Any CPU</LastUsedPlatform>
|
<LastUsedPlatform>Any CPU</LastUsedPlatform>
|
||||||
<PublishProvider>FileSystem</PublishProvider>
|
<PublishProvider>FileSystem</PublishProvider>
|
||||||
<PublishUrl>bin\Release\net6.0\publish\</PublishUrl>
|
<PublishUrl>bin\Release\net7.0\publish\</PublishUrl>
|
||||||
<WebPublishMethod>FileSystem</WebPublishMethod>
|
<WebPublishMethod>FileSystem</WebPublishMethod>
|
||||||
|
<_TargetId>Folder</_TargetId>
|
||||||
<SiteUrlToLaunchAfterPublish />
|
<SiteUrlToLaunchAfterPublish />
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
<ProjectGuid>e5497bb4-b0c1-4794-9fae-163f626ec399</ProjectGuid>
|
<ProjectGuid>e5497bb4-b0c1-4794-9fae-163f626ec399</ProjectGuid>
|
||||||
<SelfContained>false</SelfContained>
|
<SelfContained>false</SelfContained>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
@ -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>
|
|
||||||
@ -16,6 +16,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
|
<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="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
|
||||||
@ -24,11 +25,11 @@
|
|||||||
<PackageReference Include="NLog" Version="5.0.4" />
|
<PackageReference Include="NLog" Version="5.0.4" />
|
||||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.1.4" />
|
<PackageReference Include="NLog.Web.AspNetCore" Version="5.1.4" />
|
||||||
<PackageReference Include="Mapster" Version="7.3.0" />
|
<PackageReference Include="Mapster" Version="7.3.0" />
|
||||||
<PackageReference Include="Hei.Captcha" Version="0.3.0" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Controllers\" />
|
<Folder Include="Controllers\" />
|
||||||
|
<Folder Include="Properties\PublishProfiles\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -103,6 +103,6 @@
|
|||||||
},
|
},
|
||||||
//验证码配置
|
//验证码配置
|
||||||
"CaptchaOptions": {
|
"CaptchaOptions": {
|
||||||
"length": 4//验证码长度
|
"IgnoreCase": true // 比较时是否忽略大小写
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user