CAS登录成功后返回更多信息

转自:http://blog.csdn.net/tianfei_1984/article/details/8622810



今天介绍下cas 3.4登录成

 功返回用户更多信息。cas登录成功默认返回的只有用户名,

 java客户端获取:

 

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

String username = principal.getName();


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

 

 各种版本配置方式也不尽相同,这里讲的是目前最新版本3.4.4。配置方式如下,

 

 一、首先需要配置属性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>

                //这里的key需写username,value对应数据库用户名字段

                <entry key="username" value="loginname"/>

            </map>

        </property>

        <property name="resultAttributeMapping">

            <map>

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

                <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>

 

三、修改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");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值