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 | 标签: |字号大中小 订阅 权限控制是保护系统安全运行很重要 ...
随机推荐
- python与 Ajax跨域请求
同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上的 ...
- 任务调度框架Quartz原理简介
[TOC] 第一章 Quartz 1.1 Quartz概念 Quartz是OpenSymphony开源组织的一个Java开源项目, 在2009被Terracotta收购.Quartz官网 1.2 Qu ...
- element-ui,router.push到其他路由,菜单栏不会高亮对应的路由
使用饿了吗的路由,使用this.$router.push({path: ''})跳到其他的路由,菜单不会高亮. 如图所示,点击图上三个位置,需要使用this.$router.push({path: ' ...
- SpringMVC连接MongoDB操作数据库
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...
- 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】
Valley Numer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- NowCoderWannafly挑战赛3-B.遇见
遇见 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 A和B在同一条路上,他们之间的距离为 k ...
- poj1258prim算法
/*poj 1258 *题意:有若干个农场,现需要将各个农场用光纤连接起来,各个农场之间连接的光纤长度也许不同, *要求求出使得将所有农场连接起来的最短光线长度 *算法分析:使用矩阵将各个农场之间的光 ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- mysql 恢复数据
前提:保存了需要恢复数据库的文件 .frm 和 .ibd 文件 条件:InnoDB 类型的 恢复表结构1.新建一个数据库--新建一个表,表名和列数和需要恢复数据库相同2.停止mysql服务器 serv ...
- The most interesting feature of iPhone X - FaceID
No doubt everybody knows that iPhone 8 & iPhone X appear on the market. A feature called FaceID ...