在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#,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>
除此之外:
實現返回功能的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", test@126.com);
// results.add(map);
return parseAttributeMapFromResults(results, queryUserName);
寫在最后,若按照以上配置還是不能獲得返回值的話,我通過調試源碼發現把源碼中
org.jasig.cas.CentralAuthenticationServiceImpl.java 編譯后再cas-server-core-3.4.4.jar中的第360-368行代碼給注釋掉就ok了,
360// for (final String attribute : registeredService
361// .getAllowedAttributes()) {
362// final Object value = principal.getAttributes().get(
363// attribute);
364//
365// if (value != null) {
366// attributes.put(attribute, value);
367// }
368// }
至於原因嘛目前還沒搞明白,有知道的可以交流下。