feat: 滑块验证码增加服务器端验证

This commit is contained in:
Argo Zhang 2019-08-27 12:06:06 +08:00
parent 1b7fec25e4
commit 17914936dc
4 changed files with 139 additions and 84 deletions

View File

@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Client.Controllers.Api
{
/// <summary>
/// Gitee 网站信息接口类
/// </summary>
[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class CaptchaController : ControllerBase
{
/// <summary>
/// 服务器端滑块验证方法
/// </summary>
/// <returns></returns>
[HttpPost]
public bool Post([FromBody]List<int> datas)
{
var sum = datas.Sum();
var avg = sum * 1.0 / datas.Count;
var stddev = datas.Select(v => Math.Pow(v - avg, 2)).Sum() / datas.Count;
return stddev != 0;
}
}
}

View File

@ -15,7 +15,8 @@
that.sliderCaptcha('reset'); that.sliderCaptcha('reset');
$('.userinfo .dropdown-menu a:first')[0].click(); $('.userinfo .dropdown-menu a:first')[0].click();
}, 1000); }, 1000);
} },
remoteUrl: $.formatUrl('api/Captcha')
}); });
$('#btnCaptcha').on('click', function () { $('#btnCaptcha').on('click', function () {

View File

@ -24,7 +24,24 @@
maxLoadCount: 3, maxLoadCount: 3,
localImages: function () { localImages: function () {
return 'images/Pic' + Math.round(Math.random() * 4) + '.jpg'; return 'images/Pic' + Math.round(Math.random() * 4) + '.jpg';
},
verify: function (arr, url) {
var ret = false;
$.ajax({
url: url,
data: JSON.stringify(arr),
async: false,
cache: false,
type: 'POST',
contentType: 'application/json',
dataType: 'json',
success: function (result) {
ret = result;
} }
});
return ret;
},
remoteUrl: null
}; };
function Plugin(option) { function Plugin(option) {
@ -273,16 +290,23 @@
}; };
_proto.verify = function () { _proto.verify = function () {
var arr = this.trail; // 拖动时y轴的移动距离
var left = parseInt(this.block.style.left);
var verified = false;
if (this.options.remoteUrl !== null) {
verified = this.options.verify(arr, this.options.remoteUrl);
}
else {
var sum = function (x, y) { return x + y; }; var sum = function (x, y) { return x + y; };
var square = function (x) { return x * x; }; var square = function (x) { return x * x; };
var arr = this.trail; // 拖动时y轴的移动距离
var average = arr.reduce(sum) / arr.length; var average = arr.reduce(sum) / arr.length;
var deviations = arr.map(function (x) { return x - average; }); var deviations = arr.map(function (x) { return x - average; });
var stddev = Math.sqrt(deviations.map(square).reduce(sum) / arr.length); var stddev = Math.sqrt(deviations.map(square).reduce(sum) / arr.length);
var left = parseInt(this.block.style.left); verified = stddev !== 0;
}
return { return {
spliced: Math.abs(left - this.x) < this.options.offset, spliced: Math.abs(left - this.x) < this.options.offset,
verified: stddev !== 0 // 简单验证下拖动轨迹为零时表示Y轴上下没有波动可能非人为操作 verified: verified
}; };
}; };

View File

@ -68,6 +68,7 @@ body {
.sliderContainer_fail .sliderIcon:before { .sliderContainer_fail .sliderIcon:before {
content: "\f00d"; content: "\f00d";
} }
.sliderContainer_active .sliderText, .sliderContainer_success .sliderText, .sliderContainer_fail .sliderText { .sliderContainer_active .sliderText, .sliderContainer_success .sliderText, .sliderContainer_fail .sliderText {
display: none; display: none;
} }
@ -111,7 +112,6 @@ body {
} }
.sliderIcon { .sliderIcon {
} }
.refreshIcon { .refreshIcon {