當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
一、新建ValidateCode.java驗證碼工具類
package?org.shiro.demo.util;import?java.util.Random; import?java.awt.image.BufferedImage; import?java.awt.Graphics; import?java.awt.Font; import?java.awt.Color;/***?驗證碼生成器類,可生成數字、大寫、小寫字母及三者混合類型的驗證碼。?支持自定義驗證碼字符數量;?支持自定義驗證碼圖片的大小;?支持自定義需排除的特殊字符;*?支持自定義干擾線的數量;?支持自定義驗證碼圖文顏色*/ public?class?ValidateCode?{/***?驗證碼類型為僅數字?0~9*/public?static?final?int?TYPE_NUM_ONLY?=?0;/***?驗證碼類型為僅字母,即大寫、小寫字母混合*/public?static?final?int?TYPE_LETTER_ONLY?=?1;/***?驗證碼類型為數字、大寫字母、小寫字母混合*/public?static?final?int?TYPE_ALL_MIXED?=?2;/***?驗證碼類型為數字、大寫字母混合*/public?static?final?int?TYPE_NUM_UPPER?=?3;/***?驗證碼類型為數字、小寫字母混合*/public?static?final?int?TYPE_NUM_LOWER?=?4;/***?驗證碼類型為僅大寫字母*/public?static?final?int?TYPE_UPPER_ONLY?=?5;/***?驗證碼類型為僅小寫字母*/public?static?final?int?TYPE_LOWER_ONLY?=?6;private?ValidateCode()?{}/***?生成驗證碼字符串*?*?@param?type*????????????驗證碼類型,參見本類的靜態屬性*?@param?length*????????????驗證碼長度,大于0的整數*?@param?exChars*????????????需排除的特殊字符(僅對數字、字母混合型驗證碼有效,無需排除則為null)*?@return?驗證碼字符串*/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)?{//?僅數字case?TYPE_NUM_ONLY:while?(i?<?length)?{int?t?=?r.nextInt(10);if?(exChars?==?null?||?exChars.indexOf(t?+?"")?<?0)?{//?排除特殊字符code.append(t);i++;}}break;//?僅字母(即大寫字母、小寫字母混合)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;//?數字、大寫字母、小寫字母混合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;//?數字、大寫字母混合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;//?數字、小寫字母混合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;//?僅大寫字母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;//?僅小寫字母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();}/***?已有驗證碼,生成驗證碼圖片*?*?@param?textCode*????????????文本驗證碼*?@param?width*????????????圖片寬度*?@param?height*????????????圖片高度*?@param?interLine*????????????圖片中干擾線的條數*?@param?randomLocation*????????????每個字符的高低位置是否隨機*?@param?backColor*????????????圖片顏色,若為null,則采用隨機顏色*?@param?foreColor*????????????字體顏色,若為null,則采用隨機顏色*?@param?lineColor*????????????干擾線顏色,若為null,則采用隨機顏色*?@return?圖片緩存對象*/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();//?畫背景圖g.setColor(backColor?==?null???getRandomColor()?:?backColor);g.fillRect(0,?0,?width,?height);//?畫干擾線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);}}//?寫驗證碼//?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));//?寫驗證碼字符for?(int?i?=?0;?i?<?textCode.length();?i++)?{fy?=?randomLocation???(int)?((Math.random()?*?0.3?+?0.6)?*?height)?:?fy;//?每個字符高低是否隨機g.setColor(foreColor?==?null???getRandomColor()?:?foreColor);g.drawString(textCode.charAt(i)?+?"",?fx,?fy);fx?+=?fsize?*?0.9;}g.dispose();return?bim;}/***?生成圖片驗證碼*?*?@param?type*????????????驗證碼類型,參見本類的靜態屬性*?@param?length*????????????驗證碼字符長度,大于0的整數*?@param?exChars*????????????需排除的特殊字符*?@param?width*????????????圖片寬度*?@param?height*????????????圖片高度*?@param?interLine*????????????圖片中干擾線的條數*?@param?randomLocation*????????????每個字符的高低位置是否隨機*?@param?backColor*????????????圖片顏色,若為null,則采用隨機顏色*?@param?foreColor*????????????字體顏色,若為null,則采用隨機顏色*?@param?lineColor*????????????干擾線顏色,若為null,則采用隨機顏色*?@return?圖片緩存對象*/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;}/***?產生隨機顏色*?*?@return*/private?static?Color?getRandomColor()?{Random?r?=?new?Random();Color?c?=?new?Color(r.nextInt(255),?r.nextInt(255),?r.nextInt(255));return?c;}}二、修改UserController.java的實現?
package?org.shiro.demo.controller;import?java.awt.Color; import?java.awt.image.BufferedImage; import?java.io.IOException;import?javax.imageio.ImageIO; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; import?javax.servlet.http.HttpSession;import?org.apache.commons.lang.StringUtils; import?org.apache.shiro.SecurityUtils; import?org.apache.shiro.authc.AuthenticationException; import?org.apache.shiro.authc.UsernamePasswordToken; import?org.apache.shiro.subject.Subject; import?org.apache.shiro.web.util.WebUtils; import?org.shiro.demo.entity.User; import?org.shiro.demo.util.ValidateCode; import?org.springframework.stereotype.Controller; import?org.springframework.web.bind.annotation.RequestMapping; import?org.springframework.web.bind.annotation.RequestMethod;@Controller public?class?LoginController?{@RequestMapping(value?=?"/login"?,method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})public?String?login(User?currUser,HttpSession?session,?HttpServletRequest?request){String?code?=?(String)?session.getAttribute("validateCode");String?submitCode?=?WebUtils.getCleanParam(request,?"validateCode");if?(StringUtils.isEmpty(submitCode)?||?!StringUtils.equals(code,submitCode.toLowerCase()))?{return?"redirect:/";}Subject?user?=?SecurityUtils.getSubject();UsernamePasswordToken?token?=?new?UsernamePasswordToken(currUser.getAccount(),currUser.getPassword());token.setRememberMe(true);try?{user.login(token);return?"/system/main";}catch?(AuthenticationException?e)?{token.clear();return?"redirect:/";}}/***?生成驗證碼*?@param?request*?@param?response*?@throws?IOException*/@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_ONLY,?4,?null);request.getSession().setAttribute("validateCode",?verifyCode);response.setContentType("image/jpeg");BufferedImage?bim?=?ValidateCode.generateImageCode(verifyCode,?90,?30,?3,?true,?Color.WHITE,?Color.BLACK,?null);ImageIO.write(bim,?"JPEG",?response.getOutputStream());} }三、修改login.jsp
<%@?page?language="java"?pageEncoding="utf-8"%> <%@?taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"?%> <% String?path?=?request.getContextPath(); String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; %><!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"> <html><head><base?href="<%=basePath%>"><title>shirodemo?login?page</title><meta?http-equiv="content-type"?content="text/html;?charset=UTF-8"><script?type="text/javascript"?src='<c:url?value="/resources/js/jquery-1.6.3.min.js"/>'></script><script?type="text/javascript"><!--function?reloadValidateCode(){$("#validateCodeImg").attr("src","<%=basePath%>/validateCode?data="?+?new?Date()?+?Math.floor(Math.random()*24));}//--></script></head><body><form?action="<%=basePath%>/login"?method="post"><ul><li>姓 名:<input?type="text"?name="account"?/>?</li><li>密 碼:<input?type="text"?name="password"?/>?</li><li>驗證碼:<input?type="text"?name="validateCode"?/> <img?id="validateCodeImg"?src="<%=basePath%>/validateCode"?/> <a?href="#"?onclick="javascript:reloadValidateCode();">看不清?</a></li><li><input?type="submit"?value="確認"?/>?</li></ul></form></body> </html>至此,就給你的登陸頁面加上驗證碼了。訪問你的login.jsp試試?
轉載于:https://my.oschina.net/heroShane/blog/198381
總結
以上是生活随笔為你收集整理的SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dynamics CRM 提示“操作无效
- 下一篇: rtp rtcp rtsp