好的,所以我最终使用了Cage Captcha generator。它可以与 Maven 集成,并且很容易通过带有 JQuery AJAX 的 Spring MVC 应用程序实现。
/**
* Generates captcha as image and returns the image path
* stores the captcha code in the http session
* and deletes older, unused captcha images.
*/
@RequestMapping(value = "/captcha/generate", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public ResponseEntity<CaptchaRequestData> generateCaptcha(HttpSession session) {
String captchaImageUploadDirectory = environment.getProperty("captcha_image_folder");
String captchaWebAlias = environment.getProperty("captcha_web_alias");
//Creating dir or making new one if it doesn't exist
File file = new File(captchaImageUploadDirectory);
if (!file.exists()) {
try {
file.mkdirs();
} catch(Exception e){}
}
String timeSuffix = DBUtils.getDateTimeAsString();
String fileName = CAPTCHA_IMAGE_PREFIX + timeSuffix + "." + CAPTCHA_IMAGE_EXTENSION;
String fullFilename = captchaImageUploadDirectory + fileName;
//Generating the captcha code and setting max length to 4 symbols
Cage currGcage = new YCage();
String captchaToken = currGcage.getTokenGenerator().next();
if (captchaToken.length() > CAPTCHA_CODE_MAX_LENGTH) {
captchaToken = captchaToken.substring(0, CAPTCHA_CODE_MAX_LENGTH).toUpperCase();
}
//Setting the captcha token in http session
session.setAttribute("captchaToken", captchaToken);
try {
OutputStream os = new FileOutputStream(fullFilename, false);
currGcage.draw(captchaToken, os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
deleteFilesOlderThan(captchaImageUploadDirectory, CAPTCHA_IMAGE_LIFE_MILLISECONDS, CAPTCHA_IMAGE_EXTENSION);
CaptchaRequestData data = new CaptchaRequestData(captchaWebAlias + fileName);
return new ResponseEntity<>(data, HttpStatus.OK);
}
然后,当我创建对象时,我检查给定的代码是否等于存储在会话中的代码:
if (!httpSession.getAttribute("captchaToken").equals(bindingData.getCaptchaCode())) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
最后,如果提供的验证码不正确,我会生成一个新的验证码。