stucts 里面用 jcaptcha 做登录验证码

jcaptcha是专业的解决方案,做到了极致,没必要自己去写了

下载之:http://sourceforge.net/projects/jcaptcha/files/

根据他官方的例子,在struts中集成方法如下

环境:struts 1.3.x  jcaptcha 1.0

1. 加载lib

把jcaptcha-1.0-all.jar 和 jcaptcha-integration-struts-1.0.jar 放到工程的lib里面,并设置包含之

2. 配置struts-config.xml

加plug-in描述

<!-- WizRtf2Html Charset=0 --> <plug-in className =“com.octo.captcha.module.struts.CaptchaServicePlugin”/>

加引用的 action

<!-- WizRtf2Html Charset=0 --> <action-mappings >这个里面加以下代码
<!– add the render action–>
<action
path=“/jcaptcha”
type=“com.octo.captcha.module.struts.image.RenderImageCaptchaAction”
>
</action >

即:可用 http://server/proj/jcaptcha.do 调用(如果你设置的是 *.do)   http://server/proj/do/jcapcha (如果你设置的是 /do/*的话) 显示生成的图片

运行起来后,在浏览器中应该可以看到图片了

3. jsp中集成

如 login.jsp中

最前面加入taglib定义
<%– Add the jcaptcha taglib–%>
<%@ taglib uri=“http://jcaptcha.sourceforge.net” prefix=“jcaptcha” %>

增加显示和校验代码
<%– Add the jcaptcha form part–%>
<input type =“text” name =“jcaptcha_response” />

<!– 加入错误提示,改成用errors了–>
<font class =“required_error_font”>  <html:errors property=“jcaptcha_error_msg” /></font ><br />
<font size =“-1″ color =“#6f6f6f”> <jcaptcha:question /></font ><br />
<%– Add the image–%>
<img name =“jcaptcha” id =“jcaptcha” onclick =“refresh_jcaptcha(this)” src =<%= request.getContextPath()%>/do/jcaptchaalt =“click to refresh” style =“cursor:pointer;”/>

增加了个点击刷新的功能,脚本如下

<!-- WizRtf2Html Charset=0 --> <script language=“Javascript” >
function refresh_jcaptcha(obj){
//alert(obj);
obj.src=”<%= request.getContextPath()%> /do/jcaptcha?” + Math.random();
}

</script>

 

4. 在提交页面对应的Action.excute()中增加校验代码处理 request

我把他封装成了函数

<!-- WizRtf2Html Charset=0 --> public boolean isJcaptchaOK(HttpServletRequest request)
{
log .debug(“enter captcha challenge verification” );
CaptchaService service = CaptchaServicePlugin.getInstance ()
.getService();
String responseKey = CaptchaServicePlugin.getInstance ()
.getResponseKey();
String captchaID = CaptchaModuleConfigHelper.getId (request);
String challengeResponse = request.getParameter(responseKey);
if (log .isDebugEnabled())
log .debug(“response for id “ + captchaID + ” : “ + challengeResponse);
request.removeAttribute(responseKey);
boolean isResponseCorrect = false ;
if (challengeResponse != null )
try {
isResponseCorrect = service.validateResponseForID(captchaID,
challengeResponse).booleanValue();
} catch (CaptchaServiceException e) {
log .debug(“Error during challenge verification” , e);
request.setAttribute(CaptchaServicePlugin
.getInstance ().getMessageKey(),
CaptchaModuleConfigHelper
.getMessage (request));
log .debug(“forward to error with message : “ + CaptchaModuleConfigHelper.getMessage (request));
throw (e);//抛出程序应用异常
}
if (isResponseCorrect) {
log .debug(“correct : forward to success” );
return true;//正确
}
if (log .isDebugEnabled()) {
log .debug(“false : forward to failure with message : “
+ CaptchaModuleConfigHelper.getMessage (request));
log .debug(“in request attribute key : “
+ CaptchaServicePlugin.getInstance ().getMessageKey());
}
ActionErrors errors = new ActionErrors();//用ActionError替换了他自己的Message
errors.add(“jcaptcha_error_msg” , new ActionMessage(
“error.jcaptcha.error.inputInvalid” ));
this .addErrors(request, errors);
return false;
}

—–

excute(…)中:

<!-- WizRtf2Html Charset=0 --> if (!isJcaptchaOK(request))
    return mapping.findForward(“return_form” ); //返回页面
return mapping.findForward(“global_success” );
 
——
大功告成

你可能感兴趣的:(JavaScript,jsp,.net,struts,脚本)