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如下:
?
?
??????????????????? <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客户端获取:
<property name="registeredServices"> <list> <bean value="0" /> <property name="name" value="HTTP" /> <property name="description" value="Only Allows HTTP Urls" /> <property name="serviceId" value="http://**" /> </bean> <bean value="1" /> <property name="name" value="HTTPS" /> <property name="description" value="Only Allows HTTPS Urls" /> <property name="serviceId" value="https://**" /> </bean> <bean value="2" /> <property name="name" value="IMAPS" /> <property name="description" value="Only Allows HTTPS Urls" /> <property name="serviceId" value="imaps://**" /> </bean> <bean 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>
即可<bean id="serviceRegistryDao" 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 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 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 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><bean id="serviceRegistryDao" 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 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 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 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; }里面已经自己设置了? 12 楼 chenzuotan 2012-08-16 假如通过这样的方式 返回用户名与密码给 客户端应用 不知道安全性怎样? 13 楼 xiaokang1582830 2012-09-05 老是提示CAS不支持您提供的凭证是怎么回事? 14 楼 jalorchen 2012-10-19 大神们。挖个坟先,我的 request 按楼主的说法我的NULLPOINTEXCEPTION 了,求解释,求帮忙。