⑴ 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;
}
}
⑵ 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">
⑶ java web中生成的验证码为什么不放在session
java web中session是服务端机制,是占服务器的内存的,一个用户可能会提取多个验证码那上千个用户差点的服务器就高压了,一般都是一次传递,没必要用session这个贵重的东西,session是在万不得已的情况下存储一些重要的用户信息。
验证码只需要存放在客户端,每次重新验证即可。
⑷ 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();
}*/
}
⑸ javaweb中如何实时判定验证码的正误
你好,你先把图片生成,然后把验证码放在session中,然后传到页面上,当用户照着验证码输入后,ajax异步提交到后台进行校验,跟之前放session的那个值对比就可以了
⑹ java web 验证码生成后一般在什么地方保存这个验证码存到数据库还是怎么地
说起验证码,关系它的是安全性,再联系到你的时效性,这就非session(本身是客户端的唯一使用服务器资源的凭证,而且是有时效限制的,用户长期未访问服务器,这个session是就会被主动注销掉)不能当此大任了,可用性不在话下,至于你说的加密这是画蛇添足了。
依楼上所言,未免误人,服务器端是必须保存这个验证码的,就像我给你了一个验证码,你可以使用,我自己也得备份一下和你比对,不然那不就变成了:你说你的验证码是对的,你已经验证过了,我就信任你了,那就等于没有密码,客户端的一切安全认证都是不足为信的!
⑺ javaweb的验证码问题
点击后,src属性地址换一下,后面带个时间戳字段,这样就不会缓存
⑻ javaweb登录三次失败验证码怎么实现
使用redis存储,存储当前登录的用户名与登录失败次数,超过三次就显示验证码
session和cookie都不建议使用,建议服务端开启验证,浏览器端都可以绕过这些检查
⑼ 如何制作 java web 验证码
java web的验证码原理如下: 根据参数随机生成一个字符串,将字符串存储在一个静态变量中,然后将字符串写在图片上传递到前台供用户识别。用户将识别出的字符串再提交到服务器, 服务器取出静态变量中的字符串与之对比。然后将对比结果返回给用户。
可以自己在网上找些实现好的代码学习一下,也可以使用一些比较成熟的验证码组件。
随着越来越发达的网络技术, 验证码技术也在不断进步,有的验证码中的字符变成了中文,有的在图片中生成问题来让用户回答,有的甚至用到了图片归类,但原理没变。
⑽ java web 项目验证码的刷新问题
用js换 img的src就行了啊。
function change(){
document.getElementById("CreateCheckCode").src="PictureCheckCode?"+Math.random();
}
</script>
<img id="CreateCheckCode" src="PictureCheckCode" onclick="change()" />