cas登录成功什么意思_cas登录成功返回用户更多信息

本文详细介绍了在CAS 3.4.4版本中如何配置attributeRepository以获取并返回用户的姓名、手机号和电子邮件等更多信息。主要涉及deployerConfigContext.xml的修改,包括配置属性repository、用户认证凭据转化的解析器以及casServiceValidationSuccess.jsp的扩展。客户端通过Java或PHP代码可以获取这些额外的用户属性。
摘要由CSDN通过智能技术生成

java客户端获取:

AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

String username = principal.getName();

php客户端获取

$username=phpCAS::getUser();

我们的程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况。cas

各种版本配置方式也不尽相同,这里讲的是目前最新版本3.4.4。配置方式如下,

一、首先需要配置属性attributeRepository,首先,你需要到WEB-INF目录找到

deployerConfigContext.xml文件,同时配置attributeRepository如下:

//这里的key需写username,value对应数据库用户名字段

其中queryAttributeMapping是组装sql用的查询条件属性,如下表中

结合 封装成查询sql就是select * from userinfo where loginname=#username#,resultAttributeMapping是sql执行完毕后返回的结构属性, key对应数据库字段,value对应客户端获取参数。

二、配置用户认证凭据转化的解析器,也是在deployerConfigContext.xml中,找到

credentialsToPrincipalResolvers,为UsernamePasswordCredentialsToPrincipalResolver注入attributeRepository,那么attributeRepository就会被触发并通过此类进行解析,红色为新添部分。

三、修改WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jsp,在server验证成功后,这个页面负责生成与客户端交互的xml信息,在默认的casServiceValidationSuccess.jsp中,只包括用户名,并不提供其他的属性信息,因此需要对页面进行扩展,如下,红色为新添加部分

${fn:escapeXml(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.id)}

${fn:escapeXml(attr.value)}

${pgtIou}

${fn:escapeXml(proxy.principal.id)}

通过完成上面三个步骤的配置后,server端的工作就完成了,那么如何在客户端获取这些信息呢?下面进行说明:

java客户端获取:

AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

Map attributes = principal.getAttributes();

String email=attributes .get("email");

php客户端;

$email=phpCAS::getAttribute('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类型的所以自定义

//    Map map=new HashMap();

//    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//                }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值