Issue with ICageCaptcha

I have a problem when I use CageCaptcha. When I try to compare the letters of the captcha image that is kept in a variable "randomText", with the letters written by the user, "randomtext" is always null. So, I can't check if the user wrote the captcha correctly.

I think the problem is that captchaImageResource.invalidate() is not using render() defined in Cageform.java to create the captcha image. Instead, it creates an image without using the render method.

For e.g., when I editing my user's information in my wbesite, where is this captcha implementation, the captcha image is generated (e.g.: asdfg3). Later, When I complete the input text with the captcha letters and press save ( to save all my changes), the "randomText" variable is null, it does not have "asdfg3".

I hope you can help me with this issue. I leave you my implementation below. I am using Java 8 and Wicket 1.5.6.

Thank you!

AbstractCaptchaForm.java

public abstract class AbstractCaptchaForm<T> extends GenericPanel<T> { private static final long serialVersionUID = 1L;  /**  * The generated random text;  */ protected String randomText;  /**  * The text provided by the user  */ private String captchaText; private final CaptchaImageResource captchaImageResource;  /**  * Constructor.  *  * @param id  *          The component id  */ public AbstractCaptchaForm(String id) {     super(id);      Form<T> form = new Form<T>("form")     {         @Override         public void onSubmit()         {             if(captchaText == null)                 error(getString("captchatext_empty"));               else if (!randomText.equals(captchaText))                 error(getString("captchatext_wrong"));              // force redrawing             captchaImageResource.invalidate();         }     };     add(form);       captchaImageResource = createCaptchImageResource();     final Image captchaImage = new Image("image", captchaImageResource);     captchaImage.setOutputMarkupId(true);      form.add(captchaImage);      AjaxLink<Void> changeCaptchaLink = new AjaxLink<Void>("changeLink")     {         @Override         public void onClick(AjaxRequestTarget target)         {             captchaImageResource.invalidate();             target.add(captchaImage);         }     };     form.add(changeCaptchaLink);      form.add(new TextField<String>("text",             new PropertyModel<String>(AbstractCaptchaForm.this, "captchaText"), String.class)     {         @Override         protected final void onComponentTag(final ComponentTag tag)         {             super.onComponentTag(tag);             // clear the field after each render             tag.put("value", "");         }     }); }  protected abstract CaptchaImageResource createCaptchImageResource(); } 

Cageform.java

  public class CageForm<T> extends AbstractCaptchaForm<T> { private static final long serialVersionUID = 1L;  /**  * Constructor.  *  * @param id  *            The component id  */ public CageForm(String id) {     super(id); }  @Override protected CaptchaImageResource createCaptchImageResource() {     return new CaptchaImageResource() {         @Override         protected byte[] render() {             randomText = randomString(6, 8);             Cage cage = new GCage();             return cage.draw(randomText);         }     }; }  static int randomInt(int min, int max) {     return (int) (Math.random() * (max - min) + min); }  static String randomString(int min, int max) {     int num = randomInt(min, max);     byte b[] = new byte[num];     for (int i = 0; i < num; i++)         b[i] = (byte) randomInt('a', 'z');     return new String(b); } 

}

Captcha.java

 public class Captcha {  static int randomInt(int min, int max) {     return (int)(Math.random() * (max - min) + min); }  static String randomString(int min, int max) {     int num = randomInt(min, max);     byte b[] = new byte[num];     for (int i = 0; i < num; i++)         b[i] = (byte)randomInt('a', 'z');     return new String(b); } 

}

Replay

Category: java Time: 2016-07-31 Views: 5
Tags: java captcha

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.138 (s). 12 q(s)