应需求的变化,在登录cas的时候,默认根据用户名和密码进行验证,如果加上用户名,密码和一个系统标识进行验证呢?该如何做呢?

我们知道cas默认的登录界面中,输入的用户名和密码,再配置一下deployerConfigContext.xml 这个文件中的bean  org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler 的这个标签,写上对应的sql,以及在<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">中配置数据库驱动,数据库名称,以及登陆密码等。

如果再加上一个其他的验证该怎么做呢?

1  根据xml中bean标签的提示,很容器找到这个类QueryDatabaseAuthenticationHandler.java类,首先先修改login-webflow.xml,修改代码如下所示:

<binder>
<binding property="username" />
<binding property="password" />
<binding property="systemId" />
</binder>

其中<bingding property="systemId" />与界面中传递过来的隐含域一致。

2   casLoginView.jsp中增加的js代码如下所示,从登陆地址的url传递参数。

<script language="javascript"  type="text/javascript">
window.onload=function()//用window的onload事件,窗体加载完毕的时候
{
//do something
var result = location.search.match(new RegExp("[\?\&]" + 'systemId'+ "=([^\&]+)","i"));
if(result == null || result.length < 1){ result ="";
} $("#systemId")[0].value=result[1];
} </script>

参登陆页面地址为https://www.cdvcloud.com:8443/cas/login?systemId=vms2.0 ,在第一次登陆界面的时候会携带这两个参数https://www.cdvcloud.com:8443/cas/login?service=http%3A%2F%2F172.16.3.101%3A8080%2Fvms2.0%2Fuser%2FtoMain%2F 其中的一个为我们的自定义的系统标识,第二个为cas验证数据库成功后转到的主界面。

3   在登录界面中加上了hidden,以此来传递给CAS。

<input type="hidden" name="systemId" id="systemId">

4  修改CAS源代码,UsernamePasswordCredentials.java,代码如下所示。

/*
* Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license
* distributed with this file and available online at
* http://www.ja-sig.org/products/cas/overview/license/
*/
package org.jasig.cas.authentication.principal; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; /**
* UsernamePasswordCredentials respresents the username and password that a user
* may provide in order to prove the authenticity of who they say they are.
*
* @author Scott Battaglia
* @version $Revision: 1.2 $ $Date: 2007/01/22 20:35:26 $
* @since 3.0
* <p>
* This is a published and supported CAS Server 3 API.
* </p>
*/
public class UsernamePasswordCredentials implements Credentials { /** Unique ID for serialization. */
private static final long serialVersionUID = -8343864967200862794L; /** The username. */
@NotNull
@Size(min=1,message = "required.username")
private String username; /** The password. */
@NotNull
@Size(min=1, message = "required.password")
private String password; /** The systemId for vms2.0 for sql validate xx add 2014��7��21��16:12:51. */
@NotNull
@Size(min=1, message = "required.systemId")
private String systemId;
/*systemId begin*/ /**
* @return Returns the systemId.
*/ public String getSystemId() {
return systemId;
} public void setSystemId(String systemId) {
this.systemId = systemId;
} public String toStringSystemId() {
return "[systemId: " + this.systemId + "]";
} /*end */ /**
* @return Returns the password.
*/
public final String getPassword() {
return this.password;
} /**
* @param password The password to set.
*/
public final void setPassword(final String password) {
this.password = password;
} /**
* @return Returns the userName.
*/
public final String getUsername() {
return this.username;
} /**
* @param userName The userName to set.
*/
public final void setUsername(final String userName) {
this.username = userName;
} public String toString() {
return "[username: " + this.username + "]";
} @Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; UsernamePasswordCredentials that = (UsernamePasswordCredentials) o; if (password != null ? !password.equals(that.password) : that.password != null) return false;
if (username != null ? !username.equals(that.username) : that.username != null) return false; return true;
} @Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
result = 31 * result + (password != null ? password.hashCode() : 0);
return result;
}
}

除了cas自己的用户名和密码,添加自己的systemId标识。

5  修改QueryDatabaseAuthenticationHandler.java类 , 代码如下所示。

/*
* Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license
* distributed with this file and available online at
* http://www.ja-sig.org/products/cas/overview/license/
*/
package org.jasig.cas.adaptors.jdbc; import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.springframework.dao.IncorrectResultSizeDataAccessException; import javax.validation.constraints.NotNull; /**
* Class that if provided a query that returns a password (parameter of query
* must be username) will compare that password to a translated version of the
* password provided by the user. If they match, then authentication succeeds.
* Default password translator is plaintext translator.
*
* @author Scott Battaglia
* @author Dmitriy Kopylenko
* @version $Revision$ $Date$
* @since 3.0
*/
public final class QueryDatabaseAuthenticationHandler extends
AbstractJdbcUsernamePasswordAuthenticationHandler { @NotNull
private String sql; protected final boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials) throws AuthenticationException {
final String username = getPrincipalNameTransformer().transform(credentials.getUsername());
final String password = credentials.getPassword();
//xx add 2014 7 21 16:27:58 for vms2.0 systemid begin----------
//final String systemId = credentials.getSystemId();
String mySystemId = credentials.getSystemId();
String[] systemIdGroup=mySystemId.split(",");
String systemId= systemIdGroup[0];
System.out.println("systemId---------"+systemId+"----------------systemid value");
//xxadd 2014 7 21 16:27:58 for vms2.0 systemid end----------
final String encryptedPassword = this.getPasswordEncoder().encode(
password); try {
final String dbPassword = getJdbcTemplate().queryForObject(
this.sql, String.class, username,systemId);
return dbPassword.equals(encryptedPassword);
} catch (final IncorrectResultSizeDataAccessException e) {
// this means the username was not found.
return false;
}
} /**
* @param sql The sql to set.
*/
public void setSql(final String sql) {
this.sql = sql;
}
}

