ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

captcha code

terrytornadoTop Contributor
1 Jul 2011 09:06:58 GMT
1 Jul 2011 09:06:58 GMT

Hi all,

does anyone have integrate a non zk captcha component while working with ce edition and can post the codes for this.

thanks
Stephan

mjablonskiTop Contributor
1 Jul 2011 09:18:12 GMT
1 Jul 2011 09:18:12 GMT

Hi Stephan,

I'm using a non-ZK-Captcha based on SimpleCaptcha for my JeaseCMS. You can see it in use here:

http://www.jease.org/blog/quotation_marks/

Although it is not integrated as "native" ZK component, the usage within ZK is very straighforward. Building a server side wrapper for this should be a job of less than an hour:

<zk> 
 <vlayout id="input" width="100%"> 
   <image src="/site/service/Captcha.jsp?id=myCaptcha" /> 
   Please enter the code: 
   <textbox id="code" hflex="1" /> 
   <button label="Submit"> 
    <attribute name="onClick"><![CDATA[ 
      if (code.getValue().equals( 
          Sessions.getCurrent().getNativeSession().getAttribute("myCaptcha"))) { 
        alert("Code is correct"); 
      } else { 
        alert("Code is wrong"); 
      } 
    ]]></attribute> 
   </button> 
 </vlayout> 
</zk> 

All you need is this JSP... you can transform it easily into a servlet as well:

http://code.google.com/p/jease/source/browse/trunk/Jease/site/service/Captcha.jsp

And the corresponding jar-file:

http://jease.googlecode.com/svn/trunk/Jease/WEB-INF/lib/simplecaptcha.jar

Cheers, Maik

terrytornadoTop Contributor
1 Jul 2011 09:27:55 GMT
1 Jul 2011 09:27:55 GMT

Thank you Maik. I will study this.

Many thanks
Stephan

PS: I'm wondering why such an global used comp is not in the ce version ?????

TonyQ
1 Jul 2011 09:58:50 GMT
1 Jul 2011 09:58:50 GMT

Here's also an captcha component that base on google reCaptcha service.

https://github.com/tony1223/zk-recaptcha

terrytornadoTop Contributor
1 Jul 2011 11:12:21 GMT
1 Jul 2011 11:12:21 GMT

Maik. What version you use?

I have problems with GradiatedBackgroundProducer( Color.WHITE, Color.LIGHT_GRAY ) .
In my loaded version (v. 1.1.1) it accepts no arguments.
I need a version which is available from a maven repo.

best
Stephan

mjablonskiTop Contributor
1 Jul 2011 12:09:35 GMT
1 Jul 2011 12:09:35 GMT

I'm using 1.1.1 which is available via SourceForge:

http://sourceforge.net/projects/simplecaptcha/files/

I just compared this jar-file with the one available via Maven (and declared as 1.1.1). There are differences between this two jars, as for example the GradiatedBackgroundProducer does take no arguments for the Maven version. If you still want to go with Maven, just leave out the parameters and use the default constructor.

Cheers, Maik

terrytornadoTop Contributor
1 Jul 2011 14:57:52 GMT
1 Jul 2011 14:57:52 GMT

Great Maik,
thanks TonyQ too,

here is the pic from the 'forgotten password' dialog.
I will try to integrate the codes for all in the Zksample2 too.

thanks
Stephan

CaptchaUtils.java:

import nl.captcha.Captcha;
import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.text.renderer.ColoredEdgesWordRenderer;

/**
 * EN: Utility class for creating a CAPTCHA.<br>
 * Captcha can be direct loaded into a org.zkoss.zul.Image<br>
 * DE: Hilfsklasse zur Erzeugung eines CAPTCHA.<br>
 * Captcha kann direkt in ein org.zkoss.zul.Image eingelesen werden.
 * 
 * <pre>
 * Image img = new org.zkoss.zul.Image();
 * img.setContent(CaptchaUtils.getCaptcha().getImage());
 * 
 * String verifyStr = captcha.getAnswer();
 * </pre>
 * 
 * @author Stephan Gerth
 */
public class FDCaptchaUtils {

	public FDCaptchaUtils() {
	}

