转自:
http://home.open-open.com/space-124-do-blog-id-791.html
Yale cas
是目前项目中用到最多的
SSO
整合方案,
cas
的原理在很多文章中都有所说明,这里就不再多进行叙述了,从实战出发,结合实际项目整合的经验是非常值得借鉴的,了解
cas
的同志们都知道,在
cas
提供的
demo
中,返回给客户端的用户信息很少,只有一个登录用户名,而在实际应用中,客户端需要知道更多的用户信息,比如用户的性别,年龄,爱好,地址,用户的分组,角色信息等等,那么,下面就给大家介绍
cas server3.4.2
如何给客户端返回更多的用户资料和信息。
一、
首先需要配置属性仓库
attributeRepository
,顾名思义,只用来存储相关的属性信息的,在领域分析模型中,就介绍了关于领域对象的资源库的概念,主要目的也是为了与客户程序解耦。首先,你需要到
WEB-INF
目录找到
deployerConfigContext.xml
文件,同时配置
attributeRepository
如下:
<bean
class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao" id="attributeRepository">
<constructor-arg index="0" ref="casDataSource"/>
<constructor-arg index="1" value="
select * from sec_user where {0}
"/>
<property name="queryAttributeMapping">
<map>
<entry key="username" value="user_iidd"/>
</map>
</property>
<property name="resultAttributeMapping">
<map>
<entry key="user_iidd" value="username"/>
<entry key="user_name" value="user_name"/>
<entry key="user_sex" value="user_sex"/>
</map>
</property>
</bean>
|
这里将进行说明一下,
SingleRowJdbcPersonAttributeDao
的构造函数需要提供两个参数,数据源
DataSource
和查询用户信息的
sql
。
public SingleRowJdbcPersonAttributeDao(DataSource ds, String sql)
{
super(ds, sql);
}
|
构造函数配置完成后,需要配置
queryAttributeMapping
和
resultAttributeMapping
,查询官方网站说明如下:
Property
|
Type
|
Default Value
|
|
queryAttributeMapping
|
Map<String, ?>
|
null
|
A Map
from attribute names used in the Map
to attribute names to use in the SQL. The values can be either String
or Collection<String> to use a single Map attribute under
multiple names as in the SQL. If set only Map attributes listed will be
used in the SQL. If not set all Map attributes are used as-is in the
SQL.
|
resultAttributeMapping
|
Map<String, ?>
|
null
|
A Map
from SQL result names to returned attribute names. The values can be
either String or Collection<String> to use a single SQL result
under multiple returned attributes. If set only SQL attributes listed
will be returned. If not set all SQL attributes will be returned.
|
其中
queryAttributeMapping
是组装
sql
用的查询条件属性,如下表中
结合
封装成查询
sql
就是
select * from sec_user where user_iidd=#username#
,
resultAttributeMapping
是
sql
执行完毕后返回的结构属性,
key
对应数据库字段,
value
对应客户端获取参数。
二、
配置用户认证凭据转化的解析器,也是在
deployerConfigContext.xml
中,找到
credentialsToPrincipalResolvers
,为
UsernamePasswordCredentialsToPrincipalResolver
注入
attributeRepository
,那么
attributeRepository
就会被触发并通过此类进行解析,红色为新添部分。
<property name="credentialsToPrincipalResolvers">
<list>
<bean
class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">
<property name="attributeRepository" ref="attributeRepository"/>
</bean>
<bean
class="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver"/>
</list>
</property>
|
三、
修改
WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jsp
,在
server
验证成功后,这个页面负责生成与客户端交互的
xml
信息,信息格式如下:
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:authenticationSuccess>
<cas:user>admin</cas:user>
<cas:attributes>
<cas:attribute>
<cas:name>userId</cas:name>
<cas:value>admin</cas:value>
</cas:attribute>
<cas:attribute>
<cas:name>userSex</cas:name>
<cas:value>M</cas:value>
</cas:attribute>
<cas:attribute>
<cas:name>userName</cas:name>
<cas:value>
系统管理员
</cas:value>
</cas:attribute>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
|
在默认的
casServiceValidationSuccess.jsp
中,只包括用户名
,并不提供其他的属性信息,因此需要对页面进行扩展,如下,红色为新添加部分:
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
<cas:authenticationSuccess>
<cas:user>${fn:escapeXml(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.id)}</cas:user>
<c:if
test="${fn:length(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes)
> 0}">
<cas:attributes>
<c:forEach
var="attr"
items="${assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes}">
<cas:attribute>
<cas:name>${fn:escapeXml(attr.key)}</cas:name>
<cas:value>${fn:escapeXml(attr.value)}</cas:value>
</cas:attribute>
</c:forEach>
</cas:attributes>
</c:if>
<c:if test="${not empty pgtIou}">
<cas:proxyGrantingTicket>${pgtIou}</cas:proxyGrantingTicket>
</c:if>
<c:if test="${fn:length(assertion.chainedAuthentications) > 1}">
<cas:proxies>
<c:forEach
var="proxy" items="${assertion.chainedAuthentications}"
varStatus="loopStatus" begin="0"
end="${fn:length(assertion.chainedAuthentications)-2}" step="1">
<cas:proxy>${fn:escapeXml(proxy.principal.id)}</cas:proxy>
</c:forEach>
</cas:proxies>
</c:if>
</cas:authenticationSuccess>
</cas:serviceResponse>
|
通过完成上面三个步骤的配置后,
server
端的工作就完成了,那么如何在客户端获取这些信息呢?下面进行说明:
获取远程用户
request.getRemoteUser()
获取其他信息
AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal(); Map attributes = principal.getAttributes();
|
备注:总体来说,
cas
的代码结构不是很完善,尤其是客户端,从
2.x
到
3.x
,代码结构基本是进行了翻天覆地的重构,另外获取客户端信息时,也是只有最新版本的
3.1.1
才支持,之前的版本是不支持的,请一定要注意。
分享到:
相关推荐
Yale CAS SSO DotNet Client
Yale CAS SSO JAVA Client
Yale CAS实现单点登陆的客户端源码和服务端源码,客户端cas-client-3.1.10代码和cas-server-3.4.2.1代码
Yale CAS 3.3实现SSO详细教程。
NULL 博文链接:https://kennylee26.iteye.com/blog/855392
基于Yale_CAS的单点登录的设计与实现,大学发表论文
一、Yale CAS简介 2 二、CAS 原理和协议 2 三、准备工作 4 四、部署CAS Server 5 步骤1.配置CAS Server应用服务器的Https 协议 5 ●生成服务器证书 5 ●配置Tomcat的https 8 步骤2.部署CAS Server 8 ●扩展认证接口 ...
有关耶鲁大学CAS服务器的配置 1.环境准备 2.在jdk上配置SSL 3.SSL验证证书 4.Tomcat配置 5.Tomcat jmx bug
在Tomcat中使用Yale CAS实现单点登陆,收集的资源,拿出来共享
解决普元EOS报错:edu.yale.its.tp.cas.client.IContextInit 下载后需jar到lib里面且单击右键在属性一栏的弹出框内添加该jar包即可解决爆粗
简单而精练的介绍了yalu-cas的原理及其实现
软件介绍: ...下载后先解压,然后将sso-client-java-7.0.8.jar文件复制到lib里面,鼠标单击右键在属性一栏的弹出框内添加该jar包即可解决解决普元EOS报错问题:edu.yale.its.tp.cas.client.IContextInit
CAS学习笔记 --CAS的服务器配置和各种客户端的配置
NULL 博文链接:https://sgq0085.iteye.com/blog/2003190
该资源不仅提供部署在Tomcat服务器上的cas.war包(解压缩后在webapps目录下),而且创建Mysql数据源,完成用户表中用户信息的认证。 deployerConfigContext.xml中创建MySql数据源如下: <value>...
Weblogic使用YALE(耶鲁)CAS实现SSO单点登录 的方法.doc
Yale人脸数据库中一个采集志愿者的10张样本,相比较ORL人脸数据库Yale库中每个对象采集的样本包含更明显的光照、表情和姿态以及遮挡变化。4. YALE人脸数据Bhttps://computervisiononline.com/dataset/1105138686包含...
cas源码,可直接使用 单点登录(Single Sign On),简称为 SSO,是目前...CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目。
CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS 具有以下特点: 【1】开源的企业级单点登录解决方案。 【2】CAS ...