(四)SSO之CAS框架单点登录,自定义验证登录方式的更多相关文章

  1. (二)SSO之CAS框架单点退出,自定义退出界面.

    用CAS的退出,只能使用它自己的那个退出界面,如果有这样的要求, 要求退出后自动跳转到登录界面, 该如何做呢?下面这篇文章实现了退出后可以自定义跳转界面.  用了CAS,发现退出真是个麻烦事,退出后跳 ...

  2. (三)SSO之CAS框架单点退出,退出到CAS登录界面

    应需求的改变.CAS自定义登录页面不安全,不再使用,于是我一下子回到了原点,在linux上部署上了没有加自定义登陆界面的CAS,接下来开始修改CAS自己默认的登录界面为我们的界面. 一下子修改成功是根 ...

  3. (二)SSO之CAS框架单点退出,自己定义退出界面.

    用CAS的退出,仅仅能使用它自己的那个退出界面,假设有这种要求, 要求退出后自己主动跳转到登录界面, 该怎样做呢? 以下这篇文章实现了退出后能够自己定义跳转界面. 用了CAS,发现退出真是个麻烦事,退 ...

  4. (一)SSO之CAS框架通俗原理

    版权声明:本文为博主原创文章.转载请标明出处. https://blog.csdn.net/lovesummerforever/article/details/36068249 SSO统一验证     ...

  5. SSO 基于CAS实现单点登录 实例解析(二)

    本文目录: 概述 演示环境 部署CAS-Server相关的Tomcat 部署CAS-Client相关的Tomcat 测试验证SSO 第一: 本demo在一个机器上实现(三个虚拟主机),来看SSO单点登 ...

  6. (六)SSO之CAS框架扩展 改动CAS源代码实现与ESS动态password验证对接

    题记: 偶尔的偶尔我们会听到这个站点的数据泄露了,那个站点的用户数据泄露了.让用户又一次改动登录password,所以,对于用户数据安全性越发的引起我们的重视了,尤其是一些保密性要求高的站点.更须要添 ...

  7. Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册

    说明:这里没练习静态自动验证:如果用到静态验证首先自定义一个控制器,再在Model文件夹里创建一个NiHaoModel.php 类  NiHao是自定义的,前缀可以随意,但是一定要用驼峰法(首字母大写 ...

  8. c# 自定义验证登录(Authorize)

    我们的项目本来是用azure的auth认证,是用过程中发现登录速度太慢了,所以还是自己搞一个吧,没想到搞起来挺简单的,不是用一个专门的认证服务器哈,就是一个简单的工具类. 验证是否登录的类 /// & ...

  9. CAS实现单点登录SSO执行原理及部署

    一.不落俗套的开始 1.背景介绍 单点登录:Single Sign On,简称SSO,SSO使得在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. CAS框架:CAS(Centra ...

随机推荐

  1. 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集

    [抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...

  2. IPMI (Intelligent Platform Management Interface)

    4.3. ipmitool - utility for controlling IPMI-enabled devices 4.3.1. ipmitool 4.3.1.1. ubuntu 确定硬件是否支 ...

  3. VR眼镜和AR眼镜的区别

    VR眼镜是纯虚拟的世界建模,不结合现实世界.(VR一体机和手机VR眼镜是不同的,不只是是不是以手机为载体播放器的问题,而是它们结构上也有很大的区别:另外还有一点就是电脑端VR,这个主要是游戏:http ...

  4. 23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy

    23 DesignPatterns学习笔记:C++语言实现 --- 2.7 Proxy 2016-07-18 (www.cnblogs.com/icmzn) 模式理解

  5. Android-Java读写文件到自身APP目录

    界面: Layout: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...

  6. WebApi 插件式构建方案:IOC 容器初始化

    body { border: 1px solid #ddd; outline: 1300px solid #fff; margin: 16px auto; } body .markdown-body ...

  7. easyui - using

    using 是 easyloader.load 简写                  using('calendar', function()  { alert("加载calendar成功 ...

  8. oracle数据导出导入(exp/imp)

    1.本地数据库导入导出 1.导出 (运行---cmd中操作)exp 用户名/密码@数据库实例名file=本地存放路径eg: exp jnjp/jnjp@ORCL file=C:/jnjp.dmp 2. ...

  9. swagger 在本地正常调试 发布后出现500 : {"Message":"出现错误。"}

    点击项目属性 勾上xml 解决

  10. 经典的兔子生兔子问题(C#递归解法)

    古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 思路:先求出每个月新增的兔子,再用循环求和即可算出这个月 ...