验证码的代码记录(前后端)
一、验证码工具类
(Java Util)
1 import java.awt.*; 2 import java.awt.image.BufferedImage; 3 import java.util.Random; 4 5 /** 6 * @Classname YanZhengUtil 7 * @Description TODO 8 * @Date 2020/9/19 22:49 9 * @Created by 潜水的狗子 10 */ 11 public class YanZhengUtil { 12 13 public static String drawRandomText(int width, int height, BufferedImage verifyImg) { 14 Graphics2D graphics = (Graphics2D)verifyImg.getGraphics(); 15 graphics.setColor(Color.WHITE);//设置画笔颜色-验证码背景色 16 graphics.fillRect(0, 0, width, height);//填充背景 17 graphics.setFont(new Font("微软雅黑", Font.BOLD, 40)); 18 //数字和字母的组合 19 String baseNumLetter="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; 20 21 StringBuffer sBuffer = new StringBuffer(); 22 int x = 10; //旋转原点的 x 坐标 23 String ch = ""; 24 Random random = new Random(); 25 for(int i = 0;i < 4;i++){ 26 graphics.setColor(getRandomColor()); 27 //设置字体旋转角度 28 int degree = random.nextInt() % 30; //角度小于30度 29 int dot = random.nextInt(baseNumLetter.length()); 30 ch = baseNumLetter.charAt(dot) + ""; 31 sBuffer.append(ch); 32 //正向旋转 33 graphics.rotate(degree * Math.PI / 180, x, 45); 34 graphics.drawString(ch, x, 45); 35 //反向旋转 36 graphics.rotate(-degree * Math.PI / 180, x, 45); 37 x += 48; 38 } 39 //画干扰线 40 for (int i = 0; i <6; i++) { 41 // 设置随机颜色 42 graphics.setColor(getRandomColor()); 43 // 随机画线 44 graphics.drawLine(random.nextInt(width), random.nextInt(height), 45 random.nextInt(width), random.nextInt(height)); 46 } 47 //添加噪点 48 for(int i=0;i<30;i++){ 49 int x1 = random.nextInt(width); 50 int y1 = random.nextInt(height); 51 graphics.setColor(getRandomColor()); 52 graphics.fillRect(x1, y1, 2,2); 53 } 54 return sBuffer.toString(); 55 } 56 57 /** 58 * 随机取色 59 */ 60 61 private static Color getRandomColor() { 62 Random ran = new Random(); 63 Color color = new Color(ran.nextInt(256), 64 ran.nextInt(256), ran.nextInt(256)); 65 return color; 66 } 67 68 }
二、Java Action
@Controller @RequestMapping("/UserController") public class UserController { @RequestMapping("/getVerificationCode") public void getVerificationCode(HttpServletResponse response, HttpServletRequest request) { try { int width=200; int height=69; BufferedImage verifyImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //生成对应宽高的初始图片 String randomText = YanZhengUtil.drawRandomText(width,height,verifyImg); //单独的一个类方法,出于代码复用考虑,进行了封装。 //功能是生成验证码字符并加上噪点,干扰线,返回值为验证码字符 request.getSession().setAttribute("getVerificationCode", randomText); response.setContentType("image/png");//必须设置响应内容类型为图片,否则前台不识别 OutputStream os = response.getOutputStream(); //获取文件输出流 ImageIO.write(verifyImg,"png",os);//输出图片流 os.flush(); os.close();//关闭流 } catch (IOException e) { e.printStackTrace(); } } }
三、前端代码
<img class="getVerificationCode" id="img" src="<c:url value="/UserController/getVerificationCode.do"/> " alt="加载失败"> <script type="text/javascript"> $(function(){ $('#img').click(function(){ $("#img").attr("src","<c:url value='/UserController/getVerificationCode.do?time='/>"+new Date().getTime());//添加时间戳,防止读取缓存 }); }); </script>

浙公网安备 33010602011771号