Commit 692f8820 authored by Liang Ding's avatar Liang Ding

Fixed #101

只生成了 100 个静态验证码。
parent 819c196d
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration> <project-shared-configuration>
<!-- <!--
This file contains additional configuration written by modules in the NetBeans IDE. This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout. therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether. Without this configuration present, some functionality in the IDE may be limited or fail altogether.
--> -->
<spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1"> <spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1">
<word>Admin</word> <word>Admin</word>
...@@ -14,6 +14,7 @@ Without this configuration present, some functionality in the IDE may be limited ...@@ -14,6 +14,7 @@ Without this configuration present, some functionality in the IDE may be limited
<word>blogging</word> <word>blogging</word>
<word>captcha</word> <word>captcha</word>
<word>Captcha</word> <word>Captcha</word>
<word>Captchas</word>
<word>cron</word> <word>cron</word>
<word>datastore</word> <word>datastore</word>
<word>ftl</word> <word>ftl</word>
......
...@@ -17,16 +17,10 @@ package org.b3log.solo.processor; ...@@ -17,16 +17,10 @@ package org.b3log.solo.processor;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.b3log.latke.image.Image; import org.b3log.latke.image.Image;
import java.util.Random; import java.util.Random;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
...@@ -46,14 +40,12 @@ import org.b3log.solo.SoloServletListener; ...@@ -46,14 +40,12 @@ import org.b3log.solo.SoloServletListener;
* Captcha processor. * Captcha processor.
* *
* <p> * <p>
* See <a href="http://isend-blog.appspot.com/2010/03/25/captcha_on_GAE.html"> * Checkout <a href="http://toy-code.googlecode.com/svn/trunk/CaptchaGenerator">
* 在GAE上拼接生成图形验证码</a> for philosophy. Checkout * the sample captcha generator</a> for more details.
* <a href="http://toy-code.googlecode.com/svn/trunk/CaptchaGenerator">
* the sample captcha generator</a> for mor details.
* </p> * </p>
* *
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.1.0.1, Oct 10, 2011 * @version 1.1.0.2, Sep 18, 2012
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -63,43 +55,22 @@ public final class CaptchaProcessor { ...@@ -63,43 +55,22 @@ public final class CaptchaProcessor {
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(CaptchaProcessor.class.getName()); private static final Logger LOGGER = Logger.getLogger(CaptchaProcessor.class.getName());
/**
* Length of captcha.
*/
private static final int LENGTH = 4;
/** /**
* Images service. * Images service.
*/ */
private static final ImageService IMAGE_SERVICE = ImageServiceFactory.getImageService(); private static final ImageService IMAGE_SERVICE = ImageServiceFactory.getImageService();
/**
* Random.
*/
private static final Random RANDOM = new Random();
/** /**
* Key of captcha. * Key of captcha.
*/ */
public static final String CAPTCHA = "captcha"; public static final String CAPTCHA = "captcha";
/** /**
* Maximum captcha row. * Captchas.
*/
public static final int MAX_CAPTCHA_ROW = 10;
/**
* Maximum captcha column.
*/
public static final int MAX_CAPTCHA_COLUM = 10;
/**
* Width of a captcha character.
*/
public static final int WIDTH_CAPTCHA_CHAR = 13;
/**
* Height of a captcha character.
*/ */
public static final int HEIGHT_CAPTCHA_CHAR = 20; private Image[] captchas;
/** /**
* Captcha &lt;"imageName", Image&gt;. * Count of static captchas.
* For example &lt;"0/5.png", Image&gt;.
*/ */
private static final Map<String, Image> CAPTCHAS = new HashMap<String, Image>(); private static final int CAPTCHA_COUNT = 100;
/** /**
* Gets captcha. * Gets captcha.
...@@ -111,28 +82,19 @@ public final class CaptchaProcessor { ...@@ -111,28 +82,19 @@ public final class CaptchaProcessor {
final PNGRenderer renderer = new PNGRenderer(); final PNGRenderer renderer = new PNGRenderer();
context.setRenderer(renderer); context.setRenderer(renderer);
if (CAPTCHAS.isEmpty()) { if (null == captchas) {
loadCaptchas(); loadCaptchas();
} }
try { try {
final String row = String.valueOf(RANDOM.nextInt(MAX_CAPTCHA_ROW));
String captcha = "";
final List<Image> images = new ArrayList<Image>();
for (int i = 0; i < LENGTH; i++) {
final String column = String.valueOf(RANDOM.nextInt(MAX_CAPTCHA_COLUM));
captcha += column;
final String imageName = row + "/" + column + ".png";
final Image captchaChar = CAPTCHAS.get(imageName);
images.add(captchaChar);
}
final Image captchaImage = IMAGE_SERVICE.makeImage(images);
final HttpServletRequest request = context.getRequest(); final HttpServletRequest request = context.getRequest();
final HttpServletResponse response = context.getResponse(); final HttpServletResponse response = context.getResponse();
final Random random = new Random();
final int index = random.nextInt(CAPTCHA_COUNT);
final Image captchaImg = captchas[index];
final String captcha = captchaImg.getName();
final HttpSession httpSession = request.getSession(false); final HttpSession httpSession = request.getSession(false);
if (null != httpSession) { if (null != httpSession) {
LOGGER.log(Level.FINER, "Captcha[{0}] for session[id={1}]", new Object[]{captcha, httpSession.getId()}); LOGGER.log(Level.FINER, "Captcha[{0}] for session[id={1}]", new Object[]{captcha, httpSession.getId()});
...@@ -143,7 +105,7 @@ public final class CaptchaProcessor { ...@@ -143,7 +105,7 @@ public final class CaptchaProcessor {
response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0); response.setDateHeader("Expires", 0);
renderer.setImage(captchaImage); renderer.setImage(captchaImg);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e); LOGGER.log(Level.SEVERE, e.getMessage(), e);
} }
...@@ -152,34 +114,32 @@ public final class CaptchaProcessor { ...@@ -152,34 +114,32 @@ public final class CaptchaProcessor {
/** /**
* Loads captcha. * Loads captcha.
*/ */
private static void loadCaptchas() { private synchronized void loadCaptchas() {
LOGGER.info("Loading captchas...."); LOGGER.info("Loading captchas....");
try { try {
final URL captchaURL = SoloServletListener.class.getClassLoader().getResource("captcha.zip"); captchas = new Image[CAPTCHA_COUNT];
final ZipFile zipFile = new ZipFile(captchaURL.getFile());
final Set<String> imageNames = new HashSet<String>();
for (int row = 0; row < MAX_CAPTCHA_ROW; row++) {
for (int column = 0; column < MAX_CAPTCHA_COLUM; column++) {
imageNames.add(row + "/" + column + ".png");
}
} final URL captchaURL = SoloServletListener.class.getClassLoader().getResource("captcha_static.zip");
final ZipFile zipFile = new ZipFile(captchaURL.getFile());
final ImageService imageService = ImageServiceFactory.getImageService(); final Enumeration<? extends ZipEntry> entries = zipFile.entries();
final Iterator<String> i = imageNames.iterator(); int i = 0;
while (i.hasNext()) { while (entries.hasMoreElements()) {
final String imageName = i.next(); final ZipEntry entry = entries.nextElement();
final ZipEntry zipEntry = zipFile.getEntry(imageName);
final BufferedInputStream bufferedInputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); final BufferedInputStream bufferedInputStream = new BufferedInputStream(zipFile.getInputStream(entry));
final byte[] captchaCharData = new byte[bufferedInputStream.available()]; final byte[] captchaCharData = new byte[bufferedInputStream.available()];
bufferedInputStream.read(captchaCharData); bufferedInputStream.read(captchaCharData);
bufferedInputStream.close(); bufferedInputStream.close();
final Image captchaChar = imageService.makeImage(captchaCharData); final Image image = IMAGE_SERVICE.makeImage(captchaCharData);
image.setName(entry.getName().substring(0, entry.getName().lastIndexOf('.')));
captchas[i] = image;
CAPTCHAS.put(imageName, captchaChar); i++;
} }
zipFile.close(); zipFile.close();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment