[jbossseam-issues] [JBoss JIRA] Commented: (JBSEAM-865) Allow customisation of Captcha images

Christian Bauer (JIRA) jira-events at lists.jboss.org
Sat Apr 21 03:59:30 EDT 2007


    [ http://jira.jboss.com/jira/browse/JBSEAM-865?page=comments#action_12360129 ] 
            
Christian Bauer commented on JBSEAM-865:
----------------------------------------

I had a look at jCaptcha and man, this is a great example of over-engineering. We don't need all that stuff it is doing, it has its own abstraction of where the captcha value is stored! We have our contexts for that, so writing a captcha is as simple as this (unless you store view/PAGE state on the client, then it breaks):

@Name("org.jboss.seam.captcha.captcha")
@Scope(ScopeType.PAGE)
@Install(precedence = Install.DEPLOYMENT)
public class WikiCaptcha implements Serializable {

    private String question;
    private transient String response;
    private transient Random myRamdom = new SecureRandom();

    public String getQuestion() {
        int one = myRamdom.nextInt(50);
        int two = myRamdom.nextInt(50);
        question = String.valueOf(one + two);
        return one + " + " + two;
    }

    @WikiCaptchaResponse
    public String getResponse() {
        return response;
    }

    public void setResponse(String input) {
        this.response = input;
    }

    public boolean validateResponse(String response) {
        return question.equals(response);
    }

    public static WikiCaptcha instance() {
        if (!Contexts.isPageContextActive()) {
            throw new IllegalStateException("No page context active");
        }
        return (WikiCaptcha) Component.getInstance(WikiCaptcha.class, ScopeType.PAGE);
    }
}

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.METHOD)
@ValidatorClass(WikiCaptchaResponseValidator.class)
public @interface WikiCaptchaResponse {
    String message() default "Your answer was not correct, please try again.";
}

public class WikiCaptchaResponseValidator implements Validator {

    public void initialize(Annotation captchaResponse) {
    }

    public boolean isValid(Object response) {
        return WikiCaptcha.instance().validateResponse((String) response);
    }
}

            <s:validateAll>

                <s:div styleClass="entry">
                    <div class="label">Verification question:</div>
                    <div class="output">
                        What is the result of&#160;<h:outputText value="#{captcha.question}"/>?
                    </div>
                </s:div>

                <s:decorate>
                    <s:div styleClass="entry">
                        <div class="label">Enter response:</div>
                        <div class="input">
                            <h:inputText tabindex="7" size="15" id="verifyCaptcha" value="#{captcha.response}" required="true"/>
                        </div>
                    </s:div>
                </s:decorate>

            </s:validateAll>


This works and I'm using this from now-on forward. If we would make the Seam built-in WikiCaptchaResponse more flexible - the isValid() method should not use instance() but execute a component lookup by name! - I could even remove the two extra classes and have a math captcha in 5 lines.


> Allow customisation of Captcha images
> -------------------------------------
>
>                 Key: JBSEAM-865
>                 URL: http://jira.jboss.com/jira/browse/JBSEAM-865
>             Project: JBoss Seam
>          Issue Type: Feature Request
>          Components: Security
>    Affects Versions: 1.1.6.GA
>            Reporter: Shane Bryzak
>         Assigned To: Shane Bryzak
>            Priority: Minor
>   Original Estimate: 2 hours
>  Remaining Estimate: 2 hours
>
> We should allow configuration of the captcha service to use a different engine.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://jira.jboss.com/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the seam-issues mailing list