[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[整理]CAS单点登录,服务端返回更多用户属性,修改代码实现~

上一篇:[备忘]access的建表语句什么的,Intermediate Microsoft Jet SQL for Access 2000
下一篇:[备忘]oracle数据库中ORA-28000: the account is locked

添加日期:2014/6/3 17:31:47 快速返回   返回列表 阅读4035次
一般都是通过配置实现,实在有点晕~~

可以搜索:cas 返回更多
----------------------------------
配置很麻烦,而且没咋看懂,干脆点,咱直接改代码,反正已经改了好多了

瞎改凑合用呗~~
----------------------------------
取用户信息在org.jasig.cas.CentralAuthenticationServiceImpl类中

validateServiceTicket方法中。


final String principalId = determinePrincipalIdForRegisteredService(principal, registeredService, serviceTicket);
            final Authentication authToUse;

            if (!registeredService.isIgnoreAttributes()) {
final Map<String, Object> attributes = new HashMap<String, Object>();

                for (final String attribute : registeredService.getAllowedAttributes()) {
                    final Object value = principal.getAttributes().get(attribute);

                    if (value != null) {
                        attributes.put(attribute, value);
                    }
                }

//-----------------在此往attributes里put东西即可----------------

final Principal modifiedPrincipal = new SimplePrincipal(principalId, attributes);
                final MutableAuthentication mutableAuthentication = new MutableAuthentication(
                    modifiedPrincipal, authentication.getAuthenticatedDate());
                mutableAuthentication.getAttributes().putAll(
                    authentication.getAttributes());
                mutableAuthentication.getAuthenticatedDate().setTime(
                    authentication.getAuthenticatedDate().getTime());
                authToUse = mutableAuthentication;
            } else {
                final Principal modifiedPrincipal = new SimplePrincipal(principalId, principal.getAttributes());
                authToUse = new MutableAuthentication(modifiedPrincipal, authentication.getAuthenticatedDate());
            }


看上面的代码,里面注释那行,在那put东西即可。

我是直接把这个类改成继承extends
AbstractJdbcUsernamePasswordAuthenticationHandler了,

就可以取DB了


String sql = "SELECT count(*) as myCount from ...";
List xx = getJdbcTemplate().queryForList(sql);
Object object = ((Map) xx.get(0)).get("myCount");



应该有正常的方法,但偶不会,就这样瞎用吧。
-------------------------------
如此改完还不够,最后结果是通过xml返回给客户端的,

这个xml格式是在WEB-INF\view\jsp\protocol\2.0\casServiceValidationSuccess.jsp中写的。

默认只输出了用户名,改下。


    <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>这段是后加的,把attributes里面的东西输出到客户端了。
--------------------------------------
这样就OK了。

客户端:


Assertion assertion = (Assertion) req.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
if(assertion!=null){
    String userName = assertion.getPrincipal().getName();
    Object xxx = assertion.getPrincipal().getAttributes().get("xxx");

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved