
正文
java滑块验证码源代码 滑块验证码的作用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java随机生成四位验证码,只求编程代码
我自己做的系统里面用作验证码的JSP的
%@page contentType="image/jpeg;charset=utf-8"%
%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %
%@ page import="java.io.OutputStream" %
html
body
%!
Color getRandColor(int fc,int bc)
{
Random rd=new Random();
if(fc255) fc=255;
if(bc255) bc=255;
int red=fc+rd.nextInt(bc-fc);
int green=fc+rd.nextInt(bc-fc);
int blue=fc+rd.nextInt(bc-fc);
return new Color(red,green,blue);
}
%
%
Random r=new Random();
response.addHeader("Pragma","No-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("expires",0);
int width=90;
int height=23;
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);
String[] rNum ={"0","1","2","3","4","5","6","7","8","9","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","C","D","E","F","G","H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD,
Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC};
gc.setColor(Color.WHITE);
gc.drawLine(0,30,90,10);
gc.setColor(getRandColor(160,200));
for (int i=0;i50;i++)
{
int x = r.nextInt(width);
int y = r.nextInt(height);
int xl = r.nextInt(10);
int yl = r.nextInt(10);
gc.drawLine(x,y,x+xl,y+yl);
}
gc.setColor(getRandColor(60,150));
String rt = "";
for(int i=0;i4;i++){
String temp = rNum[r.nextInt(62)];
rt = rt+temp;
gc.setFont(new Font("Times New Roman",style[r.nextInt(7)],15));
gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10));
}
gc.dispose();
session.setAttribute("randNum",rt);
OutputStream os=response.getOutputStream();
ImageIO.write(pic,"JPEG",os);
System.out.println("当前验证码为:"+session.getAttribute("randNum"));
os.flush();
os.close();
os=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
%
/body
/html
相关问答
Q1: jsp中显示验证码的代码怎么写?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/*生成验证码图片
*/
public class MakeCertPic {
//验证码图片中可以出现的字符集,可以根据需要修改
private char mapTable[]={
'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','0','1','2','3',
'4','5','6','7','8','9'
};
/* 功能:生成彩色验证码图片
参数wedth为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流
*/
public String getCertPic(int width,int height,OutputStream os){
if(width=0)
width=60;
if(height=0)
height=20;
BufferedImage image= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//设定背景颜色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0,0,width,height);
//画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
//随机产生的验证码
String strEnsure = "";
//4代表4为验证码,如果要产生更多位的验证码,则加大数值
for(int i = 0;i4;++i){
strEnsure += mapTable[(int) (mapTable.length*Math.random())];
}
//将认证码显示到图像中,如果要生成更多位的验证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str, 20, 15);
str = strEnsure.substring(2,3);
g.drawString(str, 35, 18);
str = strEnsure.substring(3,4);
g.drawString(str, 45, 15);
//随机产生15个干扰点
Random rand = new Random();
for(int i=0; i10; i++){
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
//释放图形上下文
g.dispose();
try{
//输出图形到页面
ImageIO.write(image, "JPEG", os);
}catch (IOException e){
return "";
}
return strEnsure;
}
}
makeCertPic.jsp页面用于调用生成验证码图片的JavaBean,并在客户端显示,源代码如下:
%@page contentType="image/jpeg" %%@page language="java" pageEncoding="utf-8"%jsp:useBean id="image" scope="page" class="securityCode.pic.MakeCertPic"/%
String str = image.getCertPic(0,0,response.getOutputStream());
//将验证码存入session中
session.setAttribute("certCode",str);
%
下边是登录页面:
%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
title验证码测试登录页面/title
script type="text/javascript"
function changeimg()
{
var myimg = document.getElementById("code");
now = new Date();
myimg.src="makeCertPic.jsp?code="+now.getTime();
}
/script
/head
body
center
form action="loginCheck.jsp" method="post" /
用户名:input type="text" name="username" /br
密nbsp;nbsp;码:input type="password" name="password"/br
nbsp;验证码:input type="text" name="certCode"/
img id="code" src="makeCertPic.jsp"a href="javascript:changeimg()"看不清,换一张 /abr
input type="submit" value="登录"/
/form
/center
/body
/html
Q2: 怎样用java实现验证码
现在许多系统的注册 登录或者发布信息模块都添加的随机验证码功能 就是为了避免自动注册程序或者自动发布程序的使用
验证码实际上就是随机选择一些字符以图片的形式展现在页面上 如果进行提交操作的同时需要将图片上的字符同时提交 如果提交的字符与服务器session保存的不同 则认为提交信息无效 为了避免自动程序分析解析图片 通常会在图片上随机生成一些干扰线或者将字符进行扭曲 增加自动识别验证码的难度
在这里 我们使用java实现验证码
%@ page contentType= image/jpeg import= java awt * java awt image * java util * javax imageio * %
%!
Color getRandColor(int fc int bc){//给定范围获得随机颜色
Random random = new Random();
if(fc ) fc= ;
if(bc ) bc= ;
int r=fc+random nextInt(bc fc);
int g=fc+random nextInt(bc fc);
int b=fc+random nextInt(bc fc);
return new Color(r g b);
}
%
%
//设置页面不缓存
response setHeader( Pragma No cache );
response setHeader( Cache Control no cache );
response setDateHeader( Expires );
// 在内存中创建图象
int width= height= ;
BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g setColor(getRandColor( ));
g fillRect( width height);
//设定字体
g setFont(new Font( Times New Roman Font PLAIN ));
// 随机产生 条干扰线 使图象中的认证码不易被其它程序探测到
g setColor(getRandColor( ));
for (int i= ;i ;i++)
{
int x = random nextInt(width);
int y = random nextInt(height);
int xl = random nextInt( );
int yl = random nextInt( );
g drawLine(x y x+xl y+yl);
}
// 取随机产生的认证码( 位数字)
String codeList = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ;
String sRand= ;
for (int i= ;i ;i++){
int a=random nextInt(codeList length() );
String rand=codeList substring(a a+ );
sRand+=rand;
// 将认证码显示到图象中
g setColor(new Color( +random nextInt( ) +random nextInt( ) +random nextInt( )));//调用函数出来的颜色相同 可能是因为种子太接近 所以只能直接生成
g drawString(rand *i+ );
}
// 将认证码存入SESSION
session setAttribute( rand sRand);
// 图象生效
g dispose();
// 输出图象到页面
ImageIO write(image JPEG response getOutputStream());
out clear();
out = pageContext pushBody();
lishixinzhi/Article/program/Java/hx/201311/25536
关于java滑块验证码源代码和滑块验证码的作用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






