用戶登錄注冊之驗證碼技術(shù)實現(xiàn)_第1頁
用戶登錄注冊之驗證碼技術(shù)實現(xiàn)_第2頁
用戶登錄注冊之驗證碼技術(shù)實現(xiàn)_第3頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、用戶登錄注冊之驗證碼技術(shù)實現(xiàn)對于一些惡意強暴破解密碼的行為(即通過硬性嘗試用 戶名密碼進行破解) ,可以采用驗證碼對其進行抵御,對于 一些程序可以識別驗證碼,則需要對驗證碼形式進行多樣化 設計。 注!拿到的頁面是只有圖片,需要用另一個頁面引用圖 片頁面 示例: <img src = "xxx.jsp"> 用戶登錄時設置驗 證碼代碼實現(xiàn):頁面 <script type="text/javascript"> function _change() var imgEle = document.getElem

2、entById("img"); imgEle.src =+ new Date().getTime();</script><BODY><TR><TD style="HEIGHT: 28px"> 驗證碼:</TD><TD style="HEIGHT: 28px"><input type="text" name="verifyCod

3、e" size="1"/><br/><a href="javascript:_change()"> 一張 </a></TD><a href="#"></a><TD style="HEIGHT: 28px"><SPAN id=RequiredFieldValidator4 style=&

4、quot;FONT-WEIGHT: bold;VISIBILITY: hidden; COLOR: white"> 請輸入驗證碼 </SPAN></TD></TR><TR></FORM></BODY>2.action 操作/獲得驗證碼public void getVerify() throws IOException HttpServletRequest request=ServletActionCont

5、ext.getRequest();HttpServletResponse response=ServletActionContext.getResponse();VerifyCode vc=new VerifyCode();BufferedImage image=vc.getImage();/ 獲得圖片request.getSession().setAttribute("session_vcode",vc.g etText();/ 將驗證碼內(nèi)容放在域?qū)ο罄锩?VerifyCode.output(image, response.getOutputStream();public

6、 String login()HttpServletRequest request=ServletActionContext.getRequest();String sessionCode = (String)request.getSession().getAttribute("session_vcode");String paramCode =request.getParameter("verifyCode");if(!paramCode.equalsIgnoreCase(sessionCode) request.setAttribute("

7、msg", " 驗證碼錯誤! "); return "login"User userExit=userService.login(user);if(userExit!=null)if(!userExit.isState()request.setAttribute("msg", " 您尚未激活,請到郵箱"+userExit.getEmail()+"激活!");return "loginerror" request=ServletActionContext.getReq

8、uest(); request.getSession().setAttribute("user",userExit);return "loginsuccess"elsereturn "login"3. 驗證碼代碼實現(xiàn) public class VerifyCode private int w = 70;private int h = 35;private Random r = new Random();/ "宋體", "華文楷體 ", "黑體", "華文新魏 &qu

9、ot;, "華文隸書微軟雅黑","楷體_GB2312"黑體private String fontNames = " 宋體 ", "華文楷體 ", ","微軟雅黑","楷體_GB2312"/ 可選字符private String codes ="23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"/ 背景色private Color bgColor= new Color(255, 255,

10、 255);/ 驗證碼上的文本private String text ;/ 生成隨機的顏色private Color randomColor () int red = r.nextInt(150);int green = r.nextInt(150);int blue = r.nextInt(150);return new Color(red, green, blue);/ 生成隨機的字體private Font randomFont () int index = r.nextInt(fontNames.length);String fontName = fontNamesindex;/ 生成

11、隨機的字 體名稱int style = r.nextInt(4);/ 生成隨機的樣式 , 0( 無樣式 ), 1(粗體), 2(斜體), 3(粗體+斜體)int size = r.nextInt(5) + 24; / 生成隨機字號 , 24 28 return new Font(fontName, style, size);/ 畫干擾線private void drawLine (BufferedImage image) int num = 3;/ 一共畫 3 條Graphics2D g2 = (Graphics2D)image.getGraphics();for(int i = 0; i &

12、amp;lt; num; i+) / 生成兩個點的坐標, 即 4 個值int x1 = r.nextInt(w);int y1 = r.nextInt(h);int x2 = r.nextInt(w);int y2 = r.nextInt(h);g2.setStroke(new BasicStroke(1.5F);g2.setColor(Color.BLUE); / 干擾線是藍色 g2.drawLine(x1, y1, x2, y2);/ 畫線/ 隨機生成一個字符private char randomChar () int index = r.nextInt(codes.length();re

13、turn codes.charAt(index);/ 創(chuàng)建 BufferedImageprivate BufferedImage createImage () BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);Graphics2D g2 = (Graphics2D)image.getGraphics(); g2.setColor(this.bgColor);g2.fillRect(0, 0, w, h);return image;/ 調(diào)用這個方法得到驗證碼public BufferedImage

14、getImage () BufferedImage image = createImage();/ 創(chuàng)建圖片緩 沖區(qū)Graphics2D g2 =(Graphics2D)image.getGraphics();/ 得到繪制環(huán)境StringBuilder sb = new StringBuilder();/用來裝載生成的驗證碼文本/ 向圖片中畫 4 個字符for(int i = 0; i < 4; i+) / 循環(huán)四次,每次生成一個 字符String s = randomChar() + ""/ 隨機生成一個字母 sb.append(s); / 把字母添加到 sb 中float x = i * 1.0F * w / 4; / 設置當前字符的 x 軸坐標 g2.setFont(randomFont(); / 設置隨機字體 g2.setColor(randomColor(); / 設置隨機顏色 g2.drawString(s, x, h-5); / 畫圖this.text = sb.toString(); / 把生成的字符串賦給了this.text

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論