`
zxs19861202
  • 浏览: 908851 次
  • 性别: Icon_minigender_1
  • 来自: 湖北—》上海
社区版块
存档分类
最新评论

cas 3.4登录成功返回用户更多信息

阅读更多

  我的上一篇博客介绍了cas服务端,java和php客户端的配置。今天介绍下cas 3.4登录成

 功返回用户更多信息。cas登录成功默认返回的只有用户名,

 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如下:

 

 

<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#resultAttributeMappingsql执行完毕后返回的结构属性, 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>

 

三、修改WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jspserver验证成功后,这个页面负责生成与客户端交互的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>
</c:if>
 </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');

 

这里补充一下,实现返回功能的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//                }

 

至于原因嘛目前还没搞明白,有知道的可以交流下。

 

 

 

 

分享到:
评论
19 楼 周聪龙 2015-02-10  
楼主,我按照你的那个配置之后,怎么反而不能登录了,我以前只是简单地获取密码的那种最简单的配置能登录成功,把那个注销之后不能登录,不注销能登陆,但是也获取不到数据。这是什么情况??
18 楼 hapic89 2014-12-13  
sunsoldier 写道
AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

String username = principal.getName();

运行后principal总是为null,我都参考了N篇文章了,就是不行,我就想在客户端得到登录的用户名。
大哥,能不能把您的服务器端和客户端代码都发给我学习学习,简单的demo就行,谢谢您了,我真的很急啊!
我邮箱756535761@qq.com


    Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION); 
    AttributePrincipal principal = assertion.getPrincipal(); 
    String username = principal.getName(); 
这种方式试试
17 楼 zd987 2013-10-18  
多谢楼主,找了半天原因,原来是客户端使用的协议版本弄错了,cas客户端使用的是1.0协议,所以得不到其他属性。修改成2.0协议的filter后,就可以了。
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>

<filter-class>org.jasig.cas.client.validation.Cas10TicketValidationFilter</filter-class>
16 楼 sangxue2587 2013-03-13  
我按照你的配置客户端怎么都得不到属性。给个demo吧兄弟。我的邮箱是sangxue2587@163.com
15 楼 sangxue2587 2013-03-13  
你这个都没有demo。
这个让人怎么信服啊
14 楼 jalorchen 2012-10-19  
大神们。挖个坟先,我的 request 按楼主的说法我的NULLPOINTEXCEPTION 了,求解释,求帮忙。
13 楼 xiaokang1582830 2012-09-05  
老是提示CAS不支持您提供的凭证是怎么回事?
12 楼 chenzuotan 2012-08-16  
假如通过这样的方式 返回用户名与密码给 客户端应用  不知道安全性怎样?
11 楼 frozen_cmlei 2012-07-03  
chaico 写道
只要设置一下ignoreAttributes=true就可以了

<bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
		<property name="registeredServices">
			<list>
				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="0" />
					<property name="name" value="HTTP" />
					<property name="description" value="Only Allows HTTP Urls" />
					<property name="serviceId" value="http://**" />
					<property name="evaluationOrder" value="10000001" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>

				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="1" />
					<property name="name" value="HTTPS" />
					<property name="description" value="Only Allows HTTPS Urls" />
					<property name="serviceId" value="https://**" />
					<property name="evaluationOrder" value="10000002" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>

				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="2" />
					<property name="name" value="IMAPS" />
					<property name="description" value="Only Allows HTTPS Urls" />
					<property name="serviceId" value="imaps://**" />
					<property name="evaluationOrder" value="10000003" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>

				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="3" />
					<property name="name" value="IMAP" />
					<property name="description" value="Only Allows IMAP Urls" />
					<property name="serviceId" value="imap://**" />
					<property name="evaluationOrder" value="10000004" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>
			</list>
		</property>
	</bean>



 private RegisteredService constructDefaultRegisteredService(final List<String> attributes) {
        final RegisteredServiceImpl r = new RegisteredServiceImpl();
        r.setAllowedToProxy(true);
        r.setAnonymousAccess(false);
        r.setEnabled(true);
        r.setSsoEnabled(true);
        r.setAllowedAttributes(attributes);
        
        if (attributes == null || attributes.isEmpty()) {
            r.setIgnoreAttributes(true);
        }

        return r;
    }


里面已经自己设置了?
10 楼 frozen_cmlei 2012-07-03  
fanfree 写道
一步步配置过来,allowedAttributes可以配置了;可是map就是没有东西,求解!



service中断点调试已经将值放进去了,但是客户端也是取不到值,你解决没。?
9 楼 solomon 2012-04-06  
学习了,还有很多细节没提到。
8 楼 chaico 2012-03-22  
只要设置一下ignoreAttributes=true就可以了

<bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
		<property name="registeredServices">
			<list>
				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="0" />
					<property name="name" value="HTTP" />
					<property name="description" value="Only Allows HTTP Urls" />
					<property name="serviceId" value="http://**" />
					<property name="evaluationOrder" value="10000001" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>

				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="1" />
					<property name="name" value="HTTPS" />
					<property name="description" value="Only Allows HTTPS Urls" />
					<property name="serviceId" value="https://**" />
					<property name="evaluationOrder" value="10000002" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>

				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="2" />
					<property name="name" value="IMAPS" />
					<property name="description" value="Only Allows HTTPS Urls" />
					<property name="serviceId" value="imaps://**" />
					<property name="evaluationOrder" value="10000003" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>

				<bean class="org.jasig.cas.services.RegisteredServiceImpl">
					<property name="id" value="3" />
					<property name="name" value="IMAP" />
					<property name="description" value="Only Allows IMAP Urls" />
					<property name="serviceId" value="imap://**" />
					<property name="evaluationOrder" value="10000004" />
					<!-- ignoreAttributes为true表示 配置的 resultAttributeMapping 会返回-->
					<property name="ignoreAttributes" value="true" />
				</bean>
			</list>
		</property>
	</bean>
7 楼 Sunny_kaka 2011-11-29  
引用
写在最后,若按照以上配置还是不能获得返回值的话,我通过调试源码发现把源码中

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



至于原因嘛目前还没搞明白,有知道的可以交流下。



也遇见了楼主说的这个问题.
deployerConfigContext.xml文件中
<bean
id="serviceRegistryDao"
        class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
这个bean的registeredServices配置了不同协议对应的一些参数.
默认是
            <property name="registeredServices">
                <list>
                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="0" />
                        <property name="name" value="HTTP" />
                        <property name="description" value="Only Allows HTTP Urls" />
                        <property name="serviceId" value="http://**" />
                    </bean>

                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="1" />
                        <property name="name" value="HTTPS" />
                        <property name="description" value="Only Allows HTTPS Urls" />
                        <property name="serviceId" value="https://**" />
                    </bean>

                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="2" />
                        <property name="name" value="IMAPS" />
                        <property name="description" value="Only Allows HTTPS Urls" />
                        <property name="serviceId" value="imaps://**" />
                    </bean>

                    <bean class="org.jasig.cas.services.RegisteredServiceImpl">
                        <property name="id" value="3" />
                        <property name="name" value="IMAP" />
                        <property name="description" value="Only Allows IMAP Urls" />
                        <property name="serviceId" value="imap://**" />
                    </bean>
                </list>


RegisteredServiceImpl有一个allowedAttributes的属性很关键,这个属性配置的list就是允许哪些属性可以传送到客户端,因为我用的是http,所以在
http对应RegisteredServiceImpl中加入配置
                        <property name="allowedAttributes">
                        	<list>
                        		<value><!-- your attribute key --></value>
                        	</list>
                        </property>

即可
6 楼 fanfree 2011-10-18  
2011-10-18 19:47:20,153 INFO [com.github.inspektr.audit.support.Slf4jLoggingAudi
tTrailManager] - <Audit trail record BEGIN
=============================================================
WHO: audit:unknown
WHAT: ST-1-91RJwKTjEtnMhRtFdBeI-cas
ACTION: SERVICE_TICKET_VALIDATED
APPLICATION: CAS
WHEN: Tue Oct 18 19:47:20 CST 2011
CLIENT IP ADDRESS: 20.1.44.218
SERVER IP ADDRESS: 20.1.44.218
=============================================================


WHO: audit:unknown 大家知道什么问题导致的吗 谢谢
5 楼 fanfree 2011-10-18  
deployerConfigContext.xml 能否发上来看看 各位
4 楼 fanfree 2011-10-18  
一步步配置过来,allowedAttributes可以配置了;可是map就是没有东西,求解!
3 楼 xiaoxiao_qiang 2011-09-21  
最后一步,还需要在registeredService中配置allowedAttributes,这样客户端拿到的principal中就有属性了~欢迎与我交流xiaoxiao_qiang@126.com
2 楼 zxs19861202 2011-04-22  
principal 这个为null很可能是你的服务端配置有问题,具体的服务端和客户端的配置请参考我上一篇博客,我写的还是蛮详细的,照着配置就ok了。有问题也可以给我发站内信息。
1 楼 sunsoldier 2011-04-08  
AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

String username = principal.getName();

运行后principal总是为null,我都参考了N篇文章了,就是不行,我就想在客户端得到登录的用户名。
大哥,能不能把您的服务器端和客户端代码都发给我学习学习,简单的demo就行,谢谢您了,我真的很急啊!
我邮箱756535761@qq.com

相关推荐

Global site tag (gtag.js) - Google Analytics