CAS服务端返回更多的用户登录信息

cas server登录成功后,默认只能从cas server得到用户名。但程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况。cas client拿到用户名后再到数据库中查询,的确可以得到关于该用户的更多信息。但是如果用户登录成功后,直接从cas server返回给cas client用户的详细信息,这也是一个不错的做法。这个好处,尤其是在分布式中得以彰显,cas server可以把用户信息传递给各个应用系统,如果是上面那种做法,那么各个系统得到用户名后,都得去数据库中查询一遍,无疑是一件重复性工作。

1、首先需要配置属性attributeRepository

WEB-INF目录找到 deployerConfigContext.xml文件,同时配置 attributeRepository如下:

<bean class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"id="attributeRepository"> 

    <constructor-argindex="0"ref="dataSource"/> 

    <constructor-argindex="1"value="select * from t_user where {0}"/> 

    <propertyname="queryAttributeMapping"> 

       <map> 

           <!--这里的key需写username和登录页面一致,value对应数据库用户名字段--> 

           <entrykey="username"value="loginname"/> 

       </map> 

扫描二维码关注公众号,回复: 2414706 查看本文章

    </property> 

    <propertyname="resultAttributeMapping"> 

       <map> 

           <!--key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值--> 

           <entrykey="suid"value="suid"/> 

       </map> 

    </property> 

    <!--   

   <property name="queryType"> 

       <value>OR</value> 

   </property>  

   --> 

</bean> 

切记:查询出来的字段名中间不能使用 _ (下划线),否则获取不到数据,如 cell_phone需要设置别名为 cellPhone.queryAttributeMapping是组装sql用的查询条件属性,上述配置后,结合封装成查询sql就是 
select* from userinfo where loginname=#username#resultAttributeMapping
sql执行完毕后返回的结构属性, key对应数据库字段,value对应客户端获取参数。如果要组装多个查询条件,需要加上下面这个,默认为AND.

<propertyname="queryType"> 

    <value>OR</value> 

</property>  

2、修改serviceRegistryDao

deployerConfigContext.xml中的 beansidserviceRegistryDao的属性 registeredServicesList。在 registeredServicesList中添加allowedAttributes属性的值。列出的每个值,在客户端就可以访问了。

<util:listid="registeredServicesList"> 

    <beanclass="org.jasig.cas.services.RegexRegisteredService" 

         p:id="0"p:name="HTTP andIMAP"p:description="Allows HTTP(S) and IMAP(S) protocols" 

         p:serviceId="^(http?|https?|imaps?)://.*"p:evaluationOrder="10000001"> 

         <!--添加该属性allowedAttributes --> 

         <propertyname="allowedAttributes"> 

              <list> 

                  <value>suid</value> 

              </list> 

         </property> 

       <!-- ignoreAttributestrue表示配置的 resultAttributeMapping 会返回--> 

      <propertyname="ignoreAttributes" value="true" /> 

    </bean> 

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

如果获取不到属性,则需要添加如下属性值:

<!-- ignoreAttributestrue表示配置的 resultAttributeMapping会返回--> 

<propertyname="ignoreAttributes" value="true" /> 

3、修改casServiceValidationSuccess.jsp

WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jsp.server验证成功后,这个页面负责生成与客户端交互的xml信息,在默认的casServiceValidationSuccess.jsp中,只包括用户名,并不提供其他的属性信息,因此需要对页面进行扩展。如下:

<cas:serviceResponsexmlns: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:iftest="${fn:length(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes)> 0}"> 

           <cas:attributes> 

                <c:forEachvar="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:iftest="${notempty pgtIou}">   

           <cas:proxyGrantingTicket>${pgtIou}</cas:proxyGrantingTicket> 

       </c:if>   

       <c:iftest="${fn:length(assertion.chainedAuthentications)> 1}"> 

           <cas:proxies>   

                <c:forEachvar="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> 

4CAS Client获取用户信息

AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal(); 
Map attributes = principal.getAttributes(); 
String email = attributes.get(“suid”);

参照网址:http://blog.csdn.net/u012410733/article/details/51729791

猜你喜欢

转载自blog.csdn.net/shunhua19881987/article/details/72625737
今日推荐