Java实现Web页面前数字字母验证码实现
最近公司做项目开发中用到了验证码实现功能,将实现代码分享出来,
前段页面实现代码:
为了表达清晰,样式部分代码去掉了,大家根据自己的需求,自己添加样式。
页面JS代码:触发变动验证码改变的JS
后台 Controller处理:
package com.njcc.pay.controller.login;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.p_w_picpath.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.p_w_picpathio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.common.utils.StringUtils;
/**
* 验证马 Controller
*
* @author Administrator
*
*/
@Controller
public class ValidateCodeController {
@SuppressWarnings("unused")
private static final Log LOG = LogFactory.getLog(ValidateCodeController.class);
public static final String VALIDATE_CODE = "validateCode";
private int w = 70;
private int h = 23;
/**
* @throws Exception
* 函数功能说明 : 进入后台登陆页面.
*
* @参数: @return
* @return String
* @throws
*/
@RequestMapping(value = "/validateCode")
public void validateCode(HttpServletRequest request,
HttpServletResponse response) throws Exception {
createImage(request,response);
}
private void createImage(HttpServletRequest request,HttpServletResponse response) throws IOException {
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("p_w_picpath/jpeg");
String width = request.getParameter("width");
String height = request.getParameter("height");
if (StringUtils.isNumeric(width) && StringUtils.isNumeric(height)) {
w = NumberUtils.toInt(width);
h = NumberUtils.toInt(height);
}
BufferedImage p_w_picpath = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = p_w_picpath.getGraphics();
/*
* 生成背景
*/
createBackground(g);
/*
* 生成字符
*/
String s = createCharacter(g);
request.getSession().setAttribute(VALIDATE_CODE, s);
g.dispose();
OutputStream out = response.getOutputStream();
ImageIO.write(p_w_picpath, "JPEG", out);
out.close();
}
/**
* 生成颜色
* @param fc
* @param bc
* @return
*/
private Color getRandColor(int fc,int bc) {
int f = fc;
int b = bc;
Random random=new Random();
if(f>255) {
f=255;
}
if(b>255) {
b=255;
}郑州妇科医院http://jbk.39.net/yiyuanzaixian/sysdfkyy/
return new Color(f+random.nextInt(b-f),f+random.nextInt(b-f),f+random.nextInt(b-f));
}
/**
* 生成背景
* @param g
*/
private void createBackground(Graphics g) {
// 填充背景
g.setColor(getRandColor(220,250));
g.fillRect(0, 0, w, h);
// 加入干扰线条
for (int i = 0; i < 8; i++) {
g.setColor(getRandColor(40,150));
Random random = new Random();
int x = random.nextInt(w);
int y = random.nextInt(h);
int x1 = random.nextInt(w);
int y1 = random.nextInt(h);
g.drawLine(x, y, x1, y1);
}
}
/**
* 生成字符
* @param g
* @return
*/
private String createCharacter(Graphics g) {
char[] codeSeq = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
String[] fontTypes = {"Arial","Arial Black","AvantGarde Bk BT","Calibri"};
Random random = new Random();
StringBuilder s = new StringBuilder();
for (int i = 0; i < 4; i++) {
String r = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);//random.nextInt(10));
g.setColor(new Color(50 + random.nextInt(100), 50 + random.nextInt(100), 50 + random.nextInt(100)));
g.setFont(new Font(fontTypes[random.nextInt(fontTypes.length)],Font.BOLD,26));
g.drawString(r, 15 * i + 5, 19 + random.nextInt(8));
// g.drawString(r, i*w/4, h-5);
s.append(r);
}
return s.toString();
}
}
Java实现Web页面前数字字母验证码实现的更多相关文章
- Servlet实现数字字母验证码图片(二)
Servlet实现数字字母验证码图片(二): 生成验证码图片主要用到了一个BufferedImage类,如下:
- Java selenium web页面的滚动条操作
摘录自:http://blog.csdn.net/iceryan/article/details/8162703 //移动到元素element对象的"顶端"与当前窗口的" ...
- java图形验证码生成工具类及web页面校验验证码
最近做验证码,参考网上案例,发现有不少问题,特意进行了修改和完善. 验证码生成器: import javax.imageio.ImageIO; import java.awt.*; import ja ...
- web页面 验证码 生成
web页面 验证码 生成 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kapt ...
- JAVA整合kaptcha生成验证码 (字母验证码和算术验证码)
引入maven <!--图片验证码--> <dependency> <groupId>com.github.penggle</groupId> < ...
- paip.powerdesign cdm pdm文件 代码生成器 java web 页面 实现
paip.powerdesign cdm pdm文件 代码生成器 java web 页面 实现 准备从pd cdm生成java web 页面...但是,ms无直接地生成软件.... 只好自己解析cdm ...
- Java用webSocket实现tomcat的日志实时输出到web页面
原文:http://blog.csdn.net/smile326/article/details/52218264 1.场景需求 后台攻城狮和前端攻城狮一起开发时,经常受到前端攻城狮的骚扰,动不动就来 ...
- Java随机生成定长纯数字或数字字母混合数
(转)Java随机生成定长纯数字或数字字母混合数 运行效果图: 具体实现代码
- java验证,”支持6-20个字母、数字、下划线或减号,以字母开头“这个的正则表达式怎么写?
转自:https://yq.aliyun.com/wenzhang/show_96854 问题描述 java验证,”支持6-20个字母.数字.下划线或减号,以字母开头“这个的正则表达式怎么写? 验证” ...
随机推荐
- C#使用SendMessage发送组合键
有时需要出发菜单功能,例如发送ALT + F打开应用程序的文件菜单,如何使用SendMessage实现呢? 使用用spy++截取的ALT+F的消息内容(如何使用spy++,请熟悉的高手指点下,我使用s ...
- win7Setx修改环境变量
SETX.exe (Resource Kit, Windows 7) Set environment variables permanently, SETX can be used to set En ...
- .NETFramework:Encoding
ylbtech-.NETFramework:Encoding 1.返回顶部 1. #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, Pub ...
- 【旧文章搬运】PspCidTable攻与防
原文发表于百度空间,2009-03-29========================================================================== PspCi ...
- Event Handling Guide for iOS--(一)--About Events in iOS
About Events in iOS Users manipulate their iOS devices in a number of ways, such as touching the scr ...
- View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views
Views Because view objects are the main way your application interacts with the user, they have many ...
- 006--linux基础rpm和yum的使用和源码安装
一.rpm相关命令介绍 1. 查看CD里面有的文件 2. 用rpm来安装一个名为vsftpd的rpm包 3. rpm -qi 软件包名 (查看软件包的详细信息) 4. rpm -ql 软件包名 (查看 ...
- 3DMAX 9 角色建模3 uv展开
将角色删除一半,展好uv再镜像出来,节省一半工作量(前提是对称) 添加UVW展开编辑器(Unwrap UVW),选择面 打开UV编辑器 这里要注意映射问题,默认打开UV编辑器后所选择的面是映射到与选择 ...
- JAVA多线程(四) Executor并发框架向RabbitMQ推送消息
github代码地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...
- php 中的引用(&)与foreach结合后的一个注意点
关于php中引用的概念及foreach循环的的应用就不多说了,php文档已经说的很明白了.直接上一段代码: <?php $arr = array(1,2, 3); foreach($arr as ...