一旦CAS SERVER验证成功后,咱们就会跳转到客户端中去。跳转到客户端去后,你们想想,客户端总要获取用户信息吧,否则客户端是怎么知道登陆的是哪一个用户。那么客户端要怎么获取用户信息呢?html
其实验证成功,跳转客户端这个过程当中,CAS SERVER 会返回登陆的相关信息给客户端,客户端只要进行获取,就能知道登陆的具体是哪一个用户了。不过CAS 默认只返回用户帐号给客户端,那么怎么定义CAS SERVER返回的信息呢? 这就是本篇具体讲解的内容了,你们听我慢慢道来。java
在开始时,咱们先了解下有关相关的几个接口mysql
Credentials (org.jasig.cas.authentication.Credentials)接口,咱们在上一篇其实有使用过,咱们当时有用过一个叫 UsernamePasswordCredential 的类,就是实现了Credentials接口。这个接口是用来定义咱们登陆页上输入的认证信息的,好比用户名、密码、验证码等,能够理解为用户认证的相关凭据。web
Principal (org.jasig.cas.authentication.principal.Principal) 接口,这个主要是用来保存用户认证后的用户信息,信息保存在一个Map中。sql
IPersonAttributeDao (org.jasig.services.persondir.IPersonAttributeDao) 接口,这个是用来定义咱们须要返回给客户端相关信息的接口,CAS SERVER 默认有提供许多实现,好比数据库
等等,还有许多,你们能够参考源码中的实现,CAS SERVER 提供了各类功能的实现,有时候咱们能够直接使用这个现成的就好了。app
PrincipalResolver(org.jasig.cas.authentication.principal.PrincipalResolver) 接口,上面有说到 Credentials 是从登陆页面上进行获取相关用户信息的。那么认证成功后,怎么把Credentials里面的信息转换到 Principal 中呢,这就是这个接口的做用了。因为认证自己是没有返回用户信息的,只是肯定认证是经过仍是没有经过。这时还要用到咱们上面的IPersonAttributeDao 接口,在这接口中咱们就能够定义咱们须要返回的信息了。webapp
这接口中有两个方法jsp
ps: 在3.x版本中没有 PrincipalResolver接口,对应的是CredentialsToPrincipalResolver, PrincipalResolver这个是在4.0版本中加入的,你们要注意。ide
相关接口讲解后,你们应该对怎么返回信息有个大概的思路了。没错就是实现上面所说的 IPersonAttributeDao 、PrincipalResolver 接口 。下面根据代码讲解下具体的一个流程:
首先打开 deployerConfigContext.xml 文件,看下面的定义:
<!-- | Resolves a principal from a credential using an attribute repository that is configured to resolve | against a deployer-specific store (e.g. LDAP). --> <bean id="primaryPrincipalResolver" class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver" > <property name="attributeRepository" ref="attributeRepository" /> </bean> <!-- Bean that defines the attributes that a service may return. This example uses the Stub/Mock version. A real implementation may go against a database or LDAP server. The id should remain "attributeRepository" though. +--> <bean id="attributeRepository" class="org.jasig.services.persondir.support.StubPersonAttributeDao" p:backingMap-ref="attrRepoBackingMap" /> <util:map id="attrRepoBackingMap"> <entry key="uid" value="uid" /> <entry key="eduPersonAffiliation" value="eduPersonAffiliation" /> <entry key="groupMembership" value="groupMembership" /> </util:map>
//PersonDirectoryPrincipalResolver 部分源码
public final Principal resolve(final Credential credential) { logger.debug("Attempting to resolve a principal..."); String principalId = extractPrincipalId(credential); //extractPrincipalId 方法从credential中抽取id //省略... final IPersonAttributes personAttributes = this.attributeRepository.getPerson(principalId); //根据IPersonAttributeDao 中的getPerson 获取返回的属性 final Map<String, List<Object>> attributes; //最终返回 Principal return new SimplePrincipal(principalId, convertedAttributes); }
具体流程:
1.从上面的deployerConfigContext.xml 配置咱们能够看到,CAS 默认配置了一个叫作 PersonDirectoryPrincipalResolver 的类,在 这个类的 resolve 方法中有调用 extractPrincipalId 这个方法,这个方法传入一个 Credentials 类型的参数,默认调用的是Credentials 的getId() 方法,CAS默认是返回用户的userName,即登陆帐号。不过getId() 这个方法的实现咱们能够在上一章中指定的UsernamePasswordCredential 类中自定义,通常是定义成返回用户的userId或者其余惟一键,由于咱们若是知道了用户的userId,那么就能够根据这个从数据库中查询中用户的一些具体信息了,进而就能够组成咱们须要返回的信息。
2. 继续往下看源码,接着在 PersonDirectoryPrincipalResolver 中有注入一个 attributeRepository 属性,这个就是上面的IPersonAttributeDao 接口,而后在resolve方法中调用了 IPersonAttributeDao 接口 的getPerson方法,还传入了一个参数principalId,其实这个传入的参数就是咱们上面 getId() 返回的值。
因此其实咱们只要实现咱们须要的 IPersonAttributeDao 就能够了。 下面给一个简单的IPersonAttributeDao 例子:
public class BlogStubPersonAttributeDao extends StubPersonAttributeDao { @Override public IPersonAttributes getPerson(String uid) { Map<String, List<Object>> attributes = new HashMap<String, List<Object>>(); attributes.put("userid", Collections.singletonList((Object)uid)); attributes.put("cnblogUsername", Collections.singletonList((Object)"http://www.cnblogs.com/vhua")); attributes.put("cnblogPassword", Collections.singletonList((Object)"123456")); attributes.put("test", Collections.singletonList((Object)"test")); return new AttributeNamedPersonImpl(attributes); } }
这边传入的uid 默认是用户的登陆名,咱们这边没有作修改,直接用默认的。
这边是只是测试用,因此就直接写死了,实际开发确定是须要在数据库或者LDAP中进行查询后,而后组装成须要的信息 。
而后在 deployerConfigContext.xml 中修改
<bean id="primaryPrincipalResolver" class="org.jasig.cas.authentication.principal.PersonDirectoryPrincipalResolver" > <property name="attributeRepository" ref="attributeRepository" /> </bean>
<!-- 修改前 --> <bean id="attributeRepository" class="org.jasig.services.persondir.support.StubPersonAttributeDao" p:backingMap-ref="attrRepoBackingMap" /> <util:map id="attrRepoBackingMap"> <entry key="uid" value="uid" /> <entry key="eduPersonAffiliation" value="eduPersonAffiliation" /> <entry key="groupMembership" value="groupMembership" /> </util:map> <!-- 修改前 end-->
<!--修改后--> <bean id="attributeRepository" class="org.jasig.services.persondir.support.BlogStubPersonAttributeDao" /> <!--修改后 end-->
3. 修改完成后,咱们还须要在 casServiceValidationSuccess.jspcas-server-webapp\src\main\webapp\WEB-INF\view\jsp\protocol\2.0\casServiceValidationSuccess.jsp)
添加一段代码(下面红色部分):
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'> <cas:authenticationSuccess> <cas:user>${fn:escapeXml(assertion.primaryAuthentication.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> <!-- 这段 end-- >
<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>
4. 接下来 在客户端设置信息的接收,咱们直接在index.jsp中测试一下:
在Java中能够经过下面的方式获取
AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
Map attributes = principal.getAttributes();
String xxx=attributes .get("xxx");
...
<!DOCTYPE html"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>返回值测试</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal(); Map attributes = principal.getAttributes(); String userid=(String)attributes.get("userid"); String cnblogUsername = (String)attributes.get("cnblogUsername"); String cnblogPassword = (String)attributes.get("cnblogPassword"); String test=(String)attributes.get("test"); %> <div>飞奔的蜗牛博客:返回值演示</div> <ul> <li>userid:<%= userid%></li> <li>username:<%= cnblogUsername%></li> <li>password:<%= cnblogPassword%></li> <li>test:<%= test%></li> </ul> </body> </html>
好了,咱们登陆运行看看结果。
你们看到没,已经获取成功了,在CAS SERVER那边设置的信息,咱们正常获取到了 。你们能够根据业务的须要,返回相关的信息。而后在客户端进行操做。
转自:http://www.cnblogs.com/vhua/p/cas_4.html