java验证码
Posted 小狗吃月亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java验证码相关的知识,希望对你有一定的参考价值。
Util类 生成验证码
package com.soft863.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.jsp.PageContext;
import org.springframework.stereotype.Component;
/*@Component*/
public final class GraphiscHelper {
/**
* 以字符串形式返回生成的验证码,同时输出一个图片
*
* @param width 图片的宽度
* @param height 图片的高度
* @param output 图片的输出流(图片将输出到这个流中)
* @return 返回所生成的验证码(字符串)
*/
public static String create(final int width, final int height, OutputStream output) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphic = image.getGraphics();
graphic.setColor(Color.getColor("F8F8F8"));
graphic.fillRect(0, 0, width, height);
Color[] colors = new Color[]{Color.BLUE, Color.GRAY, Color.GREEN, Color.RED, Color.BLACK, Color.ORANGE, Color.CYAN};
// 在 "画板"上生成干扰线条 ( 50 是线条个数)
for (int i = 0; i < 50; i++) {
graphic.setColor(colors[random.nextInt(colors.length)]);
final int x = random.nextInt(width);
final int y = random.nextInt(height);
final int w = random.nextInt(20);
final int h = random.nextInt(20);
final int signA = random.nextBoolean() ? 1 : -1;
final int signB = random.nextBoolean() ? 1 : -1;
graphic.drawLine(x, y, x + w * signA, y + h * signB);
}
// 在 "画板"上绘制字母
graphic.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
for (int i = 0; i < 4; i++) {
final int temp = random.nextInt(26) + 97;
String s = String.valueOf((char) temp);
sb.append(s);
graphic.setColor(colors[random.nextInt(colors.length)]);
graphic.drawString(s, i * (width / 4), height - (height / 3));
}
graphic.dispose();
try {
ImageIO.write(image, "JPEG", output);
/* output.flush();
output.close();*/
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
controller层方法:
/*
* wa:宽 hb:高
*/
//用于页面展示验证码,并把验证信息存储到session中
@RequestMapping("/yzm")
public void yzm(HttpServletRequest req, HttpServletResponse resp,Integer wa,Integer hb) throws IOException, ServletException {
OutputStream out = resp.getOutputStream();
String s = GraphiscHelper.create(wa,hb,out);
resp.setHeader("pragma", "no-cache");
//存储到session中
System.out.println(s);
req.getSession().setAttribute("Code", s);
}
//用于接收前台表单提交数据 进行信息的验证
@RequestMapping("/loginyz.action")
public String loginyz(user user,HttpServletRequest req,HttpSession se) throws IOException {
String attribute = (String)se.getAttribute("Code");
System.out.println(user);
System.out.println(attribute);
return "loginUI";
}
//前台页面
//验证码板块
<tr>
<td height="35" class="login-text02">验证码:</td>
<td><input name="validateCode" id="ma_validateCode" type="text" size="30" maxlength="6" value=""/>
<img src="${pageContext.request.contextPath}/login/yzm?wa=100&hb=30" onclick="switchImg(this)"/>点击图片刷新验证码
</td>
</tr>
//单击验证码图片进行刷新
<script>
function switchImg(tag){
var d = new Date();
//获取图片的路径
var src = tag.src ;
tag.src = src +"&d="+d;
}
</script>
以上是关于java验证码的主要内容,如果未能解决你的问题,请参考以下文章