Delphi 后端随机生成前端验证码图片程序源代码,开箱即用
Posted sensor_WU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi 后端随机生成前端验证码图片程序源代码,开箱即用相关的知识,希望对你有一定的参考价值。
在前端开发的时候,常用的一个需求就是图形验证码,那么如果后端使用Delphi开发,随机生成一个验证码图片,是如何实现呢?
其实如果是VCL,就是调用TBitMap对象来实现,TBitMap有详细的Canvas操作函数,具体的操作参见Delphi的帮助文档。
直接上源代码如下:
*******************************************************
生成验证码图片: 2023-04-07
作者:sensor(老吴)QQ:910731685
将生成的图片转换成png格式,减少传输数据量。
如果是BMP图片,样本:19KB,转换成png后大小为:3KB
*******************************************************
unit uSZHN_VerifyCode;
interface
uses
System.Classes,
System.SysUtils,
VCL.Graphics,
System.UIConsts,
System.UITypes,
Vcl.ExtCtrls,
// Vcl.Imaging.jpeg,
Vcl.Imaging.pngimage,
System.Types;
type
// 生成验证码组件
TSZHN_GenerateVerifyCode = class
private const
// 定义字典表,不要零(0),因为零和字母O样子太接近
arrStr: array [0 .. 42] of char = (
'1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I',
'J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z','我','爱','中','国','科','技','畅','想');
private
FBitmapWidth: integer; // 图片宽度
FBitmapHeight: integer; // 图片高度
FCodeCount: integer; // 取验证码字符的个数,默认是4个字符
FFontName: string; // 字体名称
FMinFontSize: integer; // 最小字体大小
FRandomLineCount: integer; // 背景随机线条数
FXRandomLen: integer; // X的随机值长度
FYRandomLen: integer; // Y的随机值长度
// 画出验证码函数
function VerifyCodeDrawImg(BMP : TBitmap): string;
public
constructor Create;
procedure GetVerifyCodeAndImage(ImageStream: TStream; var VerifyCode: string);
published
property Width: integer read FBitmapWidth write FBitmapWidth;
property Height: integer read FBitmapHeight write FBitmapHeight;
property CodeCount: integer read FCodeCount write FCodeCount;
property MinFontSize: integer read FMinFontSize write FMinFontSize;
property RandomLineCount: integer read FRandomLineCount write FRandomLineCount;
property XRandomLen: integer read FXRandomLen write FXRandomLen;
property YRandomLen: integer read FYRandomLen write FYRandomLen;
end;
//外部调用函数, ImageStream 参数返回的是生成的验证码图片数据流,结果为验证码字符串
function Create_VerifyCode(ImageStream: TStream): string;
implementation
//外部调用函数, ImageStream 参数返回的是生成的验证码图片数据流,结果为验证码字符串
function Create_VerifyCode(ImageStream: TStream): string;
var
SZHN_GenerateVerifyCode : TSZHN_GenerateVerifyCode;
begin
SZHN_GenerateVerifyCode := TSZHN_GenerateVerifyCode.Create;
try
SZHN_GenerateVerifyCode.GetVerifyCodeAndImage(ImageStream, Result);
finally
SZHN_GenerateVerifyCode.Free;
end;
end;
constructor TSZHN_GenerateVerifyCode.Create();
begin
inherited;
FBitmapWidth := 120;
FBitmapHeight := 40;
FCodeCount := 4;
FMinFontSize := 4;
FRandomLineCount := 10;
FXRandomLen := 5;
FYRandomLen := 4;
end;
// 获取验证码和图片的流数据
procedure TSZHN_GenerateVerifyCode.GetVerifyCodeAndImage(ImageStream: TStream;
var VerifyCode: string);
var
BMP : TBitmap;
PNG : TPNGImage;
begin
BMP := TBitmap.Create;
PNG := TPNGImage.Create;
try
// 默认 宽120,高40
BMP.Width := FBitmapWidth;
BMP.Height:= FBitmapHeight;
//画验证码图,返回的是图形字符串
VerifyCode := VerifyCodeDrawImg(BMP);
//转换成png格式
PNG.Assign(BMP);
PNG.CompressionLevel := 9; //最高压缩率 0..9
PNG.SaveToStream(ImageStream); //保存成 Stream ,以便可以传递给前端(Base64)
finally
FreeAndNil(BMP);
FreeAndNil(PNG);
end;
end;
// 画出验证码函数
function TSZHN_GenerateVerifyCode.VerifyCodeDrawImg(BMP : TBitmap): string;
var
I, j, k: integer;
X, Y, W, H: integer;
vLeft: integer;
strResult: string;
//随机颜色函数
function RandomColor : TColor;
begin
Result := Random($FF) shl 16 + Random($FF) shl 8 + Random($FF);
end;
//随机生成字体名字,内置6种字体随机切换
function Random_FontName: string;
var
FontNames : Tarray<string>;
begin
SetLength(FontNames,6);
FontNames[0] := 'Microsoft JhengHei UI';
FontNames[1] := '宋体';
FontNames[2] := 'Microsoft YaHei UI';
FontNames[3] := 'Ink Free';
FontNames[4] := 'Malgun Gothic';
FontNames[5] := 'Cascadia Code';
Randomize;
Result := FontNames[Random(1000000) mod 6];
end;
begin
// 随机获取字符,默认4个字符
For j := 1 to FCodeCount do
begin
Randomize;
k := Random(1000000) mod 43;
strResult := strResult + trim(arrStr[k]);
end;
vLeft := 5;
//设置背景颜色
BMP.Canvas.Brush.Style := bsSolid;
BMP.Canvas.Brush.Color := RandomColor;
BMP.Canvas.FillRect(TRect.Create(0, 0, FBitmapWidth, FBitmapHeight));
// 随机画10条线
for j := 1 to FRandomLineCount do
begin
Randomize;
BMP.Canvas.Brush.Style := bsCross;
BMP.Canvas.Pen.Color := RandomColor;
BMP.Canvas.Pen.Width := 1;
BMP.Canvas.Pen.Mode := pmXor;
BMP.Canvas.MoveTo(Random(FBitmapWidth), Random(FBitmapHeight));
BMP.Canvas.LineTo(Random(FBitmapWidth), Random(FBitmapHeight));
end;
for I := 1 to length(strResult) do
begin
Randomize;
// 随机字体颜色,这里暂时不用每个字符一个随机颜色
BMP.Canvas.Font.Color := RandomColor;
//设置字体名字
BMP.Canvas.Font.Name := Random_FontName;
// 字体大小
BMP.Canvas.Font.Size := Random(8) + FMinFontSize;
if BMP.Canvas.Font.Size < (FMinFontSize + 5) then
BMP.Canvas.Font.Size := BMP.Canvas.Font.Size + 5;
if Random(2) = 1 then
BMP.Canvas.Font.Style := [TFontStyle.fsBold]
else
BMP.Canvas.Font.Style := [TFontStyle.fsItalic];
begin
X := Random(FXRandomLen) + vLeft;
Y := Random(FYRandomLen);
W := BMP.Canvas.TextWidth(strResult[I]);
H := BMP.Canvas.TextHeight(strResult[I]);
BMP.Canvas.TextOut(X,Y,strResult[I]);
vLeft := X + W + 1;
end;
end;
Result := strResult; // 返回值 ,验证码字符串
end;
end.
具体使用代码:
uses
uSZHN_VerifyCode;
var
sVerifyCode : string; //验证码字符串
TM : TMemoryStream; //验证码图片数据流信息
begin
TM := TMemoryStream.Create;
sVerifyCode := Create_VerifyCode(TM);
TM.SaveToFile('C:\\Temp\\1.png'); //可以保存成文件
Image1.Picture.LoadFromFile('C:\\Temp\\1.png'); //可以直接在Image1中显示出来
TM.Free;
end;
asp.net core配合vue实现后端验证码逻辑
概述
网上的前端验证码逻辑总感觉不安全,验证码建议还是使用后端配合验证。
如果产品确定可以上网的话,就可以使用腾讯,百度等第三方验证,对接方便。但是产品可能内网部署,就必须自己写了。
本文章就是基于这一点来实现的。
前端验证码显示一个图片,后端生成图片。
部分原理
1.前端调用生端获取图片时,传入一个roomID,后端生成一个4位验征码,放入redis中。然后生成一个图片返回。
2.前端显示图片,登录时将roomID和填写的验证码,一并提交,登录接口根据roomId从redis中取出验证码判断是否正确。
这样就相当于后端验证了。
大家觉得有问题什么,可以进行评论。谢谢。
源码
前端部分代码
<template>
<div class="login-container">
<vue-particles
color="#ffffff"
:particleOpacity="0.7"
:particlesNumber="50"
shapeType="circle"
:particleSize="4"
linesColor="#dedede"
:linesWidth="1"
:lineLinked="true"
:lineOpacity="0.4"
:linesDistance="150"
:moveSpeed="2"
:hoverEffect="true"
hoverMode="grab"
:clickEffect="true"
clickMode="push"
></vue-particles>
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
<div class="title-container">
<h3 class="title">智能综合管理系统</h3>
</div>
<el-form-item prop="username">
<span class="svg-container">
<svg-icon icon-class="user" />
</span>
<el-input ref="username" v-model="loginForm.username" placeholder="用户名" name="username" type="text" tabindex="1" autocomplete="on" />
</el-form-item>
<el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
<el-form-item prop="password">
<span class="svg-container">
<svg-icon icon-class="password" />
</span>
<el-input
:key="passwordType"
ref="password"
v-model="loginForm.password"
:type="passwordType"
placeholder="密码"
name="password"
tabindex="2"
autocomplete="on"
@keyup.native="checkCapslock"
@blur="capsTooltip = false"
@keyup.enter.native="handleLogin"
/>
<span class="show-pwd" @click="showPwd">
<svg-icon :icon-class="passwordType === \'password\' ? \'eye\' : \'eye-open\'" />
</span>
</el-form-item>
</el-tooltip>
<el-form-item prop="yzm">
<span class="svg-container">
<svg-icon icon-class="password" />
</span>
<el-input type="text" v-model="loginForm.verifyCode" maxlength="4" placeholder="验证码" />
<div class="identifyCode" @click="refreshCode">
<el-image :src="verifyImageUrl"></el-image>
</div>
</el-form-item>
<el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" @click.native.prevent="handleLogin">登录</el-button>
</el-form>
</div>
</template>
<script>
import { validUsername } from \'@/utils/validate\'
import Identify from \'./components/Identify\'
import { uuid } from \'vue-uuid\'; // uuid object is also exported to things
// outside Vue instance.
export default {
name: \'Login\',
components: { Identify },
data() {
const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) {
callback(new Error(\'请输入正确的用户名\'))
} else {
callback()
}
}
const validatePassword = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error(\'密码最少6位\'))
} else {
callback()
}
}
return {
loginForm: {
username: \'admin\',
password: \'111111\',
roomId: \'\',
verifyCode: \'\'
},
loginRules: {
username: [{ required: true, trigger: \'blur\', validator: validateUsername }],
password: [{ required: true, trigger: \'blur\', validator: validatePassword }]
},
passwordType: \'password\',
capsTooltip: false,
loading: false,
showDialog: false,
redirect: undefined,
otherQuery: {},
identifyCodes: \'1234567890\',
identifyCode: \'\',
// verifyImageRoomId: \'\',
verifyImageUrl: \'\',
}
},
watch: {
$route: {
handler: function (route) {
const query = route.query
if (query) {
this.redirect = query.redirect
this.otherQuery = this.getOtherQuery(query)
}
},
immediate: true
}
},
created() {
// window.addEventListener(\'storage\', this.afterQRScan)
// this.identifyCode = \'\'
// this.makeCode(this.identifyCodes, 4)
this.refreshCode()
},
mounted() {
if (this.loginForm.username === \'\') {
this.$refs.username.focus()
} else if (this.loginForm.password === \'\') {
this.$refs.password.focus()
}
},
destroyed() {
// window.removeEventListener(\'storage\', this.afterQRScan)
},
methods: {
checkCapslock(e) {
const { key } = e
this.capsTooltip = key && key.length === 1 && (key >= \'A\' && key <= \'Z\')
},
showPwd() {
if (this.passwordType === \'password\') {
this.passwordType = \'\'
} else {
this.passwordType = \'password\'
}
this.$nextTick(() => {
this.$refs.password.focus()
})
},
createUuid() {
let uuidV4 = uuid.v4().replace(\'-\', \'\').replace(\'-\', \'\').replace(\'-\', \'\').replace(\'-\', \'\')
this.verifyImageRoomId = uuidV4
this.verifyImageUrl = \'/api/Operator/getCaptchaImage/120/40/\' + this.verifyImageRoomId
console.log(uuidV4)
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.$store.dispatch(\'user/login\', this.loginForm)
.then(() => {
this.$router.push({ path: this.redirect || \'/\', query: this.otherQuery })
this.loading = false
})
.catch(() => {
this.loading = false
})
} else {
console.log(\'error submit!!\')
return false
}
})
},
getOtherQuery(query) {
return Object.keys(query).reduce((acc, cur) => {
if (cur !== \'redirect\') {
acc[cur] = query[cur]
}
return acc
}, {})
},
// 生成随机数
randomNum(min, max) {
return Math.floor(Math.random() * (max - min) + min)
},
// 切换验证码
refreshCode() {
let uuidV4 = uuid.v4().replace(\'-\', \'\').replace(\'-\', \'\').replace(\'-\', \'\').replace(\'-\', \'\')
this.loginForm.roomId = uuidV4
this.verifyImageUrl = \'/api/Operator/getCaptchaImage/120/40/\' + this.loginForm.roomId
console.log(uuidV4)
},
// 生成四位随机验证码
makeCode(o, l) {
for (let i = 0; i < l; i++) {
this.identifyCode += this.identifyCodes[
this.randomNum(0, this.identifyCodes.length)
]
}
console.log(this.identifyCode)
}
}
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
.login-container .el-input input {
color: $cursor;
}
}
/* reset element-ui css */
.login-container {
background: url("~@/assets/background.jpg") no-repeat;
min-height: 100vh;
.el-input {
display: inline-block;
height: 47px;
width: 85%;
input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: $light_gray;
height: 47px;
caret-color: $cursor;
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px $bg inset !important;
-webkit-text-fill-color: $cursor !important;
}
}
}
.el-form-item {
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
color: #454545;
.el-form-item__error {
color: rgb(223, 223, 176);
}
}
}
</style>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
min-height: 100%;
width: 100%;
background-color: $bg;
overflow: hidden;
.login-form {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 520px;
max-width: 100%;
padding: 160px 35px 0;
margin: 0 auto;
overflow: hidden;
}
.tips {
font-size: 14px;
color: #fff;
margin-bottom: 10px;
span {
&:first-of-type {
margin-right: 16px;
}
}
}
.svg-container {
padding: 6px 5px 6px 15px;
color: $dark_gray;
vertical-align: middle;
width: 30px;
display: inline-block;
}
.title-container {
position: relative;
.title {
font-size: 42px;
color: $light_gray;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $dark_gray;
cursor: pointer;
user-select: none;
}
.identifyCode {
position: absolute;
right: 10px;
top: 5px;
}
.thirdparty-button {
position: absolute;
right: 0;
bottom: 6px;
}
@media only screen and (max-width: 470px) {
.thirdparty-button {
display: none;
}
}
}
</style>
后端接口
/// <summary>
/// 获取验证码
/// </summary>
/// <returns></returns>
[HttpGet("getCaptchaImage/{width:int}/{height:int}/{roomId}")]
public IActionResult GetCaptchaImage(int width, int height, string roomId)
{
Console.WriteLine(roomId);
//int width = 100;
//int height = 36;
var captchaCode = Captcha.GenerateCaptchaCode();
var result = Captcha.GenerateCaptchaImage(width, height, captchaCode);
string redisKey = "VerifyCode_" + roomId;
Startup.redisDb.StringSet(redisKey, captchaCode, new TimeSpan(0, 5, 0));
Stream s = new MemoryStream(result.CaptchaByteData);
return new FileStreamResult(s, "image/png");
}
/// <summary>
/// 登录
/// </summary>
/// <returns></returns>
[HttpPost("login")]
public ApiResponseData Login(LoginDto loginInfo)
{
if (string.IsNullOrWhiteSpace(loginInfo.username))
return ApiResponse.Error("用户名不能为空");
if (string.IsNullOrWhiteSpace(loginInfo.password))
return ApiResponse.Error("密码不能为空");
Entity.BaseOperator operInfo = Db
.Select<BaseOperator>()
.Where(a => a.OperatorCode == loginInfo.username && a.Password == loginInfo.password.ToLower() && a.Status == 1 && a.IsDel == false).ToOne();
if (operInfo == null)
return ApiResponse.Error("用户名或者密码不正确");
bool verifyResult = Captcha.ValidateCaptchaCode(loginInfo.RoomId, loginInfo.VerifyCode);
if(verifyResult == false)
return ApiResponse.Error("验证码不正确");
//登录时重新生成token,一个用户只能在一个地方登录
string token = System.Guid.NewGuid().ToString().Replace("-", "");
Db.Update<BaseOperator>(operInfo.OperatorId)
.Set(a => a.Token, token)
.ExecuteAffrows();
dynamic outJson = new ExpandoObject();//初始化一个不包含任何成员的ExpandoObject
outJson.token = token;
//存入redis
string redisKey = "UserInfo_" + token;
Startup.redisDb.StringSet(redisKey, operInfo.OperatorId, new TimeSpan(24, 0, 0));
return ApiResponse.Succ(outJson);
}
以上是关于Delphi 后端随机生成前端验证码图片程序源代码,开箱即用的主要内容,如果未能解决你的问题,请参考以下文章