CAS 客户端验证成功获取更多用户信息

cas客户端需要获取更多的用户信息,需要对cas server做下修改,以支持返回更多属性信息

1.修改WEB-INF/deployerConfigContext.xml配置文件
找到id="authenticationManager" 的bean,为属性credentialsToPrincipalResolvers增加自定义bean,该bean需实现org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver接口:
<property name="credentialsToPrincipalResolvers">
<list>
[color=red]<!--add ,返回客户端更多认证信息, linym, 2014-06-10-->
<bean id="userAttributeRepository" class="com.*.*.UserAttributeRepository" />[/color]

...

</list>

</property>

public class UserAttributeRepository implements CredentialsToPrincipalResolver {
public Principal resolvePrincipal(Credentials credentials) {
String principalId = extractPrincipalId(credentials);
final Map attributes = new HashMap();
//这些属性通过 request 获取
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
LoginLogBean logBean = new LoginLogBean(request);
attributes.put("mac", logBean.getMac());
attributes.put("ip", logBean.getIp());
attributes.put("source", logBean.getSource());
attributes.put("service", logBean.getService());

attributes.put("field1", request.getAttribute("field1"));

return new SimplePrincipal(principalId, attributes);
}

public boolean supports(Credentials credentials) {
return credentials != null;
}
protected String extractPrincipalId(final Credentials credentials) {
final UsernamePasswordCredentials usernamePasswordCredentials = (UsernamePasswordCredentials) credentials;
return usernamePasswordCredentials.getUsername();
}

}


接下来需要修改WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jsp增加返回客户端的属性内容
<%@ page session="false" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %><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>
[color=red]<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> [/color]
<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>

以上红色字体为新增部分。


解下来客户端调用获取设置的属性信息:

Map attrMap = ((AttributePrincipal) request.getUserPrincipal()).getAttributes();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CAS(Central Authentication Service)是一种单点登录(SSO)协议,用于实现在多个应用系统中共享用户登录状态的功能。CAS客户文档Java是指CAS协议的Java客户的使用文档。 CAS客户文档Java主要包括以下内容: 1. 安装和配置:首先需要将CAS客户的Java库文件导入到项目中,并配置必要的参数,如CAS服务器地址、服务口等。 2. 登录和认证:CAS客户提供了一套API接口,用于用户登录认证。开发者可以通过调用这些接口实现用户登录功能。用户登录后,CAS客户会将用户信息保存在本地的会话中,以便在后续的请求中进行身份验证。 3. 单点登录(SSO):CAS客户能够与CAS服务器进行通信,实现跨应用系统的单点登录功能。用户只需要登录一次,即可在多个应用系统中共享登录状态,无需再次输入用户名和密码。 4. 会话管理:CAS客户还提供了一套会话管理机制,用于管理用户登录状态。开发者可以通过这些机制来管理用户的登录会话、注销会话等。 5. 安全机制:CAS客户实现了一套安全机制,用于保护用户的登录状态和用户信息的安全性。开发者可以根据需要进行配置,以确保用户数据的安全。 通过阅读CAS客户文档Java,开发者可以了解CAS客户的基本原理和使用方法,能够好地使用CAS客户来实现单点登录功能,提升系统的用户登录体验和安全性。同时,该文档还提供了示例代码和常见问题解答,方便开发者快速上手和解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值