当前位置:首页 » 网页前端 » web验证码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

web验证码

发布时间: 2022-01-14 15:35:17

⑴ java web 项目验证码的刷新问题

用js换 img的src就行了啊。
function change(){
document.getElementById("CreateCheckCode").src="PictureCheckCode?"+Math.random();
}
</script>
<img id="CreateCheckCode" src="PictureCheckCode" onclick="change()" />

⑵ java web中验证码代码

生成code.jsp文件的完整代码如下:

<%@ page language="java" contentType="image/jpeg; charset=gb2312"
pageEncoding="gb2312"%>
<%@ page import="java.awt.*,java.awt.image.*" %>
<%@ page import="java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){
Random r=new Random();
if(fc>255) fc=255;
if(bc>200) bc=255;
int red=fc+r.nextInt(bc-fc);
int green=fc+r.nextInt(bc-fc);
int blue=fc+r.nextInt(bc-fc);
return new Color(red,green,blue);
}%>
<% //设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("cache-Control","no-cache");
response.setDateHeader("Expires",0);
//创建随机类
Random r=new Random();
//在内存中创建图像,宽度,高度
int width=80,height=30;
BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取图形上下文环境
Graphics gc=pic.getGraphics();
//设定背景颜色并进行填充
gc.setColor(getRandColor(200,250));
gc.fillRect(0,0,width,height);
//设定图形上下文环境字体
gc.setFont(new Font("Times New Roman",Font.PLAIN,20));
//画边框
//gc.setColor(new Color(1));
//gc.drawRect(0,0,width-1,height-1);

//随机产生200条干扰直线,使图像中的认证码不易被其他分析程序探测
gc.setColor(getRandColor(160,200));
for(int i=0;i<200;i++)
{
int x1=r.nextInt(width);
int y1=r.nextInt(height);
int x2=r.nextInt(15);
int y2=r.nextInt(15);
gc.drawLine(x1,y1,x1+x2,y1+y2);
}
//随即产生100个干扰点
gc.setColor(getRandColor(120,240));
for(int i=1;i<100;i++){
int x=r.nextInt(width);
int y=r.nextInt(height);
gc.drawOval(x,y,0,0);
}
//随机产生四位数字的验证码
String RS="";
String rn="";
for(int i=0;i<4;i++)
{
//产生十以内随机数字
rn=String.valueOf(r.nextInt(10));
RS+=rn;
//将认证码用drawString函数显示到图像里
gc.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));//使字体颜色效果明显
gc.drawString(rn,13*i+16,16);
}
//释放图形上下文环境
gc.dispose();
//将认证码RS存入session中共享
session.setAttribute("random",RS);

//输出生成后的图象到页面
ImageIO.write(pic,"JPEG",response.getOutputStream());

out.clear();
out = pageContext.pushBody();

%>

使用随即码的jsp文件中加入下面一句即可
<image src="code.jsp">

⑶ JAVAWEB项目怎么实现验证码

importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;

publicclassCode{

//图片的宽度。
privateintwidth=160;
//图片的高度。
privateintheight=38;
//验证码字符个数
privateintcodeCount=4;
//验证码干扰线数
privateintlineCount=20;
//验证码
privateStringcode=null;
//验证码图片Buffer
privateBufferedImagebuffImg=null;
Randomrandom=newRandom();

privatebooleantype=false;

publicCode(){

}

publicCode(intwidth,intheight){
this.width=width;
this.height=height;
}

publicCode(intwidth,intheight,intcodeCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
}

publicCode(intwidth,intheight,intcodeCount,intlineCount){
this.width=width;
this.height=height;
this.codeCount=codeCount;
this.lineCount=lineCount;
}

publicvoidinit(booleantype){
this.type=type;
}

//生成图片
privatevoidcreatImage(booleantype){
intfontWidth=width/codeCount;//字体的宽度
intfontHeight=height-5;//字体的高度
intcodeY=height-8;

//图像buffer
buffImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=buffImg.getGraphics();
//Graphics2Dg=buffImg.createGraphics();
//设置背景色
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);//设置字体
Fontfont=null;
if(!type)font=newFont("Fixedsys",Font.BOLD,fontHeight);
elsefont=getFont(fontHeight);
g.setFont(font);

//设置干扰线
for(inti=0;i<lineCount/2;i++){
intxs=random.nextInt(width);
intys=random.nextInt(height);
intxe=xs+random.nextInt(width);
intye=ys+random.nextInt(height);
g.setColor(getRandColor(1,255));
if(!type)g.drawLine(xs,ys,xe,ye);
elseshear(g,width,height,getRandColor(1,255));
}

//添加噪点
floatyawpRate=0.01f;//噪声率
intarea=(int)(yawpRate*width*height);
for(inti=0;i<area;i++){
intx=random.nextInt(width);
inty=random.nextInt(height);

buffImg.setRGB(x,y,random.nextInt(255));
}


Stringstr1=randomStr(codeCount);//得到随机字符
this.code=str1;
for(inti=0;i<codeCount;i++){
StringstrRand=str1.substring(i,i+1);
g.setColor(getRandColor(1,255));
//g.drawString(a,x,y);
//a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的(x,y)位置处

g.drawString(strRand,i*fontWidth+3,codeY);
}


}

