Java中SSM+Shiro系統(tǒng)登錄驗(yàn)證碼的實(shí)現(xiàn)方法
先給大家展示下效果圖:
1、驗(yàn)證碼生成類(lèi):
import java.util.Random; import java.awt.image.BufferedImage; import java.awt.Graphics; import java.awt.Font; import java.awt.Color; /** * 驗(yàn)證碼生成器類(lèi),可生成數(shù)字、大寫(xiě)、小寫(xiě)字母及三者混合類(lèi)型的驗(yàn)證碼。 支持自定義驗(yàn)證碼字符數(shù)量; 支持自定義驗(yàn)證碼圖片的大??; 支持自定義需排除的特殊字符; * 支持自定義干擾線(xiàn)的數(shù)量; 支持自定義驗(yàn)證碼圖文顏色 */ public class ValidateCode { /** * 驗(yàn)證碼類(lèi)型為僅數(shù)字 0~9 */ public static final int TYPE_NUM_ONLY = 0; /** * 驗(yàn)證碼類(lèi)型為僅字母,即大寫(xiě)、小寫(xiě)字母混合 */ public static final int TYPE_LETTER_ONLY = 1; /** * 驗(yàn)證碼類(lèi)型為數(shù)字、大寫(xiě)字母、小寫(xiě)字母混合 */ public static final int TYPE_ALL_MIXED = 2; /** * 驗(yàn)證碼類(lèi)型為數(shù)字、大寫(xiě)字母混合 */ public static final int TYPE_NUM_UPPER = 3; /** * 驗(yàn)證碼類(lèi)型為數(shù)字、小寫(xiě)字母混合 */ public static final int TYPE_NUM_LOWER = 4; /** * 驗(yàn)證碼類(lèi)型為僅大寫(xiě)字母 */ public static final int TYPE_UPPER_ONLY = 5; /** * 驗(yàn)證碼類(lèi)型為僅小寫(xiě)字母 */ public static final int TYPE_LOWER_ONLY = 6; private ValidateCode() { } /** * 生成驗(yàn)證碼字符串 * * @param type * 驗(yàn)證碼類(lèi)型,參見(jiàn)本類(lèi)的靜態(tài)屬性 * @param length * 驗(yàn)證碼長(zhǎng)度,大于0的整數(shù) * @param exChars * 需排除的特殊字符(僅對(duì)數(shù)字、字母混合型驗(yàn)證碼有效,無(wú)需排除則為null) * @return 驗(yàn)證碼字符串 */ public static String generateTextCode(int type, int length, String exChars) { if (length <= 0) return ""; StringBuffer code = new StringBuffer(); int i = 0; Random r = new Random(); switch (type) { // 僅數(shù)字 case TYPE_NUM_ONLY: while (i < length) { int t = r.nextInt(10); if (exChars == null || exChars.indexOf(t + "") < 0) {// 排除特殊字符 code.append(t); i++; } } break; // 僅字母(即大寫(xiě)字母、小寫(xiě)字母混合) case TYPE_LETTER_ONLY: while (i < length) { int t = r.nextInt(123); if ((t >= 97 || (t >= 65 && t <= 90)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 數(shù)字、大寫(xiě)字母、小寫(xiě)字母混合 case TYPE_ALL_MIXED: while (i < length) { int t = r.nextInt(123); if ((t >= 97 || (t >= 65 && t <= 90) || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 數(shù)字、大寫(xiě)字母混合 case TYPE_NUM_UPPER: while (i < length) { int t = r.nextInt(91); if ((t >= 65 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 數(shù)字、小寫(xiě)字母混合 case TYPE_NUM_LOWER: while (i < length) { int t = r.nextInt(123); if ((t >= 97 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 僅大寫(xiě)字母 case TYPE_UPPER_ONLY: while (i < length) { int t = r.nextInt(91); if ((t >= 65) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; // 僅小寫(xiě)字母 case TYPE_LOWER_ONLY: while (i < length) { int t = r.nextInt(123); if ((t >= 97) && (exChars == null || exChars.indexOf((char) t) < 0)) { code.append((char) t); i++; } } break; } return code.toString(); } /** * 已有驗(yàn)證碼,生成驗(yàn)證碼圖片 * * @param textCode * 文本驗(yàn)證碼 * @param width * 圖片寬度 * @param height * 圖片高度 * @param interLine * 圖片中干擾線(xiàn)的條數(shù) * @param randomLocation * 每個(gè)字符的高低位置是否隨機(jī) * @param backColor * 圖片顏色,若為null,則采用隨機(jī)顏色 * @param foreColor * 字體顏色,若為null,則采用隨機(jī)顏色 * @param lineColor * 干擾線(xiàn)顏色,若為null,則采用隨機(jī)顏色 * @return 圖片緩存對(duì)象 */ public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) { BufferedImage bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = bim.getGraphics(); // 畫(huà)背景圖 g.setColor(backColor == null ? getRandomColor() : backColor); g.fillRect(0, 0, width, height); // 畫(huà)干擾線(xiàn) Random r = new Random(); if (interLine > 0) { int x = 0, y = 0, x1 = width, y1 = 0; for (int i = 0; i < interLine; i++) { g.setColor(lineColor == null ? getRandomColor() : lineColor); y = r.nextInt(height); y1 = r.nextInt(height); g.drawLine(x, y, x1, y1); } } // 寫(xiě)驗(yàn)證碼 // g.setColor(getRandomColor()); // g.setColor(isSimpleColor?Color.BLACK:Color.WHITE); // 字體大小為圖片高度的80% int fsize = (int) (height * 0.8); int fx = height - fsize; int fy = fsize; g.setFont(new Font("Default", Font.PLAIN, fsize)); // 寫(xiě)驗(yàn)證碼字符 for (int i = 0; i < textCode.length(); i++) { fy = randomLocation ? (int) ((Math.random() * 0.3 + 0.6) * height) : fy;// 每個(gè)字符高低是否隨機(jī) g.setColor(foreColor == null ? getRandomColor() : foreColor); g.drawString(textCode.charAt(i) + "", fx, fy); fx += fsize * 0.9; } g.dispose(); return bim; } /** * 生成圖片驗(yàn)證碼 * * @param type * 驗(yàn)證碼類(lèi)型,參見(jiàn)本類(lèi)的靜態(tài)屬性 * @param length * 驗(yàn)證碼字符長(zhǎng)度,大于0的整數(shù) * @param exChars * 需排除的特殊字符 * @param width * 圖片寬度 * @param height * 圖片高度 * @param interLine * 圖片中干擾線(xiàn)的條數(shù) * @param randomLocation * 每個(gè)字符的高低位置是否隨機(jī) * @param backColor * 圖片顏色,若為null,則采用隨機(jī)顏色 * @param foreColor * 字體顏色,若為null,則采用隨機(jī)顏色 * @param lineColor * 干擾線(xiàn)顏色,若為null,則采用隨機(jī)顏色 * @return 圖片緩存對(duì)象 */ public static BufferedImage generateImageCode(int type, int length, String exChars, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) { String textCode = generateTextCode(type, length, exChars); BufferedImage bim = generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor, lineColor); return bim; } /** * 產(chǎn)生隨機(jī)顏色 * * @return */ private static Color getRandomColor() { Random r = new Random(); Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); return c; } }
2、Controller
/** * 生成驗(yàn)證碼 * @param request * @param response * @throws IOException * @ValidateCode.generateTextCode(驗(yàn)證碼字符類(lèi)型,驗(yàn)證碼長(zhǎng)度,需排除的特殊字符) * @ValidateCode.generateImageCode(文本驗(yàn)證碼,圖片寬度,圖片高度,干擾線(xiàn)的條數(shù),字符的高低位置是否隨機(jī),圖片顏色,字體顏色,干擾線(xiàn)顏色) */ @RequestMapping(value = "validateCode") public void validateCode(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("Cache-Control", "no-cache"); String verifyCode = ValidateCode.generateTextCode(ValidateCode.TYPE_NUM_LOWER, 4, null); request.getSession().setAttribute("validateCode", verifyCode); response.setContentType("image/jpeg"); BufferedImage bim = ValidateCode.generateImageCode(verifyCode, 90, 30, 5, true, Color.WHITE, Color.BLUE, null); ImageIO.write(bim, "JPEG", response.getOutputStream()); } /** * 登錄請(qǐng)求 * @param */ @RequestMapping(value = "login", method = RequestMethod.POST, produces = "text/html; charset=utf-8") public String login(HttpServletRequest request, HttpServletResponse response, UserEntity user) { //首先進(jìn)行驗(yàn)證碼驗(yàn)證 Session session = SecurityUtils.getSubject().getSession(); String code = (String) session.getAttribute("validateCode"); String submitCode = WebUtils.getCleanParam(request, "validateCode"); if (StringUtils.isEmpty(submitCode) || !StringUtils.equals(code,submitCode.toLowerCase())) { request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100000); request.setAttribute("LOGIN_ERROR_MESSAGE", LoginConstant.LOGIN_ERROR_MESSAGE_VALIDATECODE); return "login"; } // 想要得到 SecurityUtils.getSubject() 的對(duì)象..訪(fǎng)問(wèn)地址必須跟shiro的攔截地址內(nèi).不然后會(huì)報(bào)空指針 Subject sub = SecurityUtils.getSubject(); // 用戶(hù)輸入的賬號(hào)和密碼,,存到UsernamePasswordToken對(duì)象中..然后由shiro內(nèi)部認(rèn)證對(duì)比, // 認(rèn)證執(zhí)行者交由ShiroDbRealm中doGetAuthenticationInfo處理 // 當(dāng)以上認(rèn)證成功后會(huì)向下執(zhí)行,認(rèn)證失敗會(huì)拋出異常 UsernamePasswordToken token = new UsernamePasswordToken(user.getAccountName(), user.getPassWord()); try { sub.login(token); } catch (LockedAccountException lae) { token.clear(); request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100002); request.setAttribute("LOGIN_ERROR_MESSAGE", LoginConstant.LOGIN_ERROR_MESSAGE_SYSTEMERROR); return "login"; } catch (ExcessiveAttemptsException e) { token.clear(); request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100003); request.setAttribute("LOGIN_ERROR_MESSAGE","賬號(hào):" + user.getUserName() + LoginConstant.LOGIN_ERROR_MESSAGE_MAXERROR); return "login"; } catch (AuthenticationException e) { token.clear(); request.setAttribute("LOGIN_ERROR_CODE", LoginConstant.LOGIN_ERROR_CODE_100001); request.setAttribute("LOGIN_ERROR_MESSAGE", LoginConstant.LOGIN_ERROR_MESSAGE_USERERROR); return "login"; } return "redirect:/index.shtml"; }
注意:
登錄方法里面一些參數(shù)的定義:
public interface LoginConstant { String LOGIN_ERROR_CODE_100000 = "100000"; String LOGIN_ERROR_MESSAGE_VALIDATECODE = "驗(yàn)證碼輸入錯(cuò)誤,請(qǐng)重新輸入!"; String LOGIN_ERROR_CODE_100001 = "100001"; String LOGIN_ERROR_MESSAGE_USERERROR = "賬號(hào)或密碼錯(cuò)誤,請(qǐng)重新輸入!"; String LOGIN_ERROR_CODE_100002 = "100002"; String LOGIN_ERROR_MESSAGE_SYSTEMERROR = "用戶(hù)已經(jīng)被鎖定不能登錄,請(qǐng)與管理員聯(lián)系!"; String LOGIN_ERROR_CODE_100003 = "100003"; String LOGIN_ERROR_MESSAGE_MAXERROR = "登錄失敗次數(shù)過(guò)多,鎖定10分鐘!"; String LOGIN_ERROR_CODE_100004 = "100004"; String LOGIN_ERROR_MESSAGE_FORCELOGOUT = "您已經(jīng)被管理員強(qiáng)制退出,請(qǐng)重新登錄"; }
3、登錄jsp(重要代碼)
路徑信息:
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; %>
js:用于更換驗(yàn)證碼圖片
<script> function reloadValidateCode(){ $("#validateCodeImg").attr("src","<%=basePath%>/validateCode.shtml?data=" + new Date() + Math.floor(Math.random()*24)); } </script>
登錄表單里面的標(biāo)簽:
<img id="validateCodeImg" src="<%=basePath%>/validateCode.shtml" /> <a href="#" rel="external nofollow" onclick="javascript:reloadValidateCode();">看不清?</a>
4、Shiro匿名訪(fǎng)問(wèn)配置(不配置無(wú)法生成驗(yàn)證碼圖片)
<!--自定義filterChainDefinitionMap --> <bean id="chainDefinitionSectionMetaSource" class="com.collection.shiro.ChainDefinitionSectionMetaSource"> <property name="filterChainDefinitions"> <value> /validateCode.shtml = anon//添加這行 </value> </property> </bean>
以上所述是小編給大家介紹的Java中SSM+Shiro系統(tǒng)登錄驗(yàn)證碼的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JAVA?IDEA?打開(kāi)assert?設(shè)置方式
這篇文章主要介紹了JAVA?IDEA?打開(kāi)assert?設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Spring中的BeanFactory與FactoryBean區(qū)別詳解
這篇文章主要介紹了Spring中的BeanFactory與FactoryBean區(qū)別詳解,BeanFactory是一個(gè)接口,它是spring中的一個(gè)工廠(chǎng),FactoryBean也是一個(gè)接口,實(shí)現(xiàn)了3個(gè)方法,通過(guò)重寫(xiě)其中方法自定義生成bean,需要的朋友可以參考下2024-01-01java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):算法
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛(ài)好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-07-07Java在Word中插入上標(biāo)和下標(biāo)的實(shí)現(xiàn)方法
在某些情況下,你可能需要在Microsoft?Word中插入上標(biāo)和下標(biāo)。例如,當(dāng)你正在創(chuàng)建一個(gè)涉及科學(xué)公式的學(xué)術(shù)文件時(shí),在這篇文章中,你將學(xué)習(xí)如何使用Spire.Doc?for?Java庫(kù)在Word文檔中插入上標(biāo)和下標(biāo),需要的朋友可以參考下2022-10-10Java Agent 動(dòng)態(tài)修改字節(jié)碼詳情
這篇文章主要介紹了Java Agent動(dòng)態(tài)修改字節(jié)碼的相關(guān)資料,需要的朋友可以參考下面文章具體的內(nèi)容2021-09-09Java Web項(xiàng)目中使用Socket通信多線(xiàn)程、長(zhǎng)連接的方法
很多時(shí)候在javaweb項(xiàng)目中我們需要用到Socket通信來(lái)實(shí)現(xiàn)功能,在web中使用Socket我們需要建立一個(gè)監(jiān)聽(tīng)程序,在程序啟動(dòng)時(shí),啟動(dòng)socket監(jiān)聽(tīng)。接下來(lái)通過(guò)本文給大家介紹Java Web項(xiàng)目中使用Socket通信多線(xiàn)程、長(zhǎng)連接的方法,感興趣的朋友一起學(xué)習(xí)2016-04-04Java中Collection與Collections的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Java中Collection與Collections的區(qū)別,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的同學(xué)可以參考一下2023-06-06JAVA對(duì)象分析之偏向鎖、輕量級(jí)鎖、重量級(jí)鎖升級(jí)過(guò)程
這篇文章主要介紹了JAVA對(duì)象分析之偏向鎖、輕量級(jí)鎖、重量級(jí)鎖升級(jí)過(guò)程,又對(duì)這方面感興趣的同學(xué)可以跟著一起研究下2021-02-02