Mvc4单点登录之四 配置Cas服务端,返回更多的用户信息!

        .Net单点登录详解 (SSO)

       Mvc4单点登录之一Cas简单介绍

       Mvc4单点登录之二 Cas server端配置

           Mvc4单点登录之三Cas 客户端配置

           前几篇博客大致的介绍了,cas的使用,在这篇博客当中,将为大家介绍一下如何配置服务端,让用户登录之后,返回更多的用户信息!

一、首先需要配置属性attributeRepository

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

<bean  class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"id="attributeRepository">
        <constructor-argindex="0" ref="casDataSource"/>
        <constructor-argindex="1" value="select * from userinfo where {0}"/>
        <propertyname="queryAttributeMapping">
            <map>
                <entrykey="username" value="loginname"/>  // 这里的key需写username和登录页面一致,value对应数据库用户名字段
            </map>
        </property>
        <propertyname="resultAttributeMapping">
            <map>
// <!--key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值-->  
                <entrykey="id" value="id"/>
                <entrykey="mobile" value="mobile"/>
                <entrykey="email" value="email"/>
            </map>
        </property>
    </bean>


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

<property name="queryType">
          <value>OR</value>
</property> 

二、配置用户认证凭据转化的解析器

也是在 deployerConfigContext.xml 中,为UsernamePasswordCredentialsToPrincipalResolver 注入attributeRepository,那么 attributeRepository 就会被触发并通过此类进行解析,红色为新添部分。
<propertyname="credentialsToPrincipalResolvers">
            <list>        
                <beanclass="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">
                   <span style="color:#ff0000;"> <propertyname="attributeRepository" ref="attributeRepository"/></span>
                </bean>
                <beanclass="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver"/>
            </list>
 </property>

三、修改 deployerConfigContext.xml中的 org.jasig.cas.services.InMemoryServiceRegistryDaoImpl属性 registeredServices

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

<bean  id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
           <property name="registeredServices">
               <list>
                   <beanclass="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?)://.*" />
                       <propertyname="evaluationOrder" value="10000001" />
                       <propertyname="allowedAttributes"> // 客户端需要使用的对象的属性名称
                              <list>
                                      <value>uid</value>
                                      <value>email</value>
                                      <value>mobile</value>
                              </list>
                       </property>
                   </bean>
	      </list>
	</property>
</bean>


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

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

四、修改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: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:iftest="${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>


通过这些配置,就可以在客户端获取到更多的返回的信息了!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。