博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java生成图片验证码
阅读量:2433 次
发布时间:2019-05-10

本文共 4424 字,大约阅读时间需要 14 分钟。

package com.tantuls.login.controller;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.common.tools.StringTools;/** * 验证码处理控制类 
<功能详细描述>
* * @author chenlujun * @version [版本号, 2014-9-30] * @see [相关类/方法] * @since [产品/模块版本] */@Controller@Scope("prototype")@RequestMapping("/signcode")public class SignCodeController { /** 检查验证码 * 用户登录的时候避免暴力破解加上图片验证码 * @param request * @param signcode 传入的验证码 * @return true 验证通过,输入的验证码和生成的一致 * false 验证不过,输入的验证码和后台生成的不一致 * @see [类、类#方法、类#成员] */ @RequestMapping("/check") @ResponseBody public boolean checkSignCode(HttpServletRequest request,String signcode) { HttpSession session = request.getSession(); String signcodeSession=(String)session.getAttribute("signcode"); if(StringTools.isNull(signcode)) { return false; } if(StringTools.isNull(signcodeSession)) { return false; } //验证的时候不区分大小写 signcode=signcode.toUpperCase(); signcodeSession=signcodeSession.toUpperCase(); if(signcode.equals(signcodeSession)) { return true; } return false; } /** 生成验证码 * 生成验证码直接从内存中写入输出流; * 图片文件不存盘 * @param request * @param response * @throws ServletException * @throws IOException * @see [类、类#方法、类#成员] */ @RequestMapping(value = "/get", method = RequestMethod.GET) public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 验证码图片的宽度。 int width = 70; // 验证码图片的高度。 int height = 30; BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); // 创建一个随机数生成器类。 Random random = new Random(); // 设定图像背景色(因为是做背景,所以偏淡) g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // 创建字体,字体的大小应该根据图片的高度来定。 Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28); // 设置字体。 g.setFont(font); // 画边框。 g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到。 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。 StringBuffer randomCode = new StringBuffer(); // 设置默认生成4个验证码 int length = 4; // 设置备选验证码:包括"a-z"和数字"0-9" String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; int size = base.length(); // 随机产生4位数字的验证码。 for (int i = 0; i < length; i++) { // 得到随机产生的验证码数字。 int start = random.nextInt(size); String strRand = base.substring(start, start + 1); // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110))); g.drawString(strRand, 15 * i + 6, 24); // 将产生的四个随机数组合在一起。 randomCode.append(strRand); } // 将四位数字的验证码保存到Session中。 HttpSession session = request.getSession(); session.setAttribute("signcode", randomCode.toString()); // 图象生效 g.dispose(); // 禁止图像缓存。 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); // 将图像输出到Servlet输出流中。 ServletOutputStream sos = response.getOutputStream(); ImageIO.write(buffImg, "jpeg", sos); sos.flush(); sos.close(); } // 给定范围获得随机颜色 Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) { fc = 255; } if (bc > 255) { bc = 255; } int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); }}

使用:

转载地址:http://pzvmb.baihongyu.com/

你可能感兴趣的文章
乙未年年终总结
查看>>
子网掩码
查看>>
第一天上班没精神
查看>>
启动eclipse报错:Failed to load the JNI shared library
查看>>
eclipse安装插件的两种方式在线和离线
查看>>
linux下源的相关笔记(suse)
查看>>
linux系统分区文件系统划分札记
查看>>
Linux(SUSE 12)安装Tomcat
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>