cas添加验证码,折腾了好久,终于整理好了,很大部分都是借鉴http://binghejinjun.iteye.com/blog/1255293这个的。但是他的有一个很不好的地方就是不能提升验证码错误!

红色字体的为我添加的,可以提示验证码错误!很简单,感谢。原作者。谢谢。

1.   首先,我用的cas版本是3.4.6,验证码采用的是CAPTCHA,所需jar包可以google搜索,部署好cas后.在web-info目录下找到login-webflow.xml,打开,找到如下代码:

<view-state id="viewLoginForm" view="casLoginView" model="credentials">
<var name="credentials" class="org.jasig.cas.authentication.principal.UsernamePasswordCredentials" />
<binder>
<binding property="username" />
<binding property="password" />
</binder>
<on-entry>
<set name="viewScope.commandName" value="'credentials'" />
</on-entry>
<transition on="submit" bind="true" validate="true" to="realSubmit">
<set name="flowScope.credentials" value="credentials" />
<evaluate expression="authenticationViaFormAction.doBind(flowRequestContext, flowScope.credentials)" />
</transition>
</view-state>

  此段代码的功能是绑定cas登录过程中的用户名和密码,再次我们修改如下:

<view-state id="viewLoginForm" view="casLoginView" model="credentials">
<var name="credentials" class="org.jasig.cas.authentication.principal.UsernamePasswordCredentials" />
<binder>
<binding property="username" />
<binding property="password" />
</binder>
<on-entry>
<set name="viewScope.commandName" value="'credentials'" />
</on-entry>
<transition on="submit" bind="true" validate="true" to="yzmSubmit">
<set name="flowScope.credentials" value="credentials" />
<evaluate expression="authenticationViaFormAction.doBind(flowRequestContext, flowScope.credentials)" />
</transition>
</view-state>

  也就是说,只需要修改realSubmit为yzmSubmit即可.然后加入如下配置:

  <!--fan add start-->
<action-state id="yzmSubmit">
<evaluate expression="yzmViaFormAction.submit(flowRequestContext,messageContext)" />
<transition on="success" to="realSubmit" />
<transition on="error" to="viewLoginForm" />
</action-state>
<!--fan add end-->

此段配置是自定义的验证码验证器,用来验证你提交的验证码的正确性. 
  2.在web-info下找到cas-servlet.xml,打开后,加入

<!--fan add start-->
<bean id="yzmViaFormAction" class="com.ivan.zhang.servlet.YzmAction"
/>

  <!--fan add end-->此配置是注册自定义的验证码 
  3.编写如下类:

package com.ivan.zhang.servlet; 

import com.ivan.zhang.CaptchaServiceSingleton;
import com.octo.captcha.service.image.ImageCaptchaService;
import java.io.PrintStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.jasig.cas.web.support.WebUtils;
import org.springframework.webflow.core.collection.ParameterMap;
import org.springframework.webflow.execution.RequestContext; public class YzmAction
{
public final String submit(RequestContext context)
throws Exception
{
Boolean flag = Boolean.valueOf(false);
System.out.println("YzmAction is submiting....................");
String yzm = context.getRequestParameters().get("yzm");
String captchaId = WebUtils.getHttpServletRequest(context).getSession().getId();
flag = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,
yzm);
if (flag.booleanValue()) {
return "success";
}
//我添加的改动。提示验证码错误

MessageBuilder msgBuilder = new MessageBuilder();
msgBuilder.defaultText("验证码错误!");
messageContext.addMessage(msgBuilder.error().build());

    return "error";
}
}

  其中,flag = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, 
      yzm); 
此句话是为了验证提交的验证码和先前生成的验证码的正确性,以此作为验证结果跳转的依据.CaptchaServiceSingleton此类是自定义类,稍后会附加完整的类供下载调试. 
4.打开web-info/view/jsp/default/ui/casLoginView.jsp,在密码下面加入

<%--fan add start --%>
<img alt="yzm" src="captcha.jpg">
<spring:message code="screen.welcome.label.yzm.accesskey" var="yzmAccessKey" />
<form:input cssClass="required" cssErrorClass="error" id="yzm" size="25" tabindex="1" accesskey="${yzmAccessKey}" path="yzm" autocomplete="false" htmlEscape="true" />
<%--fan add end --%>

  5. 最后一步则是注册验证码生成器,打开web.xml文件,加入

<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>com.ivan.zhang.servlet.ImageCaptchaServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jcaptcha</servlet-name>
<url-pattern>/captcha.jpg</url-pattern>
</servlet-mapping>

  ok,就这么简单.简单解释一下流程,web.xml中注册的类是用来调用自定义的验证码生成器,以便在显示登陆界面的时候绘制验证码图片,并在session中生成标志位并记录,当用户提交验证码和用户名密码时,会先走自定义的验证码验证器(此时会先验证验证码的正确性),如果正确,再走用户名和密码的验证,如果不正确,则直接跳转回登陆页面.yzm.jar是自定义的验证码生成器和验证类,直接打包好后放到web-info/lib下.

最新文章

  1. mysql事务和并发控制
  2. .htaccess更改目录下的默认主页
  3. [XAF]如何在非按钮事件中打开视图
  4. [小技巧][ASP.Net MVC Hack] 使用 HTTP 报文中的 Header 字段进行身份验证
  5. django manage.py 的各种功能
  6. [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库
  7. ASP.NET -- WebForm -- ScriptManager 类
  8. Ubuntu gitlab安装文档及邮件通知提醒配置
  9. C# ConfigurationManager不存在问题解决
  10. VS2013/VS2015/VS2017通过oschina托管代码
  11. 线程误区-join,wait(里边还是调用的wait)
  12. Ubuntu安装时没注册root用户密码,怎么登录root
  13. SQL Server 损坏修复 之一 常见错误解读
  14. JVM学习网址(收集总结)
  15. Python对list去重
  16. MySQL 找回密码
  17. Servlet------&gt;jsp自定义标签1(简单入门)
  18. Flask蓝图目录、Flask-SQLAlchemy、Flask-Script、Flask-Migrate
  19. socketserver 源码剖析:
  20. python2.7练习小例子(二十二)

热门文章

  1. Generating SSH Keys on windows
  2. Java字符串中文检测转换
  3. cocos2dx 富文本框,支持换行,支持神情(支持汉字截断无乱码)
  4. Zookeeper中的选举机制
  5. jQuery插件学习笔记
  6. PAT001 一元多项式求导
  7. 第二百二十三节,jQuery EasyUI,ComboBox(下拉列表框)组件
  8. .NET开发笔记--对config文件的操作(3)
  9. 【vijos】1881 闪烁的繁星(线段树+特殊的技巧)
  10. hdu 5078