//得到随机字符
privateStringrandomStr(intn){
Stringstr1="";//I和l不要
Stringstr2="";
intlen=str1.length()-1;
doubler;
for(inti=0;i<n;i++){
r=(Math.random())*len;
str2=str2+str1.charAt((int)r);
}
returnstr2;
}

//得到随机颜色
privateColorgetRandColor(intfc,intbc){//给定范围获得随机颜色
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}

/**
*产生随机字体
*/
privateFontgetFont(intsize){
Randomrandom=newRandom();
Fontfont[]=newFont[5];
font[0]=newFont("Ravie",Font.PLAIN,size);
font[1]=newFont("AntiqueOliveCompact",Font.PLAIN,size);
font[2]=newFont("Fixedsys",Font.PLAIN,size);
font[3]=newFont("WideLatin",Font.PLAIN,size);
font[4]=newFont("GillSansUltraBold",Font.PLAIN,size);
returnfont[random.nextInt(5)];
}

//扭曲方法
privatevoidshear(Graphicsg,intw1,inth1,Colorcolor){
shearX(g,w1,h1,color);
shearY(g,w1,h1,color);
}

privatevoidshearX(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(2);

booleanborderGap=true;
intframes=1;
intphase=random.nextInt(2);

for(inti=0;i<h1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(0,i,w1,1,(int)d,0);
if(borderGap){
g.setColor(color);
g.drawLine((int)d,i,0,i);
g.drawLine((int)d+w1,i,w1,i);
}
}

}

privatevoidshearY(Graphicsg,intw1,inth1,Colorcolor){

intperiod=random.nextInt(40)+10;//50;

booleanborderGap=true;
intframes=20;
intphase=7;
for(inti=0;i<w1;i++){
doubled=(double)(period>>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.Area(i,0,1,h1,0,(int)d);
if(borderGap){
g.setColor(color);
g.drawLine(i,(int)d,i,0);
g.drawLine(i,(int)d+h1,i,h1);
}

}

}publicvoidwrite(OutputStreamsos)throwsIOException{
if(buffImg==null)creatImage(type);
ImageIO.write(buffImg,"png",sos);
//JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(sos);
//encoder.encode(buffImg);
sos.close();
}

publicBufferedImagegetBuffImg(){
if(buffImg==null)creatImage(type);
returnbuffImg;
}

publicStringgetCode(){
returncode.toLowerCase();
}

//使用方法
/*publicvoidgetCode3(HttpServletRequestreq,HttpServletResponseresponse,HttpSessionsession)throwsIOException{
//设置响应的类型格式为图片格式
response.setContentType("image/jpeg");
//禁止图像缓存。
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);


CreateImageCodevCode=newCreateImageCode(100,30,5,10);
session.setAttribute("code",vCode.getCode());
vCode.write(response.getOutputStream());
response.flushBuffer();
}*/

}

⑷ 怎样破解web图形验证码

利用JavaScript破解验证码
近日,网上惊现可以破解验证码的JavaScript脚本——GreaseMonkey!由“Shaun Friedle”开发的这段脚本可以轻松搞定Megaupload站点的CAPTCHA。如果您不相信的话,可以到http://herecomethelizards.co.uk/mu_captcha/亲自尝试一下!

现在,Megaupload站点提供的CAPTCHA在上述代码面前已经败下阵来,说实话,这里的验证码设计的不不太好。但更有趣的是:

1.HTML 5中的Canvas应用程序接口getImageData可以用来从验证码图像中取得像素数据。利用Canvas,我们不仅可以将一个图像嵌入一个画布中,而且之后还可以再从中重新提取出来。

2.上述的脚本中包含一个完全使用JavaScript实现的神经网络。

3.使用Canvas从图像中提取出像素数据后,将其送入神经网络,通过一种简单的光学字符识别技术来推测验证码中到底使用了哪些字符。

