Struts2中实现随机验证码
一.创建RandomNum类
1: import java.awt.Color;2: import java.awt.Font;3: import java.awt.Graphics;4: import java.awt.image.BufferedImage;5: import java.io.ByteArrayInputStream;6: import java.io.ByteArrayOutputStream;7: import java.io.IOException;8: import java.util.Random;9: import javax.imageio.ImageIO;10: import javax.imageio.stream.ImageOutputStream;11:12: public class RandomNumUtil {13: private ByteArrayInputStream image; //字节流输出图像14: private String str; //随机数组成的字符串15:16: public void setImage(ByteArrayInputStream image) {17: this.image = image;18: }19:20: public void setStr(String str) {21: this.str = str;22: }23:24: private RandomNumUtil(){25: init();//初始化属性26: }27: /*28: * 取得RandomNumUtil实例29: */30: public static RandomNumUtil Instance() {31: return new RandomNumUtil();32: }33: /*34: * 取得验证码图片35: */36: public ByteArrayInputStream getImage() {37: return image;38: }39: /*40: * 取得图片的验证码41: */42: public String getStr() {43: return str;44: }45: /*46: * init()方法,为字节流赋值47: */48: private void init(){49: //在内存中创建图像50: int width=85,height=20;51: BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);52: //获取图形上下文53: Graphics g=image.getGraphics();54: //生成随机类55: Random random=new Random();56: //设定背景色57: g.setColor(getRandColor(200,250));58: g.fillRect(0, 0, width, height);59: //设定字体60: g.setFont(new Font("Times New Roman",Font.PLAIN,18));61: //随机产生155条干扰线,使图像中的认证码不易被其它程序探测到62: g.setColor(getRandColor(160, 200));63: for (int i = 0; i < 155; i++) {64: int x=random.nextInt(width);65: int y=random.nextInt(height);66: int x1=random.nextInt(12);67: int y1=random.nextInt(12);68: g.drawLine(x, y, x+x1, y+y1);69: }70: //取随机产生的认证码(6位数)71: String sRand="";72: for (int i = 0; i < 6; i++) {73: String rand=String.valueOf(random.nextInt(10));74: sRand+=rand;75: //将认证码显示到图像中76: g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));77: //78: g.drawString(rand, 13*i+6, 16);79: }80: //赋值验证码81: this.str=sRand;82: //图像生效83: g.dispose();84: ByteArrayInputStream input=null;85: ByteArrayOutputStream output=new ByteArrayOutputStream();86: try {87: ImageOutputStream imageOut=ImageIO.createImageOutputStream(output);88: ImageIO.write(image, "JPEG", imageOut);89: imageOut.close();90: input=new ByteArrayInputStream(output.toByteArray());91: } catch (IOException e) {92: System.out.println("验证码图片产生出现错误:"+e.toString());93: e.printStackTrace();94: }95: this.image=input;96: }97:98: /*99: * 给定范围获得随机颜色100: */101: private Color getRandColor(int fc,int bc) {102: Random random = new Random();103: if(fc>255){104: fc=255;105: }106: if(bc>255){107: bc=255;108: }109: int r=fc+random.nextInt(bc-fc);110: int g=fc+random.nextInt(bc-fc);111: int b=fc+random.nextInt(bc-fc);112: return new Color(r, g, b);113: }114: }
二.创建RandomAction
获取图像流,并放到sessio中1: package com.action;2:3: import java.io.ByteArrayInputStream;4:5: import com.opensymphony.xwork2.ActionContext;6: import com.opensymphony.xwork2.ActionSupport;7: import com.util.RandomNumUtil;8:9: public class RandomAction extends ActionSupport{10: private ByteArrayInputStream inputStream;11:12: public ByteArrayInputStream getInputStream() {13: return inputStream;14: }15:16: public void setInputStream(ByteArrayInputStream inputStream) {17: this.inputStream = inputStream;18: }19:20: public String execute() throws Exception{21:22: RandomNumUtil rand=RandomNumUtil.Instance();23: this.setInputStream(rand.getImage());24: ActionContext.getContext().getSession().put("random", rand.getStr());// 取得随机字符串放入HttpSession25:26: return "success";27: }28: }29:
三.配置Struts.xml
1: <package name="random" extends="struts-default">2: <!-- Random验证码 -->3: <action name="rand" class="com.action.RandomAction">4: <result type="stream"> <!-- 以流类型返回结果 -->5: <param name="contentType">image/jpeg</param>6: <param name="inputName">inputStream</param>7: </result>8: </action>9: </package>
四.在登录页面添加
1: <s:textfield label="验证码" name="rand" size="6" />2: <image src="rand.action"3: onclick="changeValidateCode(this)" title="点击图片刷新验证码" />
五.在loginAction中添加验证码验证
1: String random = (String) ActionContext.getContext().getSession().get("random");2: if (random.equals(rand)) {3:4: } else {5:6: }
Struts2中实现随机验证码的更多相关文章
- Django中生成随机验证码(pillow模块的使用)
Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...
- Struts2中的图片验证码
1.Struts中建一个action <action name="Code" class="LoginAction" method="code& ...
- Django框架登录验证及产生随机验证码的实例
1:views视图代码 # 登录验证 def login(request): # 使用ajax请求可以使用判断 # if request.is_ajax(): if request.method == ...
- 图片验证码(Struts2中使用)
写在前面: 最近在项目中做了一个登录页面,用到了图片验证码的功能,所以记录一下.方便之后再有用到,直接拿来用即可.其实图片验证码的生成都是有固定步骤的,网上也有很多的例子,有的时候,如果不想深究,都是 ...
- struts2生成随机验证码图片
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- day 51 随机验证码, 验证登陆 ,以及 装饰器在函数中的应用
前端很好的session 的例子 (随机验证码登陆) https://github.com/Endless-Clould/qianduan 参考: 验证码登录 https://www.cnblogs. ...
- 在mvc中实现图片验证码的刷新
首先,在项目模型(Model)层中建立一个生成图片验证码的类ValidationCodeHelper,代码如下: public class ValidationCodeHelper { //用户存取验 ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- Struts2中基于Annotation的细粒度权限控制
Struts2中基于Annotation的细粒度权限控制 2009-10-19 14:25:53| 分类: Struts2 | 标签: |字号大中小 订阅 权限控制是保护系统安全运行很重要 ...
随机推荐
- 移动端web总结
viewport 通用模版: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Android技术之-------电脑获取手机截图
入吾QQ群183435019(学习 交流+唠嗑) 其实这个相当简单 应该会点ADB命令的人都会吧 一 下载一个ADB 如果你是学Android编程的,,Android SDK应该有吧,在Android ...
- 【转载】Centos7 中使用Supervisor守护进程
配置supervisor实现进程守护 1.安装supervisor yum install Supervisor 2.启动服务 supervisord -c /etc/supervisord.co ...
- typeof关键字的作用
http://blog.chinaunix.net/uid-28458801-id-4200573.html 一.typeof详解: 前言: typeof关键字是C语言中的一个新扩展,这个特性在 ...
- Erlang内存吃紧之解决思路
首先使用erlang:memory()确定是哪个部分内存吃紧,根据输出的内容,比对内存占用大小,有针对性地进行分析.在erlang系统里内存的单位为word,通过erlang:system_info( ...
- 【java开发系列】—— 集合使用方法
一.首先看一下集合的框架图: 由于collection也继承了Iterator和comparable接口,因此我们可以使用Iterator来遍历元素,也可以通过自定义compareTo函数来重新编写自 ...
- 洛谷 P3377 【模板】左偏树(可并堆)
洛谷 P3377 [模板]左偏树(可并堆) 题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或 ...
- hdu_1015(dfs)
题意:根据给出的计算公式,给一个n和一个字符集,问能不能在字符串集中找到不重复的五个字符,让其计算结果等于给定的n,如果有多个解输出字典序最大的一个 题解:dfs直接上代码了 code: #inclu ...
- codeforces 746C 模拟
C. Tram time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- div排版+文档流+定位秘诀
由于没有找到自己认为完整的关于普通流.浮动和绝对定位的中文文章,于是鼓起勇气决定自己来写篇. 在普通流中的 Box(框) 属于一种 formatting context(格式化上下文) ,类型可以是 ...