文章中 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">
<entry key="id" value="id"/>
<entry key="mobile" value="mobile"/>
<entry key="email" value="email"/>
</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>
</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');