我们再使用CAS作为单点登录服务器时,在某些情况下希望从CAS Server端获取更多的用户信息,比如用户姓名、手机号、Email等,默认情况下Cas至返回用户的登录名
基于以上的Cas版本,开始让Cas返回更多的用户信息。
一、配置属性attributeRepository
<bean id="selfAttributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"> <constructor-arg index="0" ref="dataSource" /> <constructor-arg index="1" value="select email,name,login_name,password from isc_user where {0}" /> <!-- 组装sql用的查询条件属性 --> <property name="queryAttributeMapping"> <map> <!-- key必须是uername而且是小写否则会导致取不到用户的其它信息,value对应数据库用户名字段,系统会自己匹配 --> <entry key="username" value="login_name" /> </map> </property> <property name="resultAttributeMapping"> <map> <!-- key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值 --> <entry key="login_name" value="username"></entry> <entry key="email" value="email"></entry> <entry key="name" value="name"></entry> <entry key="password" value="password"></entry> </map> </property> </bean>
二、配置用户认证凭据转化的解析器
在deployerConfigContext.xml中,找到credentialsToPrincipalResolvers,为UsernamePasswordCredentialsToPrincipalResolver注入attributeRepository
<bean class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver" > <property name="attributeRepository" ref="attributeRepository" /> </bean>
替换成
<bean class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver" > <property name="attributeRepository" ref="selfAttributeRepository" /> </bean>
三、将添加的用户信息返回给客户端
找到WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jsp。此文件作用是在server验证成功后,这个页面负责生成与客户端交互的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="${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>
扩展为如下
<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="${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> <!-- 在server验证成功后,这个页面负责生成与客户端交互的xml信息,在默认的casServiceValidationSuccess.jsp中,只包括用户名,并不提供其他的属性信息,因此需要对页面进行扩展 --> <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> </cas:authenticationSuccess> </cas:serviceResponse>
通过完成上面三个步骤的配置后,server端的工作就完成了,那么如何在客户端获取这些信息呢?
下面进行说明:
欢迎,<%=AssertionHolder.getAssertion().getPrincipal().getName() %><br/> <% AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal(); String loginName = principal.getName(); out.println("loginName:" + loginName); Map<String, Object> attributes = principal.getAttributes(); out.println("<br>"); if (attributes != null) { out.println("username:" + attributes.get("username")); out.println("<br>"); out.println("password:" + attributes.get("password")); out.println("<br>"); out.println("email:" + attributes.get("email")); out.println("<br>"); out.println("name:" + attributes.get("name")); out.println("<br>"); } out.println("------------------华丽的分割线---------------------"); out.println("<br>"); AttributePrincipal principal2 = AssertionHolder.getAssertion().getPrincipal(); String loginName2 = principal2.getName(); out.println("loginName:" + loginName2); Map<String, Object> attributes2 = principal2.getAttributes(); out.println("<br>"); if (attributes2 != null) { out.println("username:" + attributes.get("username")); out.println("<br>"); out.println("password:" + attributes2.get("password")); out.println("<br>"); out.println("email:" + attributes2.get("email")); out.println("<br>"); out.println("name:" + attributes2.get("name")); out.println("<br>"); } %>
说明:AsserionHolder工具怎么获取用户信息的?
回想下,我们是不是在客户端的程序中WEB.xml文件配置过如下信息
<filter> <filter-name>CAS Assertion Thread Local Filter</filter-name> <filter-class>org.jasig.cas.client.javafilter.util.AssertionThreadLocalFilter</filter-class> </filter> <filter-mapping> <filter-name>CAS Assertion Thread Local Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
再让我们看下AssertionThreadLocalFilter.java又是如何工作的
public final class AssertionThreadLocalFilter implements Filter { public void init(final FilterConfig filterConfig) throws ServletException { // nothing to do here } public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { final HttpServletRequest request = (HttpServletRequest) servletRequest; final HttpSession session = request.getSession(false); final Assertion assertion = (Assertion) (session == null ? request.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION) : session.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION)); try { AssertionHolder.setAssertion(assertion); filterChain.doFilter(servletRequest, servletResponse); } finally { AssertionHolder.clear(); } } public void destroy() { // nothing to do }
我想大家应该看明白了吧
相关推荐
使用struts2+spring+cas实现的单点登录功能,里面包括cas-server3.5.2项目一个,cas-client3.2.1 web项目两个,数据库脚本,请按照里面的说明文档进行部署,希望你们也能配置成功。
1 单点登录总体解决方案 2 CAS原理和协议 3 CAS安全性 4 CAS工作模式 5 系统设计方案 6 CAS关键技术 6.1 域名规范 6.2 中文用户登录提交时乱码 6.3 客户端获取中文用户名及...9.3 TOMCAT中使用CAS实现单点登录LDAP方式
主要讲述了CAS单点登录多数据及获取更多用户信息配置。
这个资源是一个CAS(Central Authentication Service)单点登录演示项目,用于展示如何实现基于CAS的单点登录系统。CAS是一种常用的身份认证和授权解决方案,适用于分布式系统中的用户身份验证。该演示项目将通过...
SSO通过将用户登录信息映射到浏览器cookie中,解决其他应用免登获取用户会话的问题。 为什么需要SSO 开放平台业务本身不需要SSO,但是如果平台的普通用户也可以在申请后成为一个应用开发者,那么就需要将平台加入到...
提供了cas server 5.3.9 单点登录的资源整合,cas服务器,运行即可用 配置了spring srcurity 密码验证方式
CAS 全称集中式认证服务(Central Authentication Service),是实现单点登录(SSO)的一中手段。 CAS 的通讯流程图如下(图片来自Google图库): 对于本文用户可感知的层面,认证过程如下: 前端访问后端登录接口 ...
-- 该过滤器使得开发者可以通过 org.jasig.cas.client.util.AssertionHolder 来获取用户 的登录名。 比如 AssertionHolder.getAssertion().getPrincipal().getName()。 --> <filter-name>CAS Assertion ...
单点登录cas服务端连接数据库获取用户数据做登录验证用
│ ├─jeeplatform-sso-cas-----------------------CAS单点登录服务端 │ │ │ ├─jeeplatform--sso-oauth2---------------OAuth2.0单点登录服务端 │ │ ``` ## 二、系统设计 ### 系统管理(模块名称...
文档里面介绍了cas单点登录的主要配置及多个用户信息的配置,在客户端怎么才能获取多样的用户信息。
上述snsapi_base模式是静默模式,不需微信用户手动授权,用户感受不到界面的跳转,但仅可获取用户的openid信息,对于我们来说openid暂时足够了。 第二步:通过code换取网页授权access_token 获取code后,请求以下...
为了使示例应用程序正常运行,我们根据 MySQL 用户数据表引入了特定的 CAS 配置。 由于 CAS 架构和基于源代码的特性,在创建 CAS.war 文件时应该编译一些 MySQL 模块。安装和配置创建一个 WWPass 开发者帐户在上...
auth-cas单点登录服务器端,所有客户端的用户登录都需要转移到cas服务器端。提供用户名密码登录和qq登录两种方式 auth-cas-shiro-common shiro-cas整合需要的工具类,供所有客户端帖子使用,只需将其作为jar包即可...
CAS单点登录机制使用RSA2和RSA进行数据加密和解密。 - utils:工具包,包含一些实用的函数,如从UA池中获取随机的User-Agent,和操作数据库的方法等。 - spider.py:爬虫主文件,根据城市和地区爬取相应的数据,并将...
获取有关当前用户的信息 75 9.2.2 UserDetailsService 75 9.2.3授予权力 77 9.2.4总结 77 9.3认证 78 9.3.1什么是Spring Security中的认证? 78 9.3.2直接设置SecurityContextHolder内容 80 9.4 Web应用程序中的身份...
单点登录: 统一认证平台UC: 职责:登录和鉴权 登录:生成token信息,将信息存到UC域名下的cookies中 鉴权:验证token的正确性,子系统每个请求会首先到UC来验证token是否有效 返回token信息:子系统会使用JSONP跨域...
授权模块,支持CAS单点登录,简单properties配置即可,不用再写很多的xml。 支持多数据源,简单properties配置即可实现,为了安全性吧,暂不提供界面维护数据源,不存数据库。 数据表主键优化,如分类科目表,采用有...
JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...
JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor ...