🚀 feat(新增): 添加验证码使用示例和配置内容,删除不必要内容配置
This commit is contained in:
parent
08762564cc
commit
37ec110ceb
|
@ -1,5 +1,9 @@
|
|||
namespace Bunny.Dao.Result.Constant;
|
||||
|
||||
public class ErrorConstant
|
||||
/// <summary>
|
||||
/// 发生时的错误敞亮
|
||||
/// </summary>
|
||||
public static class ErrorConstant
|
||||
{
|
||||
public static readonly string ValidateCodeError = "输入验证码错误";
|
||||
}
|
|
@ -7,7 +7,8 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Fleck" Version="1.2.0" />
|
||||
<PackageReference Include="Fleck" Version="1.2.0"/>
|
||||
<PackageReference Include="Lazy.Captcha.Core" Version="2.0.8"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -15,7 +16,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="IRepository\UserRepository\" />
|
||||
<Folder Include="IRepository\UserRepository\"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,87 +1,65 @@
|
|||
using Bunny.Common;
|
||||
using Bunny.Common.Connect;
|
||||
using Bunny.Service.WebSocket;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Bunny.WebApi.Config;
|
||||
|
||||
public class BaseConfig
|
||||
public class BaseConfig(WebApplicationBuilder builder)
|
||||
{
|
||||
/// <summary>
|
||||
/// 只要Controller和Service放在 WebApi 项目下即可完成自动注入
|
||||
/// 遵守这个规则不用再启动类上注入IOC
|
||||
/// 遵守这个规则在Controller可以使用属性方式注入Service
|
||||
/// 只要写接口和实现、Controller中正常写即可
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
public static void Initialize(WebApplicationBuilder builder)
|
||||
public void Initialize()
|
||||
{
|
||||
Console.Title = "Bunny Template";
|
||||
|
||||
// 注入控制器、服务依赖、Host服务等
|
||||
builder.AddApplicationController();
|
||||
builder.AddApplicationServices();
|
||||
builder.AddApplicationBackendServices();
|
||||
|
||||
// 配置
|
||||
UseConfiguration(builder);
|
||||
|
||||
// 注入服务依赖、Host服务、插件等
|
||||
AddApplicationService();
|
||||
// 使用扩展,第三方如Redis、Minio、SQLSugar
|
||||
UseExtend(builder);
|
||||
|
||||
UseExtend();
|
||||
// 配置跨域
|
||||
UseCors(builder);
|
||||
UseCors();
|
||||
}
|
||||
|
||||
private static void UseConfiguration(WebApplicationBuilder builder)
|
||||
/// <summary>
|
||||
/// 注入控制器、服务依赖、Host服务等
|
||||
/// </summary>
|
||||
private void AddApplicationService()
|
||||
{
|
||||
// 配置日志相关
|
||||
// builder.Logging.AddLog4Net("Config/log4net.config");
|
||||
|
||||
//配置文件
|
||||
// 添加Service服务
|
||||
builder.AddApplicationServices();
|
||||
// 添加后台服务
|
||||
builder.AddApplicationBackendServices();
|
||||
// 添加使用自定义配置文件
|
||||
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
||||
|
||||
// 添加 SignalR
|
||||
builder.Services.AddSignalR();
|
||||
|
||||
// 让控制器实例由容器创建
|
||||
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||
|
||||
// 如果将Service放在 WebApi同级目录下,可以完成Controller自动注入,不需要写构造函数
|
||||
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonDateTimeConverter()));
|
||||
// 添加验证码
|
||||
builder.AddCaptcha();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展服务
|
||||
/// </summary>
|
||||
/// <param name="builder">WebApplicationBuilder</param>
|
||||
private static void UseExtend(WebApplicationBuilder builder)
|
||||
private void UseExtend()
|
||||
{
|
||||
// 设置过滤器
|
||||
FilterConfig.Initialize(builder);
|
||||
|
||||
// 初始化Redis
|
||||
RedisContext.Initial();
|
||||
|
||||
// 初始化 Knife4Net文档
|
||||
// 初始化 Knife4Net 文档
|
||||
Knife4Net.Initialize(builder);
|
||||
|
||||
// 启动webSocket
|
||||
// 启动 webSocket
|
||||
WebSocketInitial.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置跨域
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
private static void UseCors(IHostApplicationBuilder builder)
|
||||
private void UseCors()
|
||||
{
|
||||
// SignalR 配置跨域
|
||||
builder.Services.AddSignalRCore();
|
||||
|
||||
// 配置跨域
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
using Lazy.Captcha.Core;
|
||||
using Lazy.Captcha.Core.Generator;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Bunny.WebApi.Config;
|
||||
|
||||
public static class PluginConfig
|
||||
{
|
||||
public static void AddCaptcha(this WebApplicationBuilder builder)
|
||||
{
|
||||
var service = builder.Services;
|
||||
// 使用图形验证码
|
||||
service.AddDistributedMemoryCache();
|
||||
// 验证码相关配置内容
|
||||
service.AddCaptcha(options =>
|
||||
{
|
||||
options.CaptchaType = CaptchaType.DEFAULT; // 验证码类型
|
||||
options.CodeLength = 4; // 验证码长度
|
||||
options.ExpirySeconds = 60; // 过期时间(单位/秒)
|
||||
options.IgnoreCase = true; // 比较忽略大小写
|
||||
options.ImageOption.Animation = true; // 是否启用动画
|
||||
options.ImageOption.Width = 130; // 验证码宽度
|
||||
options.ImageOption.Height = 48; // 验证码高度
|
||||
options.ImageOption.BackgroundColor = SKColors.White;
|
||||
options.ImageOption.BubbleCount = 6; // 气泡数量
|
||||
options.ImageOption.BubbleMinRadius = 2; // 气泡最小半径
|
||||
options.ImageOption.BubbleMaxRadius = 6; // 气泡最大半径
|
||||
options.ImageOption.BubbleThickness = 2; // 气泡边沿厚度
|
||||
options.ImageOption.InterferenceLineCount = 2; // 干扰线数量
|
||||
options.ImageOption.FontSize = 36; // 字体大小
|
||||
options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; // 字体,中文使用kaiti,其他字符可根据喜好设置
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,22 +1,14 @@
|
|||
using Bunny.Common.Connect;
|
||||
using Bunny.Service.IService;
|
||||
using Bunny.Service.IService.Service;
|
||||
using Bunny.WebApi.Controllers;
|
||||
|
||||
namespace Bunny.WebApi.Config;
|
||||
|
||||
public static class ServiceRegistration
|
||||
{
|
||||
public static void AddApplicationController(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddScoped<IndexController>();
|
||||
builder.Services.AddScoped<TemplateController>();
|
||||
builder.Services.AddScoped<BlogController>();
|
||||
builder.Services.AddScoped<RedisOptionController>();
|
||||
}
|
||||
|
||||
public static void AddApplicationServices(this WebApplicationBuilder builder)
|
||||
{
|
||||
// 注入EfCore上下文对象
|
||||
builder.Services.AddScoped<EfCoreContext>();
|
||||
|
||||
// 注入Service服务
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
using Bunny.Dao.Result;
|
||||
using Bunny.Dao.Result.Constant;
|
||||
using Lazy.Captcha.Core;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bunny.WebApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 验证码测试
|
||||
/// </summary>
|
||||
[Route("/api/[controller]/[action]")]
|
||||
public class CaptchaTestController(ICaptcha _captcha) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 测试测试验证码生成
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public IActionResult GetCaptcha(string id)
|
||||
{
|
||||
var captchaData = _captcha.Generate(id);
|
||||
// 验证码的ID
|
||||
var captchaDataId = captchaData.Id;
|
||||
// 验证码的代码
|
||||
var captchaDataCode = captchaData.Code;
|
||||
|
||||
Console.WriteLine($"验证码的ID:{captchaDataId};验证码的代码:{captchaDataCode}");
|
||||
|
||||
// 返回验证码图片内容
|
||||
var stream = new MemoryStream(captchaData.Bytes);
|
||||
return File(stream, "image/gif");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 输入验证码机型校验
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public Result<string> CheckCode(string id, string code)
|
||||
{
|
||||
// 校验用户输入的验证码是否=正确
|
||||
var validate = _captcha.Validate(id, code);
|
||||
return validate ? Result<string>.Success() : Result<string>.Error(ErrorConstant.ValidateCodeError);
|
||||
}
|
||||
}
|
|
@ -7,9 +7,12 @@ public class Program
|
|||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.Title = "Bunny Template";
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
// 其它配置
|
||||
BaseConfig.Initialize(builder);
|
||||
|
||||
// 基础配置
|
||||
var baseConfig = new BaseConfig(builder);
|
||||
baseConfig.Initialize();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
@ -20,7 +23,7 @@ public class Program
|
|||
|
||||
app.UseKnife4UI(options =>
|
||||
{
|
||||
options.RoutePrefix = "bunnyDocs"; // serve the UI at root
|
||||
options.RoutePrefix = "bunnyDocs"; // 后端服务文档基础路径
|
||||
options.DocumentTitle = "Bunny 文档";
|
||||
options.SwaggerEndpoint("//swagger/v1/swagger.json", "Bunny 文档");
|
||||
});
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
"Audience": "Audience"
|
||||
},
|
||||
"Redis": {
|
||||
"Host": "47.120.65.66",
|
||||
"Host": "192.168.3.98",
|
||||
"Port": "6379",
|
||||
"Password": "02120212",
|
||||
"Password": "123456",
|
||||
"DefaultDB": 6,
|
||||
"AsyncTimeout": 300
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue