網(wǎng)站首頁(yè) 編程語言 正文
介紹
LazyCaptcha是仿EasyCaptcha和SimpleCaptcha,基于.Net Standard 2.1的圖形驗(yàn)證碼模塊。
一. 自定義隨機(jī)驗(yàn)證碼(需要版本1.1.2)
這里隨機(jī)是指CaptchaType隨機(jī),動(dòng)靜隨機(jī)等等,你可以設(shè)置CaptchaOptions任意選項(xiàng)值。每次刷新驗(yàn)證碼,效果如下:
我也不知道這種需求是否真實(shí)存在。
1. 自定義RandomCaptcha
////// 隨機(jī)驗(yàn)證碼 /// public class RandomCaptcha : DefaultCaptcha { private static readonly Random random = new(); private static readonly CaptchaType[] captchaTypes = Enum.GetValues(); public RandomCaptcha(IOptionsSnapshot options, IStorage storage) : base(options, storage) { } /// /// 更新選項(xiàng) /// /// protected override void ChangeOptions(CaptchaOptions options) // 隨機(jī)驗(yàn)證碼類型 options.CaptchaType = captchaTypes[random.Next(0, captchaTypes.Length)]; // 當(dāng)是算數(shù)運(yùn)算時(shí),CodeLength是指運(yùn)算數(shù)個(gè)數(shù) if (options.CaptchaType.IsArithmetic()) { options.CodeLength = 2; } else options.CodeLength = 4; // 如果包含中文時(shí),使用kaiti字體,否則文字亂碼 if (options.CaptchaType.ContainsChinese()) options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; options.ImageOption.FontSize = 24; options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; // 動(dòng)靜隨機(jī) options.ImageOption.Animation = random.Next(2) == 0; // 干擾線隨機(jī) options.ImageOption.InterferenceLineCount = random.Next(1, 4); // 氣泡隨機(jī) options.ImageOption.BubbleCount = random.Next(1, 4); // 其他選項(xiàng)... }
2. 注入RandomCaptcha
// 內(nèi)存存儲(chǔ), 基于appsettings.json配置 builder.Services.AddCaptcha(builder.Configuration); // 開啟隨機(jī)驗(yàn)證碼 builder.Services.Add(ServiceDescriptor.Scoped());
二. 自定義字體
使用KG HAPPY字體,效果如圖:
1. 尋找字體
你可以通過fontspace找到自己喜愛的字體。
2. 將字體放入項(xiàng)目,并設(shè)置為嵌入資源。
當(dāng)然也可以不作為嵌入資源,放到特定目錄也是可以的,只要對(duì)下邊ResourceFontFamilysFinder稍作修改即可。
3. 定義查找字體幫助類,示例使用ResourceFontFamilysFinder
public class ResourceFontFamilysFinder { private static Lazy> _fontFamilies = new Lazy
>(() => { var fontFamilies = new List
(); var assembly = Assembly.GetExecutingAssembly(); var names = assembly.GetManifestResourceNames(); if (names?.Length > 0 == true) { var fontCollection = new FontCollection(); foreach (var name in names) { if (!name.EndsWith("ttf")) continue; fontFamilies.Add(fontCollection.Add(assembly.GetManifestResourceStream(name))); } } return fontFamilies; }); public static FontFamily Find(string name) return _fontFamilies.Value.First(e => e.Name == name); } }
4. 設(shè)置option
// 內(nèi)存存儲(chǔ), 基于appsettings.json配置 builder.Services.AddCaptcha(builder.Configuration, options => { // 自定義字體 options.ImageOption.FontSize = 28; options.ImageOption.FontFamily = ResourceFontFamilysFinder.Find("KG HAPPY"); // 字體的名字在打開ttf文件時(shí)會(huì)顯示 });
原文鏈接:https://www.cnblogs.com/readafterme/p/16012880.html
相關(guān)推薦
- 2022-10-22 Python構(gòu)建簡(jiǎn)單線性回歸模型_python
- 2023-04-01 路由react-router-dom的基本使用教程_React
- 2022-08-01 GO項(xiàng)目配置與使用的方法步驟_Golang
- 2022-07-21 StreamX 部署 Flink Stream 應(yīng)用
- 2022-01-01 element對(duì)穿梭框?qū)涌诜祷氐臄?shù)據(jù)其他字段進(jìn)行校驗(yàn)多個(gè)校驗(yàn)
- 2022-09-30 LeetCode189輪轉(zhuǎn)數(shù)組python示例_python
- 2023-01-26 詳解Python手寫數(shù)字識(shí)別模型的構(gòu)建與使用_python
- 2022-10-30 解決docker訪問外部https數(shù)字證書問題_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支