通过阅读源代码,我们不仅可以更好地理解其工作原理,也可以领会这个验证码究竟是如何实现的。就像前面看到的那样,这里使用的验证码不是很复杂——每个验证码有三个字符组成,每个字符使用一种不同的颜色,并且只使用26个字母中的字符,而所有字符都使用同一种字体。

⑸ 如何制作 java web 验证码

java web的验证码原理如下: 根据参数随机生成一个字符串,将字符串存储在一个静态变量中,然后将字符串写在图片上传递到前台供用户识别。用户将识别出的字符串再提交到服务器, 服务器取出静态变量中的字符串与之对比。然后将对比结果返回给用户。
可以自己在网上找些实现好的代码学习一下,也可以使用一些比较成熟的验证码组件。
随着越来越发达的网络技术, 验证码技术也在不断进步,有的验证码中的字符变成了中文,有的在图片中生成问题来让用户回答,有的甚至用到了图片归类,但原理没变。

⑹ javaweb,邮箱发送验证码后进行校验。

简单点,就是保存在缓存里面,new一个map放在里面就是了。校验的话直接和map里面的去比。

publicclassaaa{
publicstaticMap<String,String>map=newHashMap<String,String>();//定义一个静态map,放验证码

//比如这个是生成验证码
publicStringCreateCheckCode(Stringname){
StringcheckCode="";
String[]arrs={"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","A","B"};
//随机生成验证码、
for(inti=0;i<6;i++){
inta=(int)(Math.random()*28);
checkCode+=arrs[a];
}
//为了保证唯一性质,我们可以使用注册的客户的名字作为key
map.put(name,checkCode);
System.out.println(checkCode);
returncheckCode;
}
//这个可以作为控制层:比如客户点击验证的连接,就会进入这个方法:来验证验证码
publicbooleanverifyCheckCode(Stringname,StringcheckCode)
{
booleanflag=false;
Stringcode=map.get(name);
if(null!=code&&code.equals(checkCode)){
flag=true;
}

returnflag;

}


}

⑺ 如何用html编写产生验证码

<HTML>
<HEAD>
<TITLE>生成验证码</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function createCode(len)
{
var seed = new Array(
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'0123456789'
); //创建需要的数据数组
var idx,i;
var result = ''; //返回的结果变量
for (i=0; i<len; i++) //根据指定的长度
{
idx = Math.floor(Math.random()*3); //获得随机数据的整数部分-获取一个随机整数
result += seed[idx].substr(Math.floor(Math.random()*(seed[idx].length)), 1);//根据随机数获取数据中一个值
}
return result; //返回随机结果
}

function test() {
var inputRandom=document.getElementById("inputRandom").value;
var autoRandom=document.getElementById("autoRandom").innerHTML;
if(inputRandom==autoRandom) {
alert("通过验证");
} else {
alert("没有通过验证");
}

}
</SCRIPT>
</HEAD>
<BODY>
验证码长度:
<SELECT id="sel">
<option value=1>1</option>
<option value=3>3</option>
<option value=5>5</option>
<option value=7 selected>7</option>
<option value=9>9</option>
</SELECT>
<BR>
<table>
<tr>
<td>验证码:</td>
<td><input type="text" id="inputRandom"></td>
<td><label id="autoRandom" value=""></label><INPUT TYPE="button" VALUE="获取验证码" ONCLICK="autoRandom.innerHTML=createCode(sel.value)"></td>
<td><input type="button" value="验证" onclick="test()"></td>
</tr>

</table>
<script type="text/javascript">
window.onload()=autoRandom.innerHTML=createCode(sel.value);
</script>
</BODY>
</HTML>

⑻ 怎么在web登录界面实现验证码功能

两套方案
第一:使用标签或者textbox(需设为只读)输出验证码
第二:让验证码生成图片,并加入随机线条
思路在上面,具体方法网络一下 ^_^
goodluck!

⑼ web前端怎么开发短信验证功能

用户点击获取验证码时,发送手机号到后端接口
后端接收请求后,将手机号及验证码发给短信网关,并将手机号和短信验证码保存起来
用户收到短信后,输入验证码,然后将验证码和手机号传给后端
后端校验手机号和验证码是否与之前保存的一致,如果一致,则鉴权通过
向前端发送认证通过的cookie以及通过状态
前端进行页面跳转或其他业务逻辑

⑽ javaweb的验证码问题

点击后,src属性地址换一下,后面带个时间戳字段,这样就不会缓存