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

参考地址:https://blog.csdn.net/jadyer/article/details/46916169

增加UsernamePasswordCaptchaCredential类继承UsernamePasswordCredential。

import org.jasig.cas.authentication.UsernamePasswordCredential;

/**
* 自定义的接收登录验证码的实体类
*/ public class UsernamePasswordCaptchaCredential extends UsernamePasswordCredential{ private static final long serialVersionUID = 7042484120233254159L; private String captcha; public String getCaptcha() {
return captcha;
} public void setCaptcha(String captcha) {
this.captcha = captcha;
}
}

  增加AuthenticationViaCaptchaFormAction类继承AuthenticationViaFormAction

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.jasig.cas.authentication.Credential;
import org.jasig.cas.web.flow.AuthenticationViaFormAction;
import org.jasig.cas.web.support.WebUtils;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.util.StringUtils;
import org.springframework.webflow.execution.RequestContext; /**
* 用户名密码非空验证,验证码效验Action
*/ public class AuthenticationViaCaptchaFormAction extends AuthenticationViaFormAction { public final String validateCaptcha(final RequestContext context, final Credential credential, final MessageContext messageContext){
final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
HttpSession session = request.getSession();
String rand = (String)session.getAttribute("rand");
session.removeAttribute("rand"); UsernamePasswordCaptchaCredential upc = (UsernamePasswordCaptchaCredential)credential;
String captcha = upc.getCaptcha(); System.out.println("获取Session验证码-->" + rand);
System.out.println("获取表单输入验证码-->" + captcha); if(!StringUtils.hasText(rand) || !StringUtils.hasText(captcha)){
messageContext.addMessage(new MessageBuilder().error().code("required.captcha").build());
return "error";
}
if(captcha.equals(rand)){
return "success";
}
//这段网上这么写的messageContext.addMessage(new MessageBuilder().code("required.captcha").build());
//实际上这么写是org.springframework.binding.message.INFO级别的,这会导致前台表单无法显示这里的错误信息
messageContext.addMessage(new MessageBuilder().error().code("error.authentication.captcha.bad").build());
return "error";
}
}

  修改login-webflow.xml文件

第27行修改原来的验证类
<!-- 新加的用于接收前台表单验证码字段captcha的JavaBean -->
<var name="credential" class="com.cas.UsernamePasswordCaptchaCredential"/>
修改88至102行内
<view-state id="viewLoginForm" view="casLoginView" model="credential">
<binder>
<binding property="username" required="true"/>
<binding property="password" required="true"/>
<!-- 前台添加表单添加验证码字段captcha -->
<binding property="captcha" required="true"/>
</binder>
<on-entry>
<set name="viewScope.commandName" value="'credential'"/> <!--
<evaluate expression="samlMetadataUIParserAction" />
-->
</on-entry>
<transition on="submit" bind="true" validate="true" to="authcodeValidate"/>
</view-state>
<!-- AuthenticationViaCaptchaFormAction类中重写validateCaptcha方法 -->
<action-state id="authcodeValidate">
<evaluate expression="authenticationViaFormAction.validateCaptcha(flowRequestContext, flowScope.credential, messageContext)" />
<transition on="error" to="generateLoginTicket" />
<transition on="success" to="realSubmit" />
</action-state>

  修改cas-server.xml文件

修改第305行的class
<bean id="authenticationViaFormAction" class="com.cas.AuthenticationViaCaptchaFormAction"
p:centralAuthenticationService-ref="centralAuthenticationService"
p:warnCookieGenerator-ref="warnCookieGenerator"/>

  

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

  1. cas sso单点登录系列5_cas单点登录增加验证码功能完整步骤

    转:http://blog.csdn.net/ae6623/article/details/8919718 本篇教程cas-server端下载地址:解压后,直接放到tomcat的webapp目录下就能 ...

  2. SSO单点登录系列5:cas单点登录增加验证码功能完整步骤

    本篇教程cas-server端下载地址:解压后,直接放到tomcat的webapp目录下就能用了,不过你需要登录的话,要修改数据源,C:\tomcat7\webapps\casServer\WEB-I ...

  3. CAS添加验证码功能

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

  4. TODO:Laravel增加验证码

    TODO:Laravel增加验证码1. 先聊聊验证码是什么,有什么作用?验证码(CAPTCHA)是"Completely Automated Public Turing test to te ...

  5. web系统登陆页面增加验证码

    传统登陆页面中包含两个输入项: • 用户名 • 密码有时为了防止机器人进行自动登陆操作,或者防止恶意用户进行用户信息扫描,需增加动态验证码功能.此时,登陆页面中包含了三个输入项: • 用户名 • 密码 ...

  6. yii2增加验证码详细步骤

    作者:白狼 出处:http://www.manks.top/article/yii2_captcha本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  7. cas添加验证码

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

  8. exchange邮箱系统增加验证码机制

    首先背景是exchange的邮箱系统没有后台源代码.因为这个原因,生成验证码的机制放在aspx的runat="sever"后台代码里面. 首先需要找到iis中logon.aspx文 ...

  9. CAS增加免登陆(Remember Me)功能

    1. 打开deployerConfigContext.xml 在 authenticationManager 的bean中增加 <property name="authenticati ...

随机推荐

  1. 为什么不建议将 font-size 设置为 12px 以下?如果一定要设置为 12px 以下要怎么做?

    问题:为什么不建议将 font-size 设置为 12px 以下?如果一定要设置为 12px 以下要怎么做? 先看看把 font-size 设置为 12px 以下时的效果:(浏览器为 Chrome 5 ...

  2. Python3 条件控制(九)

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: if 语句 Python中if语句的一般形式如下所示: i ...

  3. BusyBox ifup udhcpc后台运行

    /********************************************************************** * BusyBox ifup udhcpc后台运行 * ...

  4. nginx 只容许域名访问禁止掉 ip 访问

    在原有 nginx server 的基础上再加上相同端口的配置 server {            listen 80 default_server;            server_name ...

  5. cocos2dx 分辨率适配问题

    分辨率适配问题好文章: http://www.cocoachina.com/bbs/read.php?tid=201987&page=1&toread=1#tpc http://www ...

  6. A-B Problem nyoj

    A-B Problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧. 现在有两个实数A和 ...

  7. FastAdmin 插件配置文件 info.ini 中的 state 什么意思?

    FastAdmin 插件配置文件 info.ini 中的 state 什么意思? 在插件配置中有一个 state ,这是配置插件开关的.

  8. ffmpeg 从内存中读取数据(或将数据输出到内存)(转)

    更新记录(2014.7.24): 1.为了使本文更通俗易懂,更新了部分内容,将例子改为从内存中打开. 2.增加了将数据输出到内存的方法. 从内存中读取数据 ffmpeg一般情况下支持打开一个本地文件, ...

  9. UOJ 55 【WC2014】紫荆花之恋——点分治+平衡树

    题目:http://uoj.ac/problem/55 点分治.在点分树上每个点上用 splay 维护管辖的点的情况.做几次就重构点分树.TLE.只能过 20 分. #include<cstdi ...

  10. 显示等待WebDriverWait

    显示等待:WebDriverWait 等待页面加载完成,找到某个条件发生后再继续执行后续代码,如果超过设置时间检测不到则抛出异常 WebDriverWait(driver, timeout, poll ...