方法一:
1、前端顯示
HTML:
<h3>驗證碼:</h3> <input type="text" name="validationCode" id="validationCode" placeholder="請輸入驗證碼" lay-verify="required"> <img src="validate.jsp" id="validationCode_img" title="看不清?換一個" onclick="loadimage();return false;" name="validationCode_img" align="middle">
JS:
//加載驗證碼圖片,后面加時間可以保證每次頁面刷新時驗證碼也刷新 function loadimage(){ document.getElementById("validationCode_img").src= "validate.jsp?time=" + new Date().getTime(); }
2、用一個頁面生成驗證碼圖片,這里我用JSP頁面validate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.awt.image.BufferedImage"%> <%@page import="java.awt.Graphics2D"%> <%@page import="java.awt.Color"%> <%@page import="java.awt.Font"%> <%@page import="javax.imageio.ImageIO"%> <%@page import="java.util.Random"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>驗證碼</title> </head> <body> <% int width = 60; int height = 20; // 創建具有可訪問圖像數據緩沖區的Image BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); // 創建一個隨機數生成器 Random random = new Random(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 創建字體,字體的大小應該根據圖片的高度來定 Font font = new Font("Times New Roman", Font.PLAIN, 18); // 設置字體 g.setFont(font); // 畫邊框 g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); // 隨機產生160條干擾線 g.setColor(Color.LIGHT_GRAY); for (int i = 0; i < 160; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(12); int y1 = random.nextInt(12); g.drawLine(x, y, x + x1, y + y1); } // randomCode 用於保存隨機產生的驗證碼 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 隨機產生4位數字的驗證碼 for (int i = 0; i < 4; i++) { // 得到隨機產生的驗證碼數字 String strRand = String.valueOf(random.nextInt(10)); // 產生隨機的顏色分量來構造顏色值 red = random.nextInt(110); green = random.nextInt(50); blue = random.nextInt(50); // 用隨機產生的顏色將驗證碼繪制到圖像中 g.setColor(new Color(red, green, blue)); g.drawString(strRand, 13 * i + 6, 16); randomCode.append(strRand); } // 將四位數字的驗證碼保存到session中 //HttpSession session = request.getSession(); session.setAttribute("randomCode", randomCode.toString()); // 禁止圖像緩存 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.close(); //sos = null; out.clear(); out = pageContext.pushBody(); %> </body> </html>
3、在validate.jsp頁面中生成的驗證碼其實就是在java后端生成的,所以就存進了session中,我們只需要在用戶提交的時候將填寫的驗證碼帶到后端,這里我使用的是ajax請求,后端只需要判斷驗證碼是否和session中一樣就可以了。
方法二:
html代碼:
<div class="layui-form-item"> <div class="layui-inline"> <input type="text" name="verity" required lay-verify="required" placeholder="驗證碼" autocomplete="off" class="layui-input"> </div> <div class="layui-inline"> <img class="verifyImg" id="verifyImg" onclick="javascript:getvCode();" /> <script type="text/javascript"> getvCode();//初始化驗證碼,寫在此處減少驗證碼圖片出來的延時 </script> </div> </div>
js代碼:
/** * 獲取驗證碼 * 將驗證碼寫到login.html頁面的id = verifyimg 的地方 */ function getvCode() { $("#verifyImg").attr('src',timestamp("verifyCode")); } //為url添加時間戳 function timestamp(url) { var getTimestamp = new Date().getTime(); if (url.indexOf("?") > -1) { url = url + "×tamp=" + getTimestamp } else { url = url + "?timestamp=" + getTimestamp } return url; };
java代碼:
model層
//驗證碼 public class VerifyCode { private String code; private byte[] imgBytes; private long expireTime; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public byte[] getImgBytes() { return imgBytes; } public void setImgBytes(byte[] imgBytes) { this.imgBytes = imgBytes; } public long getExpireTime() { return expireTime; } public void setExpireTime(long expireTime) { this.expireTime = expireTime; } }
service層
import java.io.IOException; import java.io.OutputStream; import com.hongyang.webglasses.model.VerifyCode; //驗證碼生成接口 public interface IVerifyCodeGen { /** * 生成驗證碼並返回code,將圖片寫的os中 * * @param width * @param height * @param os * @return * @throws IOException */ String generate(int width, int height, OutputStream os) throws IOException; /** * 生成驗證碼對象 * * @param width * @param height * @return * @throws IOException */ VerifyCode generate(int width, int height) throws IOException; }
serviceimpl實現
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.hongyang.webglasses.model.VerifyCode; import com.hongyang.webglasses.service.IVerifyCodeGen; import com.hongyang.webglasses.util.RandomUtils; @Service public class SimpleCharVerifyCodeGenImpl implements IVerifyCodeGen { private static final Logger logger = LoggerFactory.getLogger(SimpleCharVerifyCodeGenImpl.class); private static final String[] FONT_TYPES = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66" }; private static final int VALICATE_CODE_LENGTH = 4; /** * 生成隨機字符 * * @param width * @param height * @param os * @return * @throws IOException */ @Override public String generate(int width, int height, OutputStream os) throws IOException { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); fillBackground(graphics, width, height); String randomStr = RandomUtils.randomString(VALICATE_CODE_LENGTH); createCharacter(graphics, randomStr); graphics.dispose(); //設置JPEG格式 ImageIO.write(image, "JPEG", os); return randomStr; } /** * 驗證碼生成 * * @param width * @param height * @return */ @Override public VerifyCode generate(int width, int height) { VerifyCode verifyCode = null; try ( //將流的初始化放到這里就不需要手動關閉流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ) { String code = generate(width, height, baos); verifyCode = new VerifyCode(); verifyCode.setCode(code); verifyCode.setImgBytes(baos.toByteArray()); } catch (IOException e) { logger.error(e.getMessage(), e); verifyCode = null; } return verifyCode; } /** * 設置字符顏色大小 * * @param g * @param randomStr */ private void createCharacter(Graphics g, String randomStr) { char[] charArray = randomStr.toCharArray(); for (int i = 0; i < charArray.length; i++) { //設置RGB顏色算法參數 g.setColor(new Color(50 + RandomUtils.nextInt(100), 50 + RandomUtils.nextInt(100), 50 + RandomUtils.nextInt(100))); //設置字體大小,類型 g.setFont(new Font(FONT_TYPES[RandomUtils.nextInt(FONT_TYPES.length)], Font.BOLD, 26)); //設置x y 坐標 g.drawString(String.valueOf(charArray[i]), 15 * i + 5, 19 + RandomUtils.nextInt(8)); } } /** * 設置背景顏色及大小,干擾線 * * @param graphics * @param width * @param height */ private static void fillBackground(Graphics graphics, int width, int height) { // 填充背景 System.setProperty("myColor", "#BBDAEE"); graphics.setColor(Color.getColor("myColor")); //設置矩形坐標x y 為0 graphics.fillRect(0, 0, width, height); // 加入干擾線條 for (int i = 0; i < 8; i++) { //設置隨機顏色算法參數 graphics.setColor(RandomUtils.randomColor(40, 150)); Random random = new Random(); int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(width); int y1 = random.nextInt(height); graphics.drawLine(x, y, x1, y1); } } }
Controller層
@GetMapping("/verifyCode") public void verifyCode(HttpServletRequest request, HttpServletResponse response) { IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl(); try { //設置長寬 VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28); String code = verifyCode.getCode(); //將VerifyCode綁定session,登錄控制器可從session中取出從而判斷用戶填的驗證碼是否正確 request.getSession().setAttribute("VerifyCode", code); //設置響應頭 response.setHeader("Pragma", "no-cache"); //設置響應頭 response.setHeader("Cache-Control", "no-cache"); //在代理服務器端防止緩沖 response.setDateHeader("Expires", 0); //設置響應內容類型 response.setContentType("image/jpeg"); response.getOutputStream().write(verifyCode.getImgBytes()); response.getOutputStream().flush(); } catch (IOException e) { } }
工具類
import java.awt.Color; import java.util.Random; //生成驗證碼隨機字符 public class RandomUtils extends org.apache.commons.lang3.RandomUtils{ private static final char[] CODE_SEQ = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' }; private static final char[] NUMBER_ARRAY = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; private static Random random = new Random(); public static String randomString(int length) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.append(String.valueOf(CODE_SEQ[random.nextInt(CODE_SEQ.length)])); } return sb.toString(); } public static String randomNumberString(int length) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.append(String.valueOf(NUMBER_ARRAY[random.nextInt(NUMBER_ARRAY.length)])); } return sb.toString(); } public static Color randomColor(int fc, int bc) { int f = fc; int b = bc; Random random = new Random(); if (f > 255) { f = 255; } if (b > 255) { b = 255; } return new Color(f + random.nextInt(b - f), f + random.nextInt(b - f), f + random.nextInt(b - f)); } public static int nextInt(int bound) { return random.nextInt(bound); } }
效果圖: