一、java的验证码
文件一:index.jsp
文件二:showradomcode.jsp
<%@ page contentType="image/jpeg" import="java.awt.image.*,javax.imageio.*" %><%
//设置页面不缓存
//response.reset();
response.setContentType("image/jpeg");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//在内存中创建图象 宽60 高18 4个字符 0条干扰线
BufferedImage image = rc.getRandomImage(58, 18, 4, 1);
//设置其session值
session.setAttribute("jcmsrandomchar", rc.getRandomString());
session.setMaxInactiveInterval(100000000);
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
文件三:RandomChar.java
package com.hanweb.sso.ldap.util;
/**
* Title: 随机码图形生成程序的处理 Description:
* 为了加强网络安全,防止黑课工具对系统的攻击,现在通用随机码来解决这个问题,因为每一次产生的随机码是随机的,所以使用穷举法破解密码的工具就无能为力了
* Copyright: Copyright (c) 2004-2010 Company: SDLDAP
*
* @author LiChanglai
* @version 1.0
*/
import java.util.*;
import java.awt.*;
import java.awt.image.*;
public class RandomChar {
/**
* 给定范围获得随机颜色
*
* @param fc
* int 参数1
* @param bc
* int 参数2
* @return Color 返回的color对象
*/
private String strRandomString; // 生成的随机数
/**
* 取其生成的随机数
*
* @return String
*/
public String getRandomString() {
return strRandomString;
}
/**
* 设置其生成的随数
*
* @param randomString
* String
*/
public void setRandomString(String randomString) {
this.strRandomString = randomString;
}
/**
* 取其某一范围的color
*
* @param fc
* int 范围参数1
* @param bc
* int 范围参数2
* @return Color
*/
private Color getRandColor(int fc, int bc) {
// 取其随机颜色
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
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);
}
/**
* 用于生成其随机数
*
* @param randomCount
* int 随机数总长度
* @return String 返回的随机数
*/
private String getRandomString(int nRandomCount) {
/*
* String[] a = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
* "B", "C", "D", "E", "F", "G", "H", "I", "G", "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" };
*/
String[] a = { "a", "b", "c", "d", "e", "f", "g", "h", "g", "k", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
String strRand = "";
int nRand;
// 取其随机数的总数
int LengthOfRandom = a.length;
// 生成随机类
Random random = new Random();
// 循环取随机取其整数,并将其整数所对应的结果进行累加
for (int i = 0; i < nRandomCount; i++) {
nRand = random.nextInt(LengthOfRandom);
strRand += a[nRand];
}
// 返回累加后的结果
return strRand;
}
/**
* 生成其随机的图形
*
* @param width
* int 生成图形的宽度
* @param height
* int 生成图形的高度
* @param nLengthOfRandom
* int 随机码的个数
* @param nDisturbLineCount
* int 干扰线的条数
* @return BufferedImage
*/
public BufferedImage getRandomImage(int nWidth, int nHeight,
int nLengthOfRandom, int nDisturbLineCount)
{
// 在内存中创建图象
BufferedImage image = new BufferedImage(nWidth, nHeight,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
String strRand = "";
nLengthOfRandom = (nLengthOfRandom <= 0) ? 6 : nLengthOfRandom;
nDisturbLineCount = (nDisturbLineCount <= 0) ? 0 : nDisturbLineCount;
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, nWidth, nHeight);
// 设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 16));
// 画边框
g.setColor(new Color(255, 255, 255));
g.drawRect(0, 0, nWidth - 1, nHeight - 1);
// 随机产生nDisturbLineCount条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < nDisturbLineCount; i++) {
int x = random.nextInt(nWidth);
int y = random.nextInt(nHeight);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(6位)
strRand = this.getRandomString(nLengthOfRandom);
// 设置其属性randomString的值
this.strRandomString = strRand;
for (int i = 0; i < nLengthOfRandom; i++) {
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(strRand.substring(i, i + 1), 13 * i + 5, 14);
}
// 图象生效
g.dispose();
return image;
}
}
但是这种验证码,遇到websphere平台会有问题,如果有同仁解决了websphere的问题,请给我mail,,谢谢
二、JS验证码:
文件一、cookies.js
/**
* Read the JavaScript cookies tutorial at:
*
*/
/**
* Sets a Cookie with the given name and value.
*
* name Name of the cookie
* value Value of the cookie
* [expires] Expiration date of the cookie (default: end of current session)
* [path] Path where the cookie is valid (default: path of calling document)
* [domain] Domain where the cookie is valid
* (default: domain of calling document)
* [secure] Boolean value indicating if the cookie transmission requires a
* secure transmission
*/
function setCookie(name, value, expires, path, domain, secure)
{
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
/**
* Gets the value of the specified cookie.
*
* name Name of the desired cookie.
*
* Returns a string containing value of specified cookie,
* or null if cookie does not exist.
*/
function getCookie(name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
/**
* Deletes the specified cookie.
*
* name name of the cookie
* [path] path of the cookie (must be same as path used to create cookie)
* [domain] domain of the cookie (must be same as domain used to create cookie)
*/
function deleteCookie(name, path, domain)
{
if (getCookie(name))
{
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
文件二、vcode.js
function dologin(type)
{
var form = document.form1;
if (form.loginuser.value=="" || form.loginpass.value=="" || form.randomcode.value=="" )
{
alert("用户名/密码/验证码均不能为空!");
return false;
}else{
setCookie("sso_username", form.loginuser.value);
var b = getCookie("sso_code");
if(b==form.randomcode.value){
form.submit();
}else{
alert("验证码错误");
return false;
}
form.loginpass.value="";
form.randomcode.value="";
randomcode();
}
}
function initlogin()
{
var form = document.form1;
var c = getCookie("sso_username");
if (typeof(c)!="undefined" && c!=null){form.loginuser.value=c;}
}
function $(id){return document.getElementById(id);}
function random(length)
{
var seed = new Array('0','1','2','3','4','5','6','7','8','9');
var idx,i;
var result = '';
for (i=0; i {
idx = Math.floor(Math.random()*10);
result += seed[idx].substr(Math.floor(Math.random()*(seed[idx].length)), 1);
}
return result;
}
function randomcode()
{
$("divcode").innerHTML = "";
$("hidecode").value = "";
$("randomcode").value = "";
var code = random(4);
setCookie("sso_code",code);
//alert(code);
$("divcode").innerHTML = code.bold().fontsize(3);
$("hidecode").value = code;
//alert($("randomcode").value);
}
initlogin();
randomcode();
文件三:前台文件