You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "matt.payne" <ma...@gmail.com> on 2008/10/01 21:09:19 UTC
Re: Captcha in Struts2
aarthy wrote:
>
> How Can I implement Captcha in Struts2?
>
see http://code.google.com/p/kaptcha/
I put the logic in a KaptchInterceptor so actions don't need to be aware of
this lib or validation.
Here is the crux of the logic for that interceptor.
<interceptor name="captcha"
class="com.pennmutual.common.struts2.interceptors.KaptchaInterceptor">
<!-- input -->
</interceptor>
// default name of field on submissions that contains the kaptcha
answer
private static final String FIELD_NAME = "kaptcha";
String fieldName = FIELD_NAME;
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String intercept(ActionInvocation invocation) throws Exception {
Object kaptchaExpected = ActionContext.getContext().getSession().get(
com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
final Map params = ActionContext.getContext().getParameters();
final ActionContext context = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest)
context.get(HTTP_REQUEST);
if (params.containsKey(fieldName)) {
String kaptchaAnswer = (String) request.getParameter(fieldName);
if (kaptchaAnswer.equals(kaptchaExpected)) {
log.debug("found kaptcha answer");
return invocation.invoke();
}
attemptAddErrorToAction(invocation, "captcha.invalid");
return failResult; //maybe just input
} else {
log.debug("Kaptcha form submission did not contain a field named:" +
fieldName);
attemptAddErrorToAction(invocation, "captcha.missing");
return failResult; //maybe error
}
}
private void attemptAddErrorToAction(ActionInvocation i, String msgKey) {
Object a = i.getAction();
if (a instanceof ValidationAware) {
ValidationAware v = (ValidationAware) a;
String message;
message = msgKey; // message becomes the key by default
if (a instanceof TextProvider) {
message = ((TextProvider) a).getText(msgKey);
if (message.length() == 0 || message.equals(msgKey)) {
log.debug("message: " + msgKey + " not found");
message = msgKey; // just send the plain key to the interface
}
}
v.addActionError(message);
}
}
Also, here is what the usage looks like on a form.
<label for="kaptcha">Type the code:</label> <input type="text"
name="kaptcha" value="" size="10" maxlength="6"/><br/>
<div class="tuckedIn" >
${base}/kaptcha <br/>
#ImageFieldSet <@s.text name="label.captcha.refresh"/>
<script type="text/javascript">
$(function(){
$('#refreshLink').click(function () { $('#captcha').attr('src',
'${base}/kaptcha?' + Math.floor(Math.random()*100) ); })
});
</script>
</div>
(jquery used in that javascript above).
I also, use jquery's validation library and do ajax captcha validation in
addition to back end checking, but I'll leave those details out until you
have the basics working.
--
View this message in context: http://www.nabble.com/Captcha-in-Struts2-tp11819699p19767136.html
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org