cas 3.5.2 登录成功后,如何返回用户更多信息?

简介: 以下文章大部分参考  http://zxs19861202.iteye.com/blog/890965 的内容,按照文章中制作后,补充了部分细节,形成了此片文章。文章中 CAS 基础环境: cas-server-3.


以下文章大部分参考  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

目录
相关文章
[✔️]AudioRelay将电脑的声音投射在手机上
[✔️]AudioRelay将电脑的声音投射在手机上
3289 0
|
机器学习/深度学习 人工智能 测试技术
VisionTS:基于时间序列的图形构建高性能时间序列预测模型,利用图像信息进行时间序列预测
构建预训练时间序列模型的主要挑战在于获取高质量、多样化的时间序列数据。目前有两种方法:迁移学习LLM(如GPT-4或Llama)和从零训练。尽管迁移学习可行,但效果有限;从零训练则依赖大量数据,如MOIRAI、TimesFM和TTM等模型所示。为解决这一难题,研究人员提出利用图像数据进行时间序列预测。
957 11
VisionTS:基于时间序列的图形构建高性能时间序列预测模型,利用图像信息进行时间序列预测
|
架构师 Java 开发者
得物面试:Springboot自动装配机制是什么?如何控制一个bean 是否加载,使用什么注解?
在40岁老架构师尼恩的读者交流群中,近期多位读者成功获得了知名互联网企业的面试机会,如得物、阿里、滴滴等。然而,面对“Spring Boot自动装配机制”等核心面试题,部分读者因准备不足而未能顺利通过。为此,尼恩团队将系统化梳理和总结这一主题,帮助大家全面提升技术水平,让面试官“爱到不能自已”。
得物面试:Springboot自动装配机制是什么?如何控制一个bean 是否加载,使用什么注解?
|
机器学习/深度学习 存储 自然语言处理
基于知识库快速搭建智能客服问答 Bot
在数字化转型的大潮中,智能客服系统成为提升企业客户体验与运营效率的关键工具。Botnow平台集成智能体创作与分发功能,提供一站式智能客服问答Bot搭建服务。本文详细介绍了如何利用Botnow的知识库功能及RAG(Retrieve-Augmented Generation)方案快速构建智能客服问答Bot。通过Botnow平台,用户可以轻松创建知识库、配置智能体,并关联知识库以实现智能回答。该方案广泛适用于对话沟通、行业知识库建设、企业内部信息检索及内容创作等多个场景。Botnow平台以其可视化编排、低技术门槛等特点,助力企业轻松实现智能客服系统的搭建与优化,成为数字化转型的重要推手。
1255 1
|
关系型数据库 MySQL Linux
mysql 主从同步 实现增量备份
【8月更文挑战第28天】mysql 主从同步 实现增量备份
274 3
|
网络协议 Linux 网络安全
Iptables深度解析:四表五链与动作参数
Iptables深度解析:四表五链与动作参数
1600 0
|
存储 数据库 C++
"深入剖析Python元组(tuple):与列表的对比、特性解析及高效应用场景展示"
【8月更文挑战第9天】Python元组与列表虽均用于存储元素集合,但有本质差异。元组不可变,创建后无法修改,适合保护数据不被意外更改的场景,如作字典键或传递固定值。列表则可变,支持动态增删改,适用于需频繁调整的数据集。元组因不可变性而在性能上有优势,可用于快速查找。两者各有千秋,根据具体需求选择使用。例如,元组可用于表示坐标点或日期,而列表更适合管理用户列表或库存。
914 1
|
资源调度 分布式计算 Hadoop
Hadoop Yarn 核心调优参数
这是一个关于测试集群环境的配置说明,包括3台服务器(master, slave1, slave2)运行CentOS 7.5,每台有4核CPU和4GB内存。集群使用Hadoop 3.1.3,JDK1.8。Yarn核心配置涉及调度器选择、ResourceManager线程数、节点检测、逻辑处理器使用、核心转换乘数、NodeManager内存和CPU设置,以及容器的内存和CPU限制。配置完成后,需要重启Hadoop并检查yarn配置。
491 4
|
存储 安全 数据可视化
ONLYOFFICE 8.0版本深度测评:革新之作还是失望之作?
ONLYOFFICE 8.0版本深度测评:革新之作还是失望之作?
814 0
|
前端开发 Java
SpringBoot 集成cas5.3 实现客户端接入cas认证中心
前面我们讲解了很多关于cas认证中心的内容,今天我们说一说如何接入客户端
1966 0