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 | 标签: |字号大中小 订阅 权限控制是保护系统安全运行很重要 ...
随机推荐
- pycharm中进行带参数的调试
之前基本都是用print进行调试的,但今天程序运行完需要等很长时间,就尝试pycharm调试. 但是在运行程序需要传递参数,之前只会点击bug按钮直接运行程序,不知道调试的时候传入参数. 运行---& ...
- 【JavaScript的基本语法】
[JavaScript的基本语法 ] 1.javascript输出 JavaScript语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. <script> documen ...
- 51Nod 1289 大鱼吃小鱼(模拟,经典好题)
1289 大鱼吃小鱼 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: ...
- [bzoj2333] [SCOI2011]棘手的操作 (可并堆)
//以后为了凑字数还是把题面搬上来吧2333 发布时间果然各种应景... Time Limit: 10 Sec Memory Limit: 128 MB Description 有N个节点,标号从1 ...
- [bzoj2288][POJ Challenge]生日礼物
用堆维护双向链表来贪心... 数据范围显然不容许O(nm)的傻逼dp>_<..而且dp光是状态就n*m个了..显然没法优化 大概就会想到贪心乱搞了吧...一开始想贪心地通过几段小的负数把正 ...
- HDU_5563Clarke and five-pointed star
Clarke and five-pointed star Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- Palindromes
http://acm.hdu.edu.cn/showproblem.php?pid=1318 Palindromes Time Limit: 2000/1000 MS (Java/Others) ...
- Socket send函数和recv函数详解
1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向TCP ...
- UEP-级联查询
级联查询在UEP中采用动态下拉的形式,cascadeid为关键字,注意jsp页面的id的相互嵌套关系,数据库字段的数值的设置,和动态下拉SQL语句的书写.本功能实现了省市区的三级联动查询
- reduceByKeyLocally
2017年3月15日, 星期三 reduceByKeyLocally--Transformation类算子 代码示例