讓cas-service 返回更多用戶信息

CAS 在默認狀況下僅提供了返回用戶名給客戶端的。但鑑於一些客戶端系統在登陸回來獲取用戶信息時,username不能惟一肯定一個用戶,可能用戶名+工號、地區編號+用戶名等各類各樣的狀況。所以,咱們須要對CAS-service 進行擴展。對,沒錯,cas必然有考慮這一點。cas提供了配置的方式給咱們方便的去返回更多用戶的信息,但因爲配置始終具有必定的侷限性,因此咱們本身實現這個功能。ide

1.咱們先看看cas的基本實現。從實現中看到,確實只返回了username

<!-- deployerConfigContext.xml  -->
<bean id="attributeRepository" class="org.jasig.services.persondir.support.NamedStubPersonAttributeDao"
          p:backingMap-ref="attrRepoBackingMap" />

<util:map id="attrRepoBackingMap">
    <entry key="uid" value="uid" />
    <entry key="eduPersonAffiliation" value="eduPersonAffiliation" />
    <entry key="groupMembership" value="groupMembership" />
    <entry>
        <key><value>memberOf</value></key>
        <list>
            <value>faculty</value>
            <value>staff</value>
            <value>org</value>
        </list>
    </entry>
</util:map>

public class NamedStubPersonAttributeDao extends StubPersonAttributeDao {
    public NamedStubPersonAttributeDao() {
    }

    public NamedStubPersonAttributeDao(Map backingMap) {
        super(backingMap);
    }

    public final Set<IPersonAttributes> getPeopleWithMultivaluedAttributes(Map<String, List<Object>> query) {
        List list = (List)query.get("username");
        HashMap m = new HashMap(this.getBackingMap());
        m.put("username", list);
        this.setBackingMap(m);
        return super.getPeopleWithMultivaluedAttributes(query);
    }
}

2.必然要把原始實現先註釋掉

3.添加自定義實現配置

<!-- deployerConfigContext.xml  -->
<bean id="attributeRepository" class="com.ucap.igsd.cas.handler.UserStubPersonAttributeDao" p:accountDao-ref="accountDao" />

4.實現UserStubPersonAttributeDao

public class UserStubPersonAttributeDao extends StubPersonAttributeDao {

    public AccountDao accountDao;

    @Override
    public IPersonAttributes getPerson(String uid) {
        Map<String, List<Object>> attributes = new HashMap<String, List<Object>>();
        try {
            Account account = accountDao.getAccountInfo(uid);
            attributes.put("username", Collections.singletonList((Object)URLEncoder.encode(account.getUserName(), "UTF-8")));
            attributes.put("email", Collections.singletonList((Object)URLEncoder.encode(account.getEmail(), "UTF-8")));
            attributes.put("phone", Collections.singletonList((Object)URLEncoder.encode(account.getPhone(), "UTF-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new AttributeNamedPersonImpl(attributes);
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
}

基本就是這樣吧!須要更加高級的本身看着辦。

相關文章
相關標籤/搜索