您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用Spring Boot怎么樣實(shí)現(xiàn)一個驗(yàn)證碼生成功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1、驗(yàn)證碼生成類
import java.awt.*; import java.awt.image.BufferedImage; import java.io.OutputStream;import java.util.HashMap; import java.util.Map;import java.util.Random; public class ImageCode { private static char mapTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; public static Map<String, Object> getImageCode(int width, int height, OutputStream os) { Map<String,Object> returnMap = new HashMap<String, Object>(); if (width <= 0) width = 60; if (height <= 0) height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 獲取圖形上下文 Graphics g = image.getGraphics(); //生成隨機(jī)類 Random random = new Random(); // 設(shè)定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); //設(shè)定字體 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // 隨機(jī)產(chǎn)生168條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 168; 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); } //取隨機(jī)產(chǎn)生的碼 String strEnsure = ""; //4代表4位驗(yàn)證碼,如果要生成更多位的認(rèn)證碼,則加大數(shù)值 for (int i = 0; i < 4; ++i) { strEnsure += mapTable[(int) (mapTable.length * Math.random())]; // 將認(rèn)證碼顯示到圖象中 g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); //直接生成 String str = strEnsure.substring(i, i + 1); g.drawString(str, 13 * i + 6, 16); } // 釋放圖形上下文 g.dispose(); returnMap.put("image",image); returnMap.put("strEnsure",strEnsure); return returnMap; } //給定范圍獲得隨機(jī)顏色 static 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); } }
2、獲取驗(yàn)證碼API
@RequestMapping(value = "/images/imagecode") public String imagecode(HttpServletRequest request, HttpServletResponse response) throws Exception { OutputStream os = response.getOutputStream(); Map<String,Object> map = ImageCode.getImageCode(60, 20, os); String simpleCaptcha = "simpleCaptcha"; request.getSession().setAttribute(simpleCaptcha, map.get("strEnsure").toString().toLowerCase()); request.getSession().setAttribute("codeTime",new Date().getTime()); try { ImageIO.write((BufferedImage) map.get("image"), "JPEG", os); } catch (IOException e) { return ""; } return null; }
3、驗(yàn)證驗(yàn)證碼API
@RequestMapping(value = "/checkcode") @ResponseBody public String checkcode(HttpServletRequest request, HttpSession session) throws Exception { String checkCode = request.getParameter("checkCode"); Object cko = session.getAttribute("simpleCaptcha") ; //驗(yàn)證碼對象 if(cko == null){ request.setAttribute("errorMsg", "驗(yàn)證碼已失效,請重新輸入!"); return "驗(yàn)證碼已失效,請重新輸入!"; } String captcha = cko.toString(); Date now = new Date(); Long codeTime = Long.valueOf(session.getAttribute("codeTime")+""); if(StringUtils.isEmpty(checkCode) || captcha == null || !(checkCode.equalsIgnoreCase(captcha))) { request.setAttribute("errorMsg", "驗(yàn)證碼錯誤!"); return "驗(yàn)證碼錯誤!"; } else if ((now.getTime()-codeTime)/1000/60>5) { //驗(yàn)證碼有效時長為5分鐘 request.setAttribute("errorMsg", "驗(yàn)證碼已失效,請重新輸入!"); return "驗(yàn)證碼已失效,請重新輸入!"; }else { session.removeAttribute("simpleCaptcha"); return "1"; } }
關(guān)于使用Spring Boot怎么樣實(shí)現(xiàn)一個驗(yàn)證碼生成功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。