All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.luues.tool.captcha.slide.SlideCaptchaUtil Maven / Gradle / Ivy

There is a newer version: 2.0.1.RELEASE
Show newest version
package cn.luues.tool.captcha.slide;

import java.util.ArrayList;
import java.util.List;

public class SlideCaptchaUtil {


    /*
    controller类
    @RequestMapping("/verify.htm")
    @ResponseBody
    public boolean verify(@RequestParam(value = "datas") String datazz){
        return SlideCaptchaUtil.verify(datazz);
    }*/

    /**
     * 服务端验证的返回结果怎么一直都是true
     * 示例代码中演示的是前端提交用户滑动轨迹到服务器端进行了 Y 轴的平方差校验,
     * 为零时才返回 false,否则返回 true,为 true 表示 Y 轴有偏移,
     * 简单的认为此操作是人为操作,因为人手拖动过程中的抖动 Y 轴理论上是不可能没有偏移的。因此依据此值进行是否是人为拖动滑块
     * @param datazz
     * @return
     */
    public static boolean verify(String datazz){
        List datas = new ArrayList<>();
        for (int i = 0; i < datazz.length(); i++) {
            char c = datazz.charAt(i);
            if (c >= '0' && c <= '9') {
                datas.add(Integer.valueOf(String.valueOf(c)));
            }
        }
        int sum = 0;
        for (Integer data : datas) {
            sum += data;
        }
        double avg = sum * 1.0 / datas.size();
        double sum2 = 0.0;
        for (Integer data : datas) {
            sum2 += Math.pow(data - avg, 2);
        }
        double stddev = sum2 / datas.size();
        return stddev != 0;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy