extjs ajax java简单精美验证码实现 有图
前端:利用ExtJs的autoEl功能加载图片。
var imgCheckValid = new Ext.create('Ext.Component',{
width: 70, //图片宽度
height: 45, //图片高度
margin:'5',
id:"imd_imgCheckValid",
autoEl: {
tag: 'img', //指定为img标签
src: 'Code/genCodeImg.do?' //指定url路径
},
listeners: {
render: function(component){
component.getEl().on('click', function(e){
this.dom.src = 'Code/genCodeImg.do?'+(new Date().getTime()); // autoEl不会自动加载,只有设置了不同的src才会加载
});
}
}
});
小编心得:autoEl会自动加载,要实现点击验证码刷新,登录失败刷新,方法是给请求的url加上时间戳。所以刷新验证码只要在需要刷新的时候重新设置加上时间戳的src即可。
疑问解答:为什么只是给了src一个路径,并没有给图片地址,页面能显示图片呢?
目前不是很清楚:猜想它是根据给的链接,自动从response中获得二进制资源。明确的是我会向response中写入流
后台:struts,spring配置
struts:
<!-- 客户端 -->
<package name="Code" extends="json" namespace="/Code">
<action name="genCodeImg" class="codeAction" method="genCodeImg">
<result name="success" type="json">
<param name="root">responseJson</param>
</result>
</action>
</package>
spring:
<!-- 验证码生成器--> <bean id="codeAction" class="com.sinsche.i.action.impl.CodeAction" scope="prototype"></bean>
后台:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Random; import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.common.core.st2.action.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.sinsche.i.action.ICodeAction; public class CodeAction extends BaseAction implements ICodeAction {
/**
*
*/
private static final long serialVersionUID = 6865491407475710154L;
private boolean success;
private String message;
// 图片的宽度。
private int width = 70;
// 图片的高度。
private int height = 45;
// 验证码字符个数
private int codeCount = 4;
// 验证码干扰线数
private int lineCount = 20;
// 验证码图片Buffer
private BufferedImage buffImg = null;
Random random = new Random();
public String genCodeImg() throws Exception {
HttpServletResponse resp = ServletActionContext.getResponse();
int fontWidth = width / codeCount;// 字体的宽度
int fontHeight = height - 5;// 字体的高度
int codeY = height - 8; // 图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.getGraphics();
// Graphics2D g = buffImg.createGraphics();
// 设置背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height); // 设置字体
// Font font1 = getFont(fontHeight);
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
g.setFont(font); // 设置干扰线
for (int i = 0; i < lineCount; i++) {
int xs = random.nextInt(width);
int ys = random.nextInt(height);
int xe = xs + random.nextInt(width);
int ye = ys + random.nextInt(height);
// g.setColor(getRandColor(1, 255));
shear(g,1,20,getRandColor(1, 255));
g.drawLine(xs, ys, xe, ye);
} // 添加噪点
float yawpRate = 0.01f;// 噪声率
int area = (int) (yawpRate * width * height);
for (int i = 0; i < area; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height); buffImg.setRGB(x, y, random.nextInt(255));
} String str1 = randomStr(codeCount);// 得到随机字符
for (int i = 0; i < codeCount; i++) {
String strRand = 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);
} // 将四位数字的验证码保存到Session中。
Map<String, Object> map = ActionContext.getContext().getSession();
System.out.print(str1);
map.put("code", str1.toString());
// 禁止图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); // 将图像输出到Servlet输出流中。
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close(); return null;
} // 得到随机字符
private String randomStr(int n) {
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
String str2 = "";
int len = str1.length() - 1;
double r;
for (int i = 0; i < n; i++) {
r = (Math.random()) * len;
str2 = str2 + str1.charAt((int) r);
}
return str2;
} // 得到随机颜色
private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
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);
} // /**
// * 产生随机字体
// */
// private Font getFont(int size) {
// Font font[] = new Font[5];
// font[0] = new Font("Ravie", Font.PLAIN, size);
// font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);
// font[2] = new Font("Fixedsys", Font.PLAIN, size);
// font[3] = new Font("Wide Latin", Font.PLAIN, size);
// font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, size);
// return font[random.nextInt(5)];
// } // 扭曲方法
private void shear(Graphics g, int w1, int h1, Color color) {
shearX(g, w1, h1, color);
shearY(g, w1, h1, color);
} private void shearX(Graphics g, int w1, int h1, Color color) { int period = random.nextInt(2); boolean borderGap = true;
int frames = 1;
int phase = random.nextInt(2); for (int i = 0; i < h1; i++) {
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(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);
}
} } private void shearY(Graphics g, int w1, int h1, Color color) { int period = random.nextInt(40) + 10; // 50; boolean borderGap = true;
int frames = 20;
int phase = 7;
for (int i = 0; i < w1; i++) {
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(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);
} } } public BufferedImage getBuffImg() {
return buffImg;
}
@Override
public String list() throws Exception {
return null;
} @Override
public String create() throws Exception {
return null;
} @Override
public String update() throws Exception {
return null;
} @Override
public String delete() throws Exception {
return null;
} @Override
public String listByPage() throws Exception {
return null;
} public boolean isSuccess() {
return success;
} public void setSuccess(boolean success) {
this.success = success;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} }
效果:






extjs ajax java简单精美验证码实现 有图的更多相关文章
- Java简单验证码的识别
1. 需求 因为项目需要,需要多次登录某网站抓取信息.所以学习了验证码的一些小知识.文章参考http://blog.csdn.net/problc/article/details/5794460的部分 ...
- java识别简单的验证码
1.老规矩,先上图 要破解类似这样的验证码: 拆分后结果: 然后去匹配,得到结果. 2.拆分图片 拿到图片后,首先把图片中我们需要的部分截取出来. 具体的做法是,创建一个的和图片像素相同的一个代表权重 ...
- html --- ajax --- javascript --- 简单的封装
Ajax的简单封装 Ajax的全称是AsynchronousJavaScriptAndXML 如有疑问请参考:http://zh.wikipedia.org/zh-cn/AJAX 以及传智播客的视频教 ...
- Flask学习之旅--用 Python + Flask 制作一个简单的验证码系统
一.写在前面 现在无论大大小小的网站,基本上都会使用验证码,登录的时候要验证,下载的时候要验证,而使用的验证码也从那些简简单单的字符图形验证码“进化”成了需要进行图文识别的验证码.需要拖动滑块的滑动验 ...
- java简单词法分析器(源码下载)
java简单词法分析器 : http://files.cnblogs.com/files/hujunzheng/%E7%AE%80%E5%8D%95%E8%AF%8D%E6%B3%95%E5%88%8 ...
- 学习笔记:利用GDI+生成简单的验证码图片
学习笔记:利用GDI+生成简单的验证码图片 /// <summary> /// 单击图片时切换图片 /// </summary> /// <param name=&quo ...
- !!转!!java 简单工厂模式
举两个例子以快速明白Java中的简单工厂模式: 女娲抟土造人话说:“天地开辟,未有人民,女娲抟土为人.”女娲需要用土造出一个个的人,但在女娲造出人之前,人的概念只存在于女娲的思想里面.女娲造人,这就是 ...
- End-to-End Tracing of Ajax/Java Applications Using DTrace
End-to-End Tracing of Ajax/Java Applications Using DTrace By Amit Hurvitz, July 2007 Aja ...
- JAVA简单Swing图形界面应用演示样例
JAVA简单Swing图形界面应用演示样例 package org.rui.hello; import javax.swing.JFrame; /** * 简单的swing窗体 * @author l ...
随机推荐
- 黄聪:关闭Win2003开机提示“上次意外关机”对话框
很多人在使用win2003服务器(特别是vps)的时候,都会意外关机,然后出现开机提示“上次意外关机”对话框,如果不及时发现,会影响到使用该服务器的网站,所以必须把这提示关闭,方法如下: 1.开始菜单 ...
- PHPCMS v9 超级安全防范教程!
一.目录权限设置很重要:可以有效防范黑客上传木马文件.如果通过 chmod 644 * -R 的话,php文件就没有权限访问了.如果通过chmod 755 * -R 的话,php文件的权限就高了. 所 ...
- iOS开发项目之MVC与MVVM
MVC MVC,Model-View-Controller,我们从这个古老而经典的设计模式入手.采用 MVC 这个架构的最大的优点在于其概念简单,易于理解,几乎任何一个程序员都会有所了解,几乎每一所计 ...
- Java内存区域
1.运行时数据区域 java虚拟机在执行java程序的过程中会将它管理的内存区域分为若干个不同的数据区域.这些区域有各自的服务对象,创建以及销毁时间,有的内存区域随着虚拟机的启动和关闭而创建和销毁,有 ...
- npoi本地文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- java 事件监听 - 键盘
java 事件监听 - 键盘 //事件监听 //键盘事件监听,写了一个小案例,按上下左右,改变圆形的位置,圆形可以移动 import java.awt.*; import javax.swing.*; ...
- flddler使用方法
http://blog.csdn.net/geekgjie/article/details/8029936
- Spring AOP不拦截从对象内部调用的方法原因
拦截器的实现原理很简单,就是动态代理,实现AOP机制.当外部调用被拦截bean的拦截方法时,可以选择在拦截之前或者之后等条件执行拦截方法之外的逻辑,比如特殊权限验证,参数修正等操作. 但是最近在项目中 ...
- javascript面向对象之一
问题:怎么动态设置和读取一个对象的属性? <script type="text/javascript"> function User(property){ for( ...
- android中MVP模式
http://blog.csdn.net/ysh06201418/article/details/46534799 Android App整体架构设计的思考 http://blog.csdn.ne ...