S2SH框架中的无刷新验证码功能实现

时间:2022-12-12 15:14:27

暑假期间在实验室做使用S2SH框架的项目,其中登录和注册需要验证码,实现了一个没有实现刷新验证码功能的简单版本,代码如下:

  1 package com.sem.action;
2
3 import java.io.*;
4 import java.util.Map;
5
6 import javax.imageio.ImageIO;
7 import javax.imageio.stream.ImageOutputStream;
8 //import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 //import org.apache.struts2.interceptor.ServletRequestAware;
12 import org.apache.struts2.interceptor.ServletResponseAware;
13 //import org.springframework.context.annotation.Scope;
14
15 import com.opensymphony.xwork2.ActionContext;
16 import com.opensymphony.xwork2.ActionSupport;
17
18 import java.awt.*;
19 import java.awt.image.BufferedImage;
20
21
22 public class ImageCodeAction extends ActionSupport implements ServletResponseAware {
23
24 private ByteArrayInputStream inputStream;
25 private HttpServletResponse response;
26
27 //生成4个0~9的随机数,放在一个字符串里
28 public String createRandomString() {
29 String str = "";
30 for (int i = 0; i < 4; i++) {
31 str += Integer
32 .toString((new Double(Math.random() * 10)).intValue());
33 }
34 return str;
35 }
36
37 //随机生成一种颜色
38 public Color createsRandomColor() {
39 int r = (new Double(Math.random() * 256)).intValue();
40 int g = (new Double(Math.random() * 256)).intValue();
41 int b = (new Double(Math.random() * 256)).intValue();
42 return new Color(r, g, b);
43 }
44
45 //生成一个内存图片,将四个随机数写在图片上
46 public BufferedImage createImage(String str) {
47 int width = 60;
48 int height = 20;
49 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
50 //获取图形上下文
51 Graphics g = image.getGraphics();
52 //设定背景色
53 g.setColor(Color.WHITE);
54 g.fillRect(0, 0, width, height);
55 //画边框
56 g.setColor(Color.black);
57 g.drawRect(0, 0, width - 1, height - 1);
58 //将验证码显示到图像中
59 g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
60 //使用随机颜色
61 g.setColor(this.createsRandomColor());
62 //将随机字符串的每个数字分别写到图片上
63 g.drawString(Character.toString(str.charAt(0)), 8, 17);
64 g.drawString(Character.toString(str.charAt(1)), 20, 17);
65 g.drawString(Character.toString(str.charAt(2)), 33, 17);
66 g.drawString(Character.toString(str.charAt(3)), 45, 17);
67 //图像生效
68 g.dispose();
69 return image;
70 }
71
72 //将图片以字节形式写到InputStream里
73 public ByteArrayInputStream createInputStream() throws Exception {
74 //获取随机字符串
75 String str = this.createRandomString();
76 BufferedImage image = this.createImage(str);
77 //将产生的字符串写入session,供校验时使用
78 ActionContext actionContext = ActionContext.getContext();
79 Map session = actionContext.getSession();
80 session.put("random", str);
81 System.out.print("str: " + str + " session: " + (String)session.get("random") + "\n");
82 ByteArrayOutputStream output = new ByteArrayOutputStream();
83 ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
84 ImageIO.write(image, "JPEG", imageOut);
85 imageOut.close();
86 ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
87 output.close();
88 return input;
89 }
90
91 //传入action后执行该方法生成图片验证码
92 public String createCodeImage() throws Exception {
93 response.setHeader("Pragma","No-cache");
94 response.setHeader("Cache-Control","no-cache");
95 response.setDateHeader("Expires", 0);
96 setInputStream(createInputStream());
97 return SUCCESS;
98 }
99
100 public ByteArrayInputStream getInputStream() {
101 return inputStream;
102 }
103
104 public void setInputStream(ByteArrayInputStream inputStream) {
105 this.inputStream = inputStream;
106 }
107
108 public void setServletResponse(HttpServletResponse response) {
109 this.response = response;
110 }
111 }

如果想要实现刷新功能,利用JavaScript可以很容易做到。

相关文章