JAva検証コード(struts 2で実現)回転
14601 ワード
ステップ1:認証コードのアクションの作成
ステップ2:actionの構成
ステップ3:jspページの作成(jqueryによる動的リフレッシュ)
1 package com;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics;
6 import java.awt.image.BufferedImage;
7 import java.util.Random;
8
9 import javax.imageio.ImageIO;
10 import javax.servlet.ServletOutputStream;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
14
15 import org.apache.commons.lang.RandomStringUtils;
16 import org.apache.struts2.ServletActionContext;
17 public class AuthCodeAction {
18 private HttpServletResponse response = ServletActionContext.getResponse();
19 private HttpServletRequest request = ServletActionContext.getRequest();
20
21 public String execute() {
22 try {
23 int width = 50;
24 int height = 18;
25 // 4
26 String s = RandomStringUtils.random(4, true, true);
27
28 // session, .
29 // session.
30 HttpSession session = request.getSession(true);
31 session.setAttribute("authCode", s);
32
33 response.setContentType("images/jpeg");
34 response.setHeader("Pragma", "No-cache");
35 response.setHeader("Cache-Control", "no-cache");
36 response.setDateHeader("Expires", 0);
37
38 ServletOutputStream out = response.getOutputStream();
39 BufferedImage image = new BufferedImage(width, height,
40 BufferedImage.TYPE_INT_RGB);
41 Graphics g = image.getGraphics();
42 //
43 g.setColor(getRandColor(200, 250));
44 g.fillRect(0, 0, width, height);
45
46 //
47 Font mFont = new Font("Times New Roman", Font.BOLD, 18);//
48 g.setFont(mFont);
49
50 //
51 // g.setColor(Color.BLACK);
52 // g.drawRect(0, 0, width - 1, height - 1);
53
54 // ,
55 g.setColor(getRandColor(160, 200));
56 //
57 Random random = new Random();
58 for (int i = 0; i < 155; i++) {
59 int x2 = random.nextInt(width);
60 int y2 = random.nextInt(height);
61 int x3 = random.nextInt(12);
62 int y3 = random.nextInt(12);
63 g.drawLine(x2, y2, x2 + x3, y2 + y3);
64 }
65
66 //
67 g.setColor(new Color(20 + random.nextInt(110), 20 + random
68 .nextInt(110), 20 + random.nextInt(110)));
69
70 g.drawString(s, 2, 16);
71
72 //
73 g.dispose();
74 //
75 ImageIO.write((BufferedImage) image, "JPEG", out);
76 out.close();
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80 return null;
81 }
82
83 private Color getRandColor(int fc, int bc) { //
84 Random random = new Random();
85 if (fc > 255)
86 fc = 255;
87 if (bc > 255)
88 bc = 255;
89 int r = fc + random.nextInt(bc - fc);
90 int g = fc + random.nextInt(bc - fc);
91 int b = fc + random.nextInt(bc - fc);
92 return new Color(r, g, b);
93 }
94 }
ステップ2:actionの構成
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="test" namespace="/test" extends="struts-default">
<action name="authCode" class="com.AuthCodeAction" method="execute">
</action>
</package>
</struts>
ステップ3:jspページの作成(jqueryによる動的リフレッシュ)
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title> </title>
<script type="text/javascript" src="js/jquery-1.6.2.min.js">
</script>
<script type="text/javascript">
function changeImg(){
$("#authCode").attr("src","test/authCode.action?d="+new Date().valueOf());
}
</script>
</head>
<body>
<center>
<br>
<img src="test/authCode.action" alt=" " id="authCode" onclick="changeImg()">
<a href="#" onclick="changeImg()"> , !</a>
</center>
</body>
</html>