国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。 
------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下文章大部分参考  http://zxs19861202.iteye.com/blog/890965 的内容,按照文章中制作后,补充了部分细节,形成了此片文章。

文章中 CAS 基础环境:

cas-server-3.5.2    

cas-client-3.2.1

----------------------------------------------------------------------------------------------------------------------------------------

服务器端配置

----------------------------------------------------------------------------------------------------------------------------------------

程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况。

cas 各种版本配置方式也不尽相同,这里讲的是目前最新版本3.4.4(同样适合 3.5.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 userinfo where {0}"/>

<property name="queryAttributeMapping">

<map>

<entry key="username" value="loginname"/>  // 这里的key需写username,value对应数据库用户名字段

</map>

</property>

<property name="resultAttributeMapping">

<map>

<entry key="id" value="id"/>

<entry key="mobile" value="mobile"/>

<entry key="email" value="email"/>

</map>

</property>

</bean>

其中:

queryAttributeMapping 是组装sql用的查询条件属性,上述配置后,结合封装成查询sql就是 select * from userinfo where loginname=#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>

 三、(在 CAS Server 3.5.2 中测试通过)修改 deployerConfigContext.xml 中的 org.jasig.cas.services.InMemoryServiceRegistryDaoImpl 的 属性 registeredServices

修改 registeredServices  列表中的每个协议中的 allowedAttributes 属性的值,列出的每个值,在客户端就可以访问了

<bean  id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
            <property name="registeredServices">
                <list>
                    <bean class="org.jasig.cas.services.RegexRegisteredService">
                        <property name="id" value="0" />
                        <property name="name" value="HTTP and IMAP" />
                        <property name="description" value="Allows HTTP(S) and IMAP(S) protocols" />
                        <property name="serviceId" value="^(https?|imaps?)://.*" />
                        <property name="evaluationOrder" value="10000001" />
                        <property name="allowedAttributes"> // 客户端需要使用的对象的属性名称
                                <list>
                                        <value>uid</value>
                                        <value>email</value>
                                        <value>mobile</value>
                                        <value>phone</value>
                                        <value>sex</value>
                                        <value>identify_type</value>
                                        <value>identify_id</value>
                                        <value>birth</value>
                                        <value>fax</value>
                                        <value>isMarry</value>
                                        <value>userno</value>
                                        <value>login_account</value>
                                </list>
                        </property>
                    </bean>

此步骤灰常重要,可以看看 org.jasig.cas.services.RegexRegisteredService 的源码,其中的 allowedAttributes 是关键

【提示】网上说 ignoreAttributes 属性控制,查看了 CAS 3.5.2 版本的 AbstractRegisteredService 源码后,发现其默认值就是 false,即:添加属性后,客户端就可见了

四、(按照上述配置后,万里长征就差最后一步了)修改 WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jsp

在server验证成功后,这个页面负责生成与客户端交互的xml信息,在默认的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:${fn:escapeXml(attr.key)}>${fn:escapeXml(attr.value)}</cas:${fn:escapeXml(attr.key)}>

</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端的工作就完成了,那么如何在客户端获取这些信息呢?下面进行说明:

----------------------------------------------------------------------------------------------------------------------------------------

客户端获取用户信息

----------------------------------------------------------------------------------------------------------------------------------------

 java客户端获取用户信息:

AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

Map attributes = principal.getAttributes();

String email=attributes .get("email");

php客户端;

$email=phpCAS::getAttribute('email');

参考文档: http://zxs19861202.iteye.com/blog/890965

cas 3.5.2 登录成功后,如何返回用户更多信息?的更多相关文章

  1. [原]基于CAS实现单点登录(SSO):登录成功后,cas client如何返回更多用户信息

    从cas server登录成功后,默认只能从casclient得到用户名.但程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况. cas client拿到用户名后再到数据库中查 ...

  2. 【转】【可用】Android 登录判断器,登录成功后帮你准确跳转到目标activity

    我们在使用应用时肯定遇到过这样的情景,打开应用,并不是需要我们登录,你可以浏览应用中的大部分页面,但是当你想看某个详情页的时候,点击后突然跳转到了登录页面,好,我们输入账号密码,点击登录,登录成功,跳 ...

  3. IdentityServer4 登录成功后,跳转到原来页面

    IdentityServer4 登录成功后,默认会跳转到Config.Client配置的RedirectUris地址http://localhost:5003/callback.html,用于获取 T ...

  4. 使用Shiro登录成功后,跳转到之前访问的页面实现

    转:http://blog.csdn.net/lhacker/article/details/20450855 很多时候,我们需要做到,当用户登录成功后,跳转回登录前的页面.如果用户是点击" ...

  5. taotao用户登录(及登录成功后的回调url处理)

    后台Controller: package com.taotao.sso.controller; import org.springframework.stereotype.Controller; i ...

  6. 在某网站的登录页面登录时如果选择“记住用户名”,登录成功后会跳转到一个中间层(页面代码将登录的用户名和密码存在cookie),中间页面中存在一个超链接,单击超链接可以链接到第三个页面查看信息。若选择“

    Response实现登录并记录用户名和密码信息 在某网站的登录页面登录时如果选择"记住用户名",登录成功后会跳转到一个中间层(页面代码将登录的用户名和密码存在cookie),中间页 ...

  7. JavaScript 正则表达式匹配成功后的返回结果

    原文地址:https://blog.csdn.net/liupeifeng3514/article/details/79005604 使用正则表达式EDIT 正则表达式可以被用于RegExp的exec ...

  8. httpclient 怎么带上登录成功后返回的cookie值访问下一页面

    我是只很菜很菜的小鸟.刚上班,有这个一个需求.要我抓取别的网站的数据.     我根据用户密码登录一个网站成功后,生成一个cookie值.我已经获取到了.然后要带上这个cookie值进行下一页面的访问 ...

  9. [saiku] 系统登录成功后查询Cubes

    一.系统启动时初始化ds和conn 1.查询出目前系统拥有的Datasources和Connections放入内存中 2.比对saiku-datasources中的ds是否有新增的,如果有,创建新的d ...

随机推荐

  1. Return Negative

    Return Negative In this simple assignment you are given a number and have to make it negative. But m ...

  2. Eclipse插件安装

    在线安装(一定要保证网络畅通) 更新插件: Eclipse中,Help->Install New Software...从Work with下拉列表框中选择,通过该列表框可以选择Eclipse已 ...

  3. vbox android x86 分辨率

    D:\Program Files\Oracle\VirtualBox>VBoxManage setextradata "android" "CustomVideoM ...

  4. 基于VPN搭建混合云架构需要考虑的网络因素

     Joy Qiao from MSFT Wed, Jan 21 2015 8:44 AM 很多用户在搭建混合云架构时,会使用到微软Azure虚拟网络中的 VPN功能,来实现Azure中的虚拟网络与用户 ...

  5. 【转】iOS中16进制转10进制

    原文网址:http://www.voidcn.com/blog/u012198553/article/p-4976772.html /// 将十六进制的字符串转化为NSData - (NSData ) ...

  6. Spring基础知识及bean的配置

    IOC与DI: IOC(inversion of control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源.作为回应,容器适时的返回资源.而应用了IOC之后,则是 ...

  7. android.content.res.Resources$NotFoundException: String resource ID #0x1

    之前忘了记录这个错误,今天又遇到了.唉,人不能纵容自己犯懒,遂记录之. 错误:android.content.res.Resources$NotFoundException: String resou ...

  8. [转] This function or variable may be unsafe

    原文:This function or variable may be unsafe,他大姨妈 错误提示: [Error]'fopen' This function or variable may b ...

  9. FZU1686 神龙的难题 dancing links 重复覆盖

    分析:每次可以打一个小矩阵的怪,然后把每个怪看成一列,然后每个小矩阵看成一行,枚举左上角就行 注:然后注意总共的节点数是新图的行*列的个数,不是原图 #include<cstdio> #i ...

  10. 软件测试模型汇总-V模型,W模型,X模型,H模型

    V模型 在软件测试方面,V模型是最广为人知的模型,尽管很多富有实际经验的测试人员还是不太熟悉V模型,或者其它的模型.V模型已存在了很长时间,和瀑布开发模型有着一些共同的特性,由此也和瀑布模型一样地受到 ...