日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

【Java】登录操作中随机生成验证码的工具类

發(fā)布時間:2024/4/15 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java】登录操作中随机生成验证码的工具类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

效果圖:

工具類CreateImageCode.java:

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;public class CreateImageCode {private int width = 70;private int height = 27;private int codeCount = 4;// 干擾線數(shù)//private int lineCount = 10;// 驗證碼圖片Buffer// 驗證碼private String code = null;public String getCode() {return code;}public void setCode(String code) {this.code = code;}private BufferedImage buffImg = null;Random random = new Random();public CreateImageCode() {createImage();}public CreateImageCode(int width, int height) {this.width = width;this.height = height;createImage();}public CreateImageCode(int width, int height, int codeCount) {this.width = width;this.height = height;this.codeCount = codeCount;createImage();}public CreateImageCode(int width, int height, int codeCount, int lineCount) {this.width = width;this.height = height;this.codeCount = codeCount;//this.lineCount = lineCount; createImage();}// 生成圖片private void createImage() {int fontWidth = width / codeCount;// 字體寬度。int fontHeight = height - 5;// 字體高度。int codeY = height - 8;// 得到圖片buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = buffImg.getGraphics();// 設(shè)置背景色g.setColor(getRandColor(249, 250));g.fillRect(0, 0, width, height);// 設(shè)置邊框//g.setColor(getRandColor(200, 250));//g.drawRect(1, 1, width - 2, height - 2);// 設(shè)置字體Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);g.setFont(font);/*// 設(shè)置干擾線for (int i = 0; i < lineCount; i++) {int x1 = random.nextInt(width);int y1 = random.nextInt(height);int x2 = random.nextInt(width);int y2 = random.nextInt(height);g.setColor(getRandColor(1, 255));g.drawLine(x1, y1, x2, y2);}*//*// 添加噪點float yawpRate = 0.01f;// 噪聲率int area = (int) (yawpRate * width * height);for (int i = 0; i < area; i++) {int x = random.nextInt(width);int y = random.nextInt(height);buffImg.setRGB(x, y, random.nextInt(255));}*/String str1 = randomStr(codeCount);// 得到隨機(jī)字符this.code = str1;for (int i = 0; i < codeCount; i++) {String strRand = str1.substring(i, i + 1);g.setColor(getRandColor(50, 250));// g.drawString(a,x,y);// a為要畫出來的東西,x和y表示要畫的東西最左側(cè)字符的基線位于此圖形上下文坐標(biāo)系的 (x, y) 位置處 g.drawString(strRand, i * fontWidth + 3, codeY);}}// 得到隨機(jī)字符private String randomStr(int n) {String str1 = "1234567890";String str2 = "";int len = str1.length() - 1;double r;for (int i = 0; i < n; i++) {r = (Math.random()) * len;str2 = str2 + str1.charAt((int) r);}return str2;}// 得到隨機(jī)顏色private Color getRandColor(int fc, int bc) {// 給定范圍獲得隨機(jī)顏色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);}public void write(OutputStream sos) throws IOException {ImageIO.write(buffImg, "png", sos);sos.close();}}

在servlet中使用此工具類,輸出驗證碼到網(wǎng)頁:

package com.utils;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet(name = "GetImageCodeServlet",urlPatterns = {"/servlet/code.servlet"}) public class GetImageCodeServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("image/jepg");// 控制瀏覽器不要緩存response.setDateHeader("expries", -1);response.setHeader("Cache-Control", "no-cache");response.setHeader("Pragma", "no-cache");// 創(chuàng)建圖片CreateImageCode image = new CreateImageCode();String str = image.getCode();// 存入Session // System.out.println("驗證碼為"+str);request.getSession().setAttribute("code",str);// 輸出到網(wǎng)頁 image.write(response.getOutputStream());} }

JSP中在表單顯示此圖片(演示時不使用Ajax,直接將form提交到另一個servlet判斷):

<form action="${pageContext.request.contextPath}/servlet/codeTest.servlet"><input type="text" name="code"><img src="${pageContext.request.contextPath}/servlet/code.servlet"onclick="javascript:this.src='${pageContext.request.contextPath}/servlet/code.servlet?rm='+Math.random()"alt="獲取驗證碼"><input type="submit" value="驗證"></form>

后臺驗證驗證碼的servlet:

package com.utils;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet(name = "CodeTestServlet" ,urlPatterns = {"/servlet/codeTest.servlet"}) public class CodeTestServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String code = request.getParameter("code");if (code.equals(request.getSession().getAttribute("code"))) {System.out.println("驗證成功,驗證碼為:" + request.getSession().getAttribute("code"));// response.sendRedirect(getServletContext().getContextPath()+"/login.jsp");}else{System.out.println("驗證失敗");}} }

----

使用的是3.0的注解開發(fā),web.xml中不需配置

若為maven項目,依賴為:

<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.1</version></dependency>

?

轉(zhuǎn)載于:https://www.cnblogs.com/to-red/p/11260628.html

總結(jié)

以上是生活随笔為你收集整理的【Java】登录操作中随机生成验证码的工具类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。