为了防止,页面向数据库暴力注册入力,用户登录暴力破解,所以加入验证码,验证码无法被软件获取上边的内容(加入算数计算,更加安全),所以在现在技术,暂时安全。
先看效果图:
第一次加载比较慢,防止无法加载验证码显示,后台获取准备好的默认正在加载图片(静态图片),后台图片获取好后,替代。
验证码效果图:
后台Java图片实现类VerificationCodeTool:
package com.tsXs.fileshare.tools;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.log4j.Logger;
/**
*
* ClassName: VerificationCodeTool <br/>
* Description: creat verification code <br/>
* Date: 2015-3-3 下午08:37:55 <br/>
* <br/>
*
* @author yptian@aliyun.com
*
* first made
* @version 1.0.0<br/>
*
*/
public class VerificationCodeTool {
//LOG
private static final Logger LOG =Logger.getLogger(VerificationCodeTool.class);
//verification code image width
private static final int IMG_WIDTH=146;
//verification code image height
private static final int IMG_HEIGHT=30;
//The number of interference lines
private static final int DISTURB_LINE_SIZE = 15;
//generate a random number
private Random random = new Random();
//result
private int xyresult;
//result random string
private String randomString;
//Chinese Numbers
// private static final String [] CNUMBERS = "零,一,二,三,四,五,六,七,八,九,十".split(",");
//零一二三四五六七八九十乘除加减
//Here, must be java Unicode code
private static final String CVCNUMBERS = "\u96F6\u4E00\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D\u5341\u4E58\u9664\u52A0\u51CF";
//Definition of drawings in the captcha characters font, font name, font style, font size
//static final font : In Chinese characters garbled
private final Font font = new Font("黑体", Font.BOLD, 18);
//data operator
private static final Map<String, Integer> OPMap = new HashMap<String, Integer>();
static{
OPMap.put("*", 11);
OPMap.put("/", 12);
OPMap.put("+", 13);
OPMap.put("-", 14);
}
/**
* The generation of image verification code
* */
public BufferedImage drawVerificationCodeImage(){
//image
BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
//In memory to create a brush
Graphics g = image.getGraphics();
//Set the brush color
// g.setColor(getRandomColor(200,250));
g.setColor(Color.WHITE);
//Fill the background color, using the current brush colour changing background color images
//The meaning of the four parameters respectively, starting x coordinates, starting y, width, height.
//image background
g.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);
//Set the brush color
g.setColor(getRandomColor(200,250));
//image border
g.drawRect(0, 0, IMG_WIDTH-2, IMG_HEIGHT-2);
//Set disturb line color
g.setColor(getRandomColor(110, 133));
//Generate random interference lines
for(int i =0;i < DISTURB_LINE_SIZE; i++){
drawDisturbLine1(g);
drawDisturbLine2(g);
}
//Generate a random number, set return data
getRandomMathString();
LOG.info("验证码 : "+randomString);
LOG.info("验证码结果 : "+xyresult);
//The generated random string used to save the system
StringBuffer logsu = new StringBuffer();
for(int j=0,k = randomString.length(); j < k; j++){
int chid = 0;
if(j==1){
chid = OPMap.get(String.valueOf(randomString.charAt(j)));
}else{
chid = Integer.parseInt(String.valueOf(randomString.charAt(j)));
}
String ch = String.valueOf(CVCNUMBERS.charAt(chid));
logsu.append(ch);
drawRandomString((Graphics2D)g,ch, j);
}
//= ?
drawRandomString((Graphics2D)g,"\u7B49\u4E8E\uFF1F", 3);
logsu.append("\u7B49\u4E8E \uFF1F");
LOG.info("汉字验证码 : "+logsu);
randomString = logsu.toString();
//Release the brush object
g.dispose();
return image;
}
/**
* Get a random string
* */
private void getRandomMathString(){
//Randomly generated number 0 to 10
int xx = random.nextInt(10);
int yy = random.nextInt(10);
//save getRandomString
StringBuilder suChinese = new StringBuilder();
//random 0,1,2
int Randomoperands = (int) Math.round(Math.random()*2);
//multiplication
if(Randomoperands ==0){
this.xyresult = yy * xx;
// suChinese.append(CNUMBERS[yy]);
suChinese.append(yy);
suChinese.append("*");
suChinese.append(xx);
//division, divisor cannot be zero, Be divisible
}else if(Randomoperands ==1){
if(!(xx==0) && yy%xx ==0){
this.xyresult = yy/xx;
suChinese.append(yy);
suChinese.append("/");
suChinese.append(xx);
}else{
this.xyresult = yy + xx;
suChinese.append(yy);
suChinese.append("+");
suChinese.append(xx);
}
//subtraction
}else if(Randomoperands ==2){
thi