	/**
	 * Create a 5 digits captcha.
	 * 
	 * @return
	 */
	public static Captcha getCaptcha() {

		Captcha captcha = new Captcha.Builder(140, 50)
                        .addText(new ColoredEdgesWordRenderer()).addNoise()
                        .addBackground(new GradiatedBackgroundProducer())
                        .addBorder()
                        .build();

		return captcha;
	}

}

Pieces of the zul view:

    . . .
						<grid sclass="GridPlain"
							style="padding-right: 15px;">
							<columns>
								<column align="right" width="180px" />
								<column />
							</columns>
							<rows>

								<!-- Captcha / Captcha -->
								<row>
									<label
										value="${c:l('common.Captcha')}" />
									<hbox>



====>>>									<image id="img_Captcha" />



										<toolbarbutton id="btnReCaptcha"
											image="/images/icons/refresh_green_16x16.gif"
											tooltiptext="${c:l('common.ReCaptcha')}" />
									</hbox>
								</row>
								<!-- Verify Captcha / Verifiziere Captcha -->
								<row>
									<label
										value="${c:l('common.Repeat')}" />
									<hbox>
										<textbox id="txtb_VerifyCaptcha"
											maxlength="6" width="140px" />
										<toolbarbutton
											id="btnVerifyCaptcha" image="/images/icons/start_16x16.gif"
											tooltiptext="${c:l('common.Check.Input')}" />
									</hbox>
								</row>
							</rows>
						</grid>

	. . .

Pieces of the Controller:

public class ForgottenPasswordCtrl extends GenericForwardComposer implements Serializable {

   . . .
	protected Window windowForgottenPassword; // autowired

	protected Image img_Captcha; // autowired
	protected Textbox txtb_EmailAddress; // autowired
	protected Textbox txtb_VerifyCaptcha; // autowired
	protected Button btnSendPassword; // autowired

	private Captcha captcha;

   . . .

	public void onCreate$windowForgottenPassword(Event event) throws Exception {

		// Create the captcha and set it as image
		doReCaptcha(event);

		windowForgottenPassword.doModal();
	}

	public void onClick$btnClose(Event event) throws InterruptedException {...
	public void onClick$btnSendPassword(Event event) {...
	public void onClick$btnReCaptcha(Event event) {...
	public void onClick$btnVerifyCaptcha(Event event) {...

    . . .

	/**
	 * Do a re-Captcha.<br>
	 * 
	 * @param event
	 */
	private void doReCaptcha(Event event) {
		// init: disable the send password button
		btnSendPassword.setDisabled(true);

		setCaptcha(FDCaptchaUtils.getCaptcha());
		img_Captcha.setContent(getCaptcha().getImage());
	}

	/**
	 * 1. Checks if the email address is entered.<br>
	 * 2. Verify the created captcha digits against the users input from a textbox.<br>
	 * 3. Enable/disable the 'reset password' button.<br>
	 * 
	 * @param event
	 */
	private void doVerifyCaptcha(Event event) {

		// init: disable the send password button
		btnSendPassword.setDisabled(true);

		// check if the email address is entered
		if (StringUtils.isEmpty(txtb_EmailAddress.getValue())) {
			throw new WrongValueException(txtb_EmailAddress, Labels.getLabel("message.Error.CannotBeEmpty"));
		}

		// check if the captcha is verified correctly
		if (StringUtils.equals(txtb_VerifyCaptcha.getValue(), getCaptcha().getAnswer())) {
			btnSendPassword.setDisabled(false);
		}
	}

    . . .

        // +++++++++++++++++++++++++++++++++++++++++++++++++ //
	// ++++++++++++++++ Setter/Getter ++++++++++++++++++ //
	// +++++++++++++++++++++++++++++++++++++++++++++++++ //

	public void setCaptcha(Captcha captcha) {
		this.captcha = captcha;
	}

	public Captcha getCaptcha() {
		return captcha;
	}

    . . . 

mayurk
12 Mar 2012 08:26:19 GMT
12 Mar 2012 08:26:19 GMT

Hey Guys,

It really works fine.

Thanks to all of you