Cage小巧,好用。這里在官方基礎例子基礎上做了擴展:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package ?com.lavasoft.ntv.web.common; import ?com.github.cage.IGenerator; import ?java.util.Random; /** ? * 驗證碼生成器 ? * ? * @author leizhimin 14-5-5 下午2:42 ? */ public ?class ?MyTokenGenerator? implements ?IGenerator<String> { ???? private ?int ?length =? 4 ; ???? private ?String charsetdir =? "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ" ; ???? private ?static ?final ?Random r =? new ?Random(); ???? public ?MyTokenGenerator() { ???? } ???? public ?MyTokenGenerator( int ?length, String charsetdir) { ???????? this .length = length; ???????? this .charsetdir = charsetdir; ???? } ???? @Override ???? public ?String next() { ???????? StringBuffer sb =? new ?StringBuffer(); ???????? int ?len = charsetdir.length(); ???????? for ?( int ?i =? 0 ; i < length; i++) { ???????????? sb.append(charsetdir.charAt(r.nextInt(len -? 1 ))); ???????? } ???????? return ?sb.toString(); ???? } ???? public ?static ?void ?main(String[] args) { ???????? MyTokenGenerator t =? new ?MyTokenGenerator(); ???????? for ?( int ?i =? 0 ; i <? 100 ; i++) { ???????????? System.out.println(t.next()); ???????? } ???? } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package ?com.lavasoft.ntv.web.servlet; import ?com.github.cage.Cage; import ?com.lavasoft.ntv.web.common.MyTokenGenerator; import ?javax.servlet.ServletException; import ?javax.servlet.http.HttpServlet; import ?javax.servlet.http.HttpServletRequest; import ?javax.servlet.http.HttpServletResponse; import ?javax.servlet.http.HttpSession; import ?java.io.IOException; public ?class ?CaptchaServlet? extends ?HttpServlet { ???? private ?static ?final ?long ?serialVersionUID = 1490947492185481844L; ???? private ?static ?final ?Cage cage =? new ?Cage( null , null , null , null , null ,? new ?MyTokenGenerator(), null ); ???? /** ????? * Generates a captcha token and stores it in the session. ????? * ????? * @param session where to store the captcha. ????? */ ???? public ?static ?void ?generateToken(HttpSession session) { ???????? String token = cage.getTokenGenerator().next(); ???????? session.setAttribute( "captchaToken" , token); ???????? markTokenUsed(session,? false ); ???? } ???? /** ????? * Used to retrieve previously stored captcha token from session. ????? * ????? * @param session where the token is possibly stored. ????? * @return token or null if there was none ????? */ ???? public ?static ?String getToken(HttpSession session) { ???????? Object val = session.getAttribute( "captchaToken" ); ???????? return ?val !=? null ?? val.toString() :? null ; ???? } ???? /** ????? * Marks token as used/unused for image generation. ????? * ????? * @param session where the token usage flag is possibly stored. ????? * @param used??? false if the token is not yet used for image generation ????? */ ???? protected ?static ?void ?markTokenUsed(HttpSession session,? boolean ?used) { ???????? session.setAttribute( "captchaTokenUsed" , used); ???? } ???? /** ????? * Checks if the token was used/unused for image generation. ????? * ????? * @param session where the token usage flag is possibly stored. ????? * @return true if the token was marked as unused in the session ????? */ ???? protected ?static ?boolean ?isTokenUsed(HttpSession session) { ???????? return ?!Boolean.FALSE.equals(session.getAttribute( "captchaTokenUsed" )); ???? } ???? @Override ???? protected ?void ?doGet(HttpServletRequest req, HttpServletResponse resp) ???????????? throws ?ServletException, IOException { ???????? HttpSession session = req.getSession( false ); ???????? String token = session !=? null ?? getToken(session) :? null ; ???????? if ?(token ==? null ?|| isTokenUsed(session)) { ???????????? resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Captcha not found." ); ???????????? return ; ???????? } ???????? setResponseHeaders(resp); ???????? markTokenUsed(session,? true ); ???????? cage.draw(token, resp.getOutputStream()); ???? } ???? /** ????? * Helper method, disables HTTP caching. ????? * ????? * @param resp response object to be modified ????? */ ???? protected ?void ?setResponseHeaders(HttpServletResponse resp) { ???????? resp.setContentType( "image/" ?+ cage.getFormat()); ???????? resp.setHeader( "Cache-Control" ,? "no-cache, no-store" ); ???????? resp.setHeader( "Pragma" ,? "no-cache" ); ???????? long ?time = System.currentTimeMillis(); ???????? resp.setDateHeader( "Last-Modified" , time); ???????? resp.setDateHeader( "Date" , time); ???????? resp.setDateHeader( "Expires" , time); ???? } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <%-- ?? Created by IntelliJ IDEA. ?? User: leizhimin 14-5-5 下午12:18 --%> <%@page import="com.lavasoft.ntv.web.servlet.CaptchaServlet"%><%@ ???????? page contentType="text/html" pageEncoding="UTF-8"%><% ???? boolean showGoodResult; ???? boolean showBadResult; ???? if ("POST".equals(request.getMethod())) { ???????? String sessionToken = CaptchaServlet.getToken(session); ???????? String requestToken = request.getParameter("captcha"); ???????? showGoodResult = sessionToken != null && sessionToken.equals(requestToken); ???????? showBadResult = !showGoodResult; ???? } else { ???????? showGoodResult = showBadResult = false; ???? } ???? CaptchaServlet.generateToken(session); %><!DOCTYPE html> < html ?xmlns = "http://www.w3.org/1999/xhtml" > < head > ???? < meta ?charset = "UTF-8" ?/> ???? < title >Captcha Reader</ title > </ head > < body > <%? if (showGoodResult) {%> < h1 ?style = "color: green;" >Your kung fu is good!</ h1 > <%? } else if (showBadResult) {%> < h1 ?style = "color: red;" >This is not right. Try again!</ h1 > <%? } %> < p >Type in the word seen on the picture</ p > < form ?action = "" ?method = "post" > ???? < input ?name = "captcha" ?type = "text" ?autocomplete = "off" ?/> ???? < input ?type = "submit" ?/> </ form > < img ?alt = "captcha image" ?src = "/ntv/captcha" ?width = "120px" ?height = "30px" /> </ body > </ html > |
1 2 3 4 5 6 7 8 | < servlet > ???? < servlet-name >captcha</ servlet-name > ???? < servlet-class >com.lavasoft.ntv.web.servlet.CaptchaServlet</ servlet-class > </ servlet > < servlet-mapping > ???? < servlet-name >captcha</ servlet-name > ???? < url-pattern >/captcha</ url-pattern > </ servlet-mapping > |
訪問頁面:
本文轉自 leizhimin 51CTO博客,原文鏈接:http://blog.51cto.com/lavasoft/1406702,如需轉載請自行聯系原作者