1. 程式人生 > >cas 單點登入設定使用者資訊

cas 單點登入設定使用者資訊

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>

                <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#resultAttributeMappingsql執行完畢後返回的結構屬性, 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");

這裡補充一下,實現返回功能的attributeRepository在person-directory-impl-1.5.0-RC6.jar這個jar包中,其中執行查詢的在類org.jasig.services.persondir.support.jdbc.AbstractJdbcPersonAttributeDao.java中,如果對返回值有其他要求,比如我的就是需要呼叫webservice獲取返回值可以在這個檔案封裝自己的返回值,或者修改查詢條件。

 List results = this.simpleJdbcTemplate.query(querySQL, rowMapper, params);

 //List中results 中儲存的物件為Map<String,Object>型別的所以自定義


//    Map<String, Object> map=new HashMap<String, Object>();
//    map.put("ID", 3);
//    map.put("LOGINNAME", "allen");
//    map.put("PASSWORD","123456");
//    map.put("ADDTIME", "2010-11-29 00:00:00.0");
//    map.put("STATE", 0);
//    map.put("MOBILE", "123456789");
//    map.put("EMAIL", [email protected]);    
//    results.add(map);

 return parseAttributeMapFromResults(results, queryUserName)