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下.

cas添加验证码的更多相关文章

  1. CAS添加验证码功能

    1.  cas.war 下面的web-inf/web.xml  lib添加  kaptcha.jar kaptcha.jar通过maven获取 <dependency> <group ...

  2. cas4.2.4 登添加验证码

    看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 -->      &l ...

  3. cas增加验证码

    参考地址:https://blog.csdn.net/attackmind/article/details/52052502 参考地址:https://blog.csdn.net/jadyer/art ...

  4. asp.net添加验证码

    1.新建一个aspx页面生成验证码图像 using System; using System.Data; using System.Configuration; using System.Collec ...

  5. PHPCMS v9 自定义表单添加验证码验证

    1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...

  6. Angular企业级开发(9)-前后端分离之后添加验证码

    1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服 ...

  7. PHPCMS v9 自定义表单添加验证码

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

  8. [phpcms v9]自定义表单添加验证码验证功能

    修改  \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...

  9. 【转】PHPCMS v9 自定义表单添加验证码验证

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

随机推荐

  1. iOS 给tableView设置contentInset不生效?

    给tableView设置contentInset的时候如果tableView中内容比较多,超过一个屏幕,设置的contentInset是生效的,但是呢,如果页面内容比较少,我们会发现设置content ...

  2. 碉堡完整的高性能PHP应用服务器appserver

    完全企业级的开发模式,是一个多线程的 PHP 应用服务器,实现真正多线程的 PHP 编程,高效安全而且快速,以Magento 为例比基于 Nginx的标准安装要快 50%.概念上非常像 Java 的 ...

  3. java.lang.IllegalArgumentException: n must be positive

    public static String randomKey(){ Random random = new Random(); int key = random.nextInt(((int)Syste ...

  4. CI 点击图片刷新验证码

    <img src="<?php echo site_url('home/login/code'); ?>" onclick= this.src="< ...

  5. php socket编程入门

    服务端 <?php /** * File name server.php * 服务器端代码 * * @author guisu.huang * @since 2012-04-11 * */ // ...

  6. 第二百零九节,jQuery EasyUI,Pagination(分页)组件

    jQuery EasyUI,Pagination(分页)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Pagination(分页)组件的使 ...

  7. SQLServerException:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。

    一.问题描述: 1.连接数据库时抛出的异常: com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 ...

  8. Flea Circus(Project Euler 213)

    original version hackerrank programming version 题目大意是N*N的格子,每个格子一开始有1个跳蚤,每过单位时间跳蚤会等概率向四周跳,问M秒后空格子的期望 ...

  9. tree的使用

    //html <ul id="tree"></ul>  js function initTree() { $('#tree').tree({ url: '/ ...

  10. 2014年辛星解读css第一节

    CSS是Cascading Style Sheets的缩写.即层叠样式表,它用于表现HTML的样式,即HTML仅仅是去写该网页有哪些内容,至于怎样去表现它们,由CSS去定制. ************ ...