当前位置: 首页 > news >正文

打对钩的方式做人机验证(vue+javascript)

要实现一个通过打对钩方式的人机验证,并且让它不容易被破解,可以考虑以下几点:

  1. 动态生成选项和题目:每次生成的验证选项和题目都不一样,防止简单的脚本通过固定的答案绕过验证。
  2. 使用图像和文字混合验证:增加复杂度,避免简单的 OCR(光学字符识别)或者脚本直接解析。
  3. 添加时间限制和次数限制:防止暴力破解,通过限制每次验证的时间和次数,增加破解的难度。
  4. 后端验证:所有验证结果在后端进行校验,防止前端被篡改。
  5. 使用随机噪音和干扰:在选项中加入一些随机噪音和干扰,防止机器学习模型直接识别。

下面是一个简单的示例,展示如何使用 Vue.js 和 Element UI 创建一个打对钩的人机验证页面。

Vue.js 和 Element UI 示例

首先,确保你已经安装了 Vue.js 和 Element UI。

安装 Vue.js 和 Element UI

npm install vue
npm install element-ui

创建一个 Vue 组件用于人机验证

<template><div><el-card><div slot="header" class="clearfix"><span>人机验证</span></div><div v-if="!isVerified"><p>{{ question }}</p><el-checkbox-group v-model="checkedOptions"><el-checkbox v-for="option in options" :key="option.id" :label="option.text">{{ option.text }}</el-checkbox></el-checkbox-group><el-button type="primary" @click="verify">验证</el-button></div><div v-else><p>验证成功</p></div></el-card></div>
</template><script>
export default {data() {return {isVerified: false,question: '',options: [],checkedOptions: [],correctOptions: []};},created() {this.generateQuestion();},methods: {generateQuestion() {// 在实际应用中,这些数据应从后端获取const questionData = {question: '请选择所有水果',options: [{ id: 1, text: '苹果', isCorrect: true },{ id: 2, text: '香蕉', isCorrect: true },{ id: 3, text: '汽车', isCorrect: false },{ id: 4, text: '猫', isCorrect: false },]};this.question = questionData.question;this.options = questionData.options;this.correctOptions = questionData.options.filter(option => option.isCorrect).map(option => option.text);},verify() {const selectedCorrectOptions = this.checkedOptions.filter(option => this.correctOptions.includes(option));if (selectedCorrectOptions.length === this.correctOptions.length && this.checkedOptions.length === this.correctOptions.length) {this.isVerified = true;} else {this.$message.error('验证失败,请重试');this.generateQuestion();this.checkedOptions = [];}}}
};
</script><style scoped>
.el-card {width: 300px;margin: 50px auto;text-align: center;
}
</style>

后端支持

为了确保安全性,验证的逻辑应在后端进行。这里是一个简单的后端逻辑示例,假设你使用 Node.js 和 Express:

安装必要的包

npm install express body-parser

后端代码

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;app.use(bodyParser.json());const questions = [{question: '请选择所有水果',options: [{ id: 1, text: '苹果', isCorrect: true },{ id: 2, text: '香蕉', isCorrect: true },{ id: 3, text: '汽车', isCorrect: false },{ id: 4, text: '猫', isCorrect: false },]},// 更多问题
];app.get('/captcha-question', (req, res) => {const randomIndex = Math.floor(Math.random() * questions.length);res.json(questions[randomIndex]);
});app.post('/verify-captcha', (req, res) => {const { question, selectedOptions } = req.body;const correctQuestion = questions.find(q => q.question === question);if (!correctQuestion) {return res.status(400).json({ success: false, message: '无效的问题' });}const correctOptions = correctQuestion.options.filter(option => option.isCorrect).map(option => option.text);if (selectedOptions.length !== correctOptions.length) {return res.json({ success: false, message: '验证失败' });}const isCorrect = selectedOptions.every(option => correctOptions.includes(option));if (isCorrect) {res.json({ success: true });} else {res.json({ success: false, message: '验证失败' });}
});app.listen(port, () => {console.log(`Server is running at http://localhost:${port}`);
});

总结

以上示例展示了一个基础的人机验证流程,其中包含前端 Vue.js 组件和后端验证逻辑。通过动态生成问题和选项,以及在后端进行验证,可以有效地增加人机验证的难度,从而防止简单的脚本破解。

实际应用中,还可以结合其他安全机制,如使用 HTTPS、增加图像和文字混合验证、增加随机噪音等,进一步提高验证的安全性。

http://www.lryc.cn/news/364856.html

相关文章:

  • 可视化脚本用于使用MMDetection库进行图像的目标检测
  • React-组件通信
  • 低代码选型要注意什么问题?
  • hive切换spark引擎倒入数据乱码
  • fpga入门 串口定时1秒发送1字节
  • 总结一下自己,最近三年,我做了哪些工作
  • SpringCloud Gateway基础入门与使用实践总结
  • TensorBoard在pytorch训练过程中如何使用,及数据读取问题解决方法
  • 【Vue】普通组件的注册使用-全局注册
  • 爬虫之反爬思路与解决手段
  • 2.1.4 采用配置类与注解方式使用MyBatis
  • 微信小程序云开发实现利用云函数将数据库表的数据导出到excel中
  • python 字符串(str)、列表(list)、元组(tuple)、字典(dict)
  • 【源码】SpringBoot事务注册原理
  • 技巧:合并ZIP分卷压缩包
  • 数据挖掘 | 实验三 决策树分类算法
  • Python机器学习预测区间估计工具库之mapie使用详解
  • Linux基础指令磁盘管理002
  • Python怎么添加库:深入解析与操作指南
  • Python | 虚拟环境的增删改查
  • 【MySQL数据库】:MySQL内外连接
  • C# FTP/SFTP 详解及连接 FTP/SFTP 方式示例汇总
  • 二、【源码】实现映射器的注册和使用
  • Android Compose 十:常用组件列表 监听
  • Wireshark 如何查找包含特定数据的数据帧
  • 【深度学习入门篇一】阿里云服务器(不需要配环境直接上手跟学代码)
  • app,waf笔记
  • 数据仓库之维度建模
  • 解决远程服务器连接报错
  • 通过电脑查看Wi-Fi密码的方法,提供三种方式