内容导航:验证码为code的图片,@param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @return 返回图片验证码,@param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @param lineCnt,图片滑动验证码
{image}
一 、往右划的验证码是在验证什么
目前滑动验证码的功能主要还是增加用户的安全 ,验证的是这是人为操作还是机器操作,防止一些不法分子利用自动程序恶意注册登录、暴力破解密码以及批量操作(刷单、发帖)等行为 。如果没有验证码,暴力破解密码后就可以直接登录 ,相反 ,如果设置了验证码,程序很难识别,那么就无法登录成功。除此之外 ,还可以防止黑客恶意攻击从而导致服务器压力太大而崩溃,比如论坛灌水 、刷页 、刷票等,有些论坛回复需要输入验证码就是出于这个目的。
1、识别判断出现的滑动验证码 。
这个步骤并不复杂 ,在编程里有很多这样的调试工具,根据实际应用场景选择合适的专业工具 。然后利用一段获取图片的代码自动获取到带有滑动验证码的图片的信息。最后通过相应代码获取它们的网络地址,并将其下载并保存到本地。
2、确定滑动验证码的滑动位置 ,如果是那种带有缺口位置的验证码,就需要拖动拼合滑块才能完成验证 。
将保存在本地的带有滑动验证码的图片调出来然后分析图片中的相关信息。这个步骤,就需要用到图像匹配技术。通俗的讲 ,就是用相应的原生代码进行图像匹配识别以及坐标定位 。
3 、根据上述收集到相关信息计算滑块移动轨迹并模拟人移动滑块的整个过程。
计算出了滑块移动的轨迹图,接下来就需要模拟人移动基凳态滑块的过程。现在的滑动验证码识别安全性很高,绝大多数都加入了机器学习 模型 ,也粗厅就是说 ,人在移动滑块的时候不可能是匀速的 。**** 所以说,只是简单的匀速滑动肯定是被认定为机器操作。因此搏源模拟人拖动滑块的过程中需要加入几个过程,比如先加速 ,再减速,适当加入回退和随机抖动,尽可能的模拟人的行为。滑动的过程可以通过在代码中设置相应变量 ,再加上相应公式就可以解决,对于专业人员并不复杂 。
二、java如何读取网络照片呢
importjava。io。BufferedInputStream;importjava 。io 。BufferedOutputStream;importjava。io。FileOutputStream;importjava 。net。HttpURLConnection;importjava。net 。URL;importjava。net。URLConnection;/ 。读取网络照片,保存到本地**/publicclassGetUrlImg{publicstaticvoidgetUrlImg(StringURLName,Stringtarget)throwsException{//URLName照片地址//target本地地址intHttpResult=0;//服务器返回的状态URLurl=newURL(URLName);//创建URLURLConnectionurlconn=url。openConnection();//试图连接并取得返回状态码urlconn。connect();HttpURLConnectionhttpconn=(HttpURLConnection)urlconn;HttpResult=httpconn 。getResponseCode();System。out。println(HttpResult);if(HttpResult!=HttpURLConnection 。HTTP_OK)//不等于HTTP_OK说明连接不成功System 。out。print("fail");else{intfilesize=urlconn。getContentLength();//取数据长度System 。out。println(filesize);。
三、图片滑动验证码怎样实现
这次给大家带来图片滑动验证码怎样实现 ,图片滑动验证码实现的注意事项有哪些,下面就是实战案例,一起来看一下 。
图片滑动验证码 ,逻辑是根据鼠标滑动轨迹,坐标位置,计算拖动速度等等来判断是否人为操作 ,当然下面的代码只是实现前端部分 ,只记录了拖动的坐标。
先上代码吧,做个备份记录
/
Created by lgy on 2017/10/21.
图片验证码
*/
(function ($) {
$. = function (options) {
//初始化参数
var defaults = {
callback:"" //回调函数
};
var opts = $.extend(defaults, options);
return (function () {
var $this = $(this);//获取当前对象
var html = '
' +
'
'
' +
'
' +
'
' +
'
'' +
'
刷新
' +'' +
'
' +
'
'按住滑块,拖动完成上方拼图' +
'';
$(html);
//定义拖动参数
var $pMove = $(this).find(".code-btn-img"); //拖动按钮
var $pWrap = $(this).find(".code-btn");//鼠标可拖拽区域
var mX = 0, mY = 0;//定义鼠标X轴Y轴
var dX = 0, dY = 0;//定义滑动区域左 、上位置
var isDown = false;//mousedown标记
if(nt) {//ie的事件监听 ,拖拽p时禁止选中内容,firefox与chrome已在css中设置过圆戚御-moz-user-select: none;
-webkit-user-select: none;
$pMove[0].attachEvent('onselectstart', function() {
return false;
});
}
//按钮拖动事件
$({
mousedown: function (e) {
//清除提示信息
$(".code-tip").html("");
var event = e || ;
mX = ;
dX = $().left;
dY = $().top;
isDown = true;//鼠标拖拽启
$(this).addClass("active");
//修改按钮阴影
$({"box-shadow"仔皮:"0 0 8px #666"});
}
});
//鼠标点击松手事件
$(document).mouseup(function (e) {
var lastX = $(".code-mask").offset().left - dX - 1;
isDown = false;//鼠标拖拽启
$ss("active");
//还原按钮阴影
$({"box-shadow":"0 0 3px #ccc"});
checkcode(lastX);
});
//滑动事件
$(function (event) {
var event = event || ;
var x = ;//鼠标滑动时的X轴
if (isDown) {
if(x>(dX+30) && x<dX+$(this).width()-20){
$({"left": (x - dX - 20) + "px"});//p动态位置赋值
$(".code-mask").css({"left": (x - dX-30) + "px"});
}
}
});
//验证数据
function checkcode(code){
var iscur=false;
//模拟ajax
setTimeout(function(){
if(iscur){
checkcoderesult(1,"验证通过");
$(".code-k-p").hide();
({code:1000,msg:"验证通过",msgcode:"23dfdf123"});
}else{
$("error");
checkcoderesult(0,"验证不通过");
({code:1001,msg:"验证不通过"});
setTimeout(function() {
$ss("error");
$(".code-mask").animate({"left":"0px"},200);
$({"left": "10px"},200);
},400);
}
},500)
}
//验证结果
function checkcoderesult(i,txt){
if(i==0){
$(".code-tip").addClass("code-tip-red");
}else{
$(".code-tip").addClass("code-tip-green");
}
$(".code-tip").html(txt);
}
})
}
})(jQuery);
css部分:
.code_bg{
position: fixed;
top:0;
left: 0;
right:0;
bottom:0;
background-color: rgba(0,0,0,.5);
z-index: 99;
}
.icon-login-bg{
background-image: url(../img/icon/);
background-repeat: no-repeat;
}
.code-con{
position: absolute;
top:100px;
width: 320px;
left: 50%;
margin-left: -160px;
background-color: #fff;
z-index: 100;
-moz-user-select: none;
-webkit-user-select: none;
}
.code-img{
margin: 5px 5px;
padding: 5px 5px;
background-color: #f5f6f7;
}
.code-img img{
display: block;
}
.icon-w-25{
display: inline-block;
width: 25px;
height: 25px;
text-indent: -9999px;
}
.icon-push{
cursor: pointer;
background-position: -149px -95px;
}
.code-push{
height: 25px;
}
.code-btn{
position: relative;
height: 30px;
text-align: center;
color: #999;
margin: 10px 10px;
box-sizing: border-box;
background-color: #f5f6f7;
border-radius: 15px;
border: 1px solid #e1e1e1;
}
.code-btn-m{
position: absolute;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #f5f6f7;
border: 1px solid #e1e1e1;
z-index: 5;
top:-8px;
left: 10px;
box-shadow: 0 0 3px #ccc;
cursor: pointer;
background-position: -63px 10px;
}
.code-btn-img{
background-image:url(../img/icon/);
background-repeat: no-repeat;
}
.{
background-position: -134px 10px;
}
.{
background-position: 8px 10px;
}
.code-img-con{
position: relative;
}
.code-mask{
position: absolute;
top:0;
left: 0;
z-index: 10;
}
.code-tip{
padding-left: 10px;
font-size: 12px;
color: #999;
}
.code-tip-red{
color: red;
}
.code-tip-green{
color: green;
}
html部分:
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
开源JS插件框架MinimaJS使用介绍
angularjs带有添加删除选项二级联动
四、J2EE如何生成验证码图片和点击刷新验证码htmlcssWEBITnose百度知
验证码图片生成步骤创建BufferedImage对象。 获取BufferedImage的画笔 ,即调用getGraphics()方法获取Graphics对象 。
调用Graphics对象的setColor()方法和fillRect()方法设置图片背景颜色。
调用Graphics对象的setColor()方法和drawLine()方法设置图片干扰线。
调用BufferedImaged对象的setRGB()方法设置图片的噪点 。
调用掘物Graphics对象的setColor()方法、setFont()方法和drawString()方法设置图片验证码。因为验证码的图片的宽度和高度要根据网站的风格来确定的,所以字体的大小需要根据图片的宽度和高度来确定,用到了小小的技巧。
package util;import ;import ;import ;import ;import mage;import
OutputStream;import on;import ;import ;public class Verification { private
static final String ALPHABET = ""; / * 生成一个宽为width , 高为height, 验证码为code的图片 *
@param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @return 返回图片验证码
*/ public static BufferedImage getImage(int width, int height, String code){
return getImage(width, height, code, 20); } / * 生成一个宽为width, 高为height ,
验证码为code的图片,图片中干扰线的条数为lineCnt * @param width 图片的宽 * @param height 图片的高 *
@param code 验证码字符串 * @param lineCnt 干扰线的条数,建议为10条左右 ,可根据结果适当调整 * @return
返回图片验证码 / public static BufferedImage getImage(int width, int height, String
code, int lineCnt){ return createImage(width, height, code, lineCnt, 0.01); }
/ * 生成一个宽为width , 高为height, 验证码为code的图片,图片中干扰线的条数为lineCnt *
噪声比为noiseRate ,即图片中噪音像素点的百分比 * @param width 图片的宽 * @param height 图片的高 * @param
code 验李散段证码字符串 * @param lineCnt 干扰线的条数,建议为10条左右,可根据结果适当调整 * @param noiseRate
图片中噪音像素点占总像素的百分比 * @return 返回图片验证码 / public static BufferedImage getImage(int
width, int height, String code, int lineCnt, double noiseRate){ return
createImage(width, height, code, lineCnt, noiseRate); } / * 生成一个宽为width ,
高为height, 验证码为code的图片哪誉,图片中干扰线的条数为lineCnt * 噪声比为noiseRate ,即图片中噪音像素点的百分比 *
@param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @param lineCnt
干扰线的条数,建议为10条左右,可根据结果适当调整 * @param noiseRate 图片中噪音像素点占总像素的百分比 * @return
返回图片验证码 / private static BufferedImage createImage(int width, int height,
String code, int lineCnt, double noiseRate){ int fontWidth = ((int)(width *
0.8)) / (); int fontHeight = (int)(height * 0.7);
//为了在任意的width和height下都能生成良好的验证码 , //字体的大小为fontWdith何fontHeight中的小者, int
fontSize = (fontWidth, fontHeight); //drawString时要用到 int paddingX = (int)
(width * 0.1); int paddingY = height - (height - fontSize) / 2; //创建图像
BufferedImage buffimg = new BufferedImage(width, height, _INT_RGB); //获得画笔
Graphics g = cs(); //设置画笔的颜色 (getRandColor(200, 255)); //然后填充一个矩形,即设置背景色 (0,
0, width, height); // 设置干扰线 for (int i = 0; i < lineCnt; i++) {
//随机获取干扰线的起点和终点 int xs = (int)(() * width); int ys = (int)(() * height); int
xe = (int)(() * width); int ye = (int)(() * height); (getRandColor(1, 255));
(xs, ys, xe, ye); } // 添加噪点 int area = (int) (noiseRate * width * height);
for(int i=0; i 255) L = 255; if(R > 255) R = 255; if(L < 0) L = 0; if(R < 0) R
= 0; int interval = R - L; int r = L + (int)(() * interval); int g = L +
(int)(() * interval); int b = L + (int)(() * interval); return new Color(r, g,
b); } / * 随机生成若干个由大小写字母和数字组成的字符串 * @param len 随机生成len个字符 * @return
返回随机生成的若干个由大小写字母和数字组成的字符串 / public static String getRandCode(int len){ String
code = ""; for(int i=0; i
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { HttpSession session = n();
//随机生成字符串 ,并写入session String code = de(4); ute("verification", code);
BufferedImage image = (100,30, code, 5); tType("image/png"); OutputStream out
= Stream(); (ray(image)); (); (); }
在中设置验证码 ,用户点击验证码时,调用js代码请求服务器得到新的验证码 。因为上面的那个生成验证码的servlet会被浏览器缓存,所以js代码中需要给该servlet一个随机的参数 ,这样浏览器就会向服务器发请求得到新的验证码,而不是去缓存中读取 。
Insert title here
最后是在这个servlet中判断用户输入的验证码是否正确,为了方便用户 ,验证码一般都设置成大小写不敏感,所以要先转化为小写字母再比对。
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { HttpSession session = n(); String
verification = (String)ute("verification"); String submitVerification =
ter("submitVerification"); PrintWriter out = (); if(verification!=null &&
submitVerification!=null){ if(se().equals(se())){ ("yes!!!"); } else{
("no!!!"); } } else{ ("no!!!"); } ribute("verification");//防止用户重复提交表单 } / *
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response) / protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException { // TODO
Auto-generated method stub doGet(request, response); } 最后运行的效果图如下
该文章在我的个人博客地址是: /