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

目录
相关文章
|
缓存 安全 Java
JAVA实现用户登录错误N次后,账户暂时锁定
JAVA实现用户登录错误N次后,账户暂时锁定
530 0
JAVA实现用户登录错误N次后,账户暂时锁定
ThreadLocal实现登录(保存用户登录信息)
ThreadLocal可以将用户信息保存在线程中,当请求结束后我们在把保存的信息清除掉。这样我们才开发的时候就可以直接从全局的ThreadLocal中很方便的获取用户信息。 使用ThreadLocal,可以在同一线程中很方便的获取用户信息,不需要频繁的传递session对象。
2217 1
ThreadLocal实现登录(保存用户登录信息)
|
应用服务中间件 nginx
|
4月前
|
存储 前端开发 Java
①实现基于session的登录流程:发送验证码、登录注册、校验登陆状态
①实现基于session的登录流程:发送验证码、登录注册、校验登陆状态
64 0
|
7月前
|
数据安全/隐私保护
session登录优化和访问次数记录
session登录优化和访问次数记录
46 0
|
安全 Java 数据库
认证用户状态的判断|学习笔记
快速学习认证用户状态的判断
108 0
认证用户状态的判断|学习笔记
【JavaWeb】【Cookie】通过Cookie记录用户上次登陆时间并判断用户是否是首次登陆
【JavaWeb】【Cookie】通过Cookie记录用户上次登陆时间并判断用户是否是首次登陆
232 0

热门文章

